Jest: toMatchSnapshot(filepath)

Created on 9 Aug 2017  路  6Comments  路  Source: facebook/jest

The current jest snapshot tests are annoying in the context of jest (and I presume relay as well) because they put things inside of quotes and add some small wrapper code to make it parse as js and it generates those snapshots in some __ directory.

I've been thinking about this problem for a while and I think I finally have a solution: add the ability to specify the filename as an argument of toMatchSnapshot().

Current

/* file.js */
expect(value).toMatchSnapshot()
/* __snapshots__/file.js.snap */
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`file.js 1`] = `
value
`;

Suggestion

/* file.js */
expect(value).toMatchSnapshot('file.snap')
/* file.snap */
value

Discussion

I'm not 100% sure how to remove unused snapshot files when they are no longer in use.

Most helpful comment

Not currently. I don't think it's reasonable to deploy something at Facebook that doesn't clean up after itself. We'd have multiple thousand snapshot files at FB at this point if we didn't have the consistency check :(

All 6 comments

Yeah, we cannot do this, precisely for the reason you mentioned about consistency between snapshot files and tests. If we had to apply static analysis to get this information, it would break down in a bunch of ways and make this really hard :(

Something that might help: https://github.com/facebook/jest/pull/4183

I'm happy not having unused snapshot files being removed automatically to be honest.

Is there a way to observe if jest is running with -u from within a test in order to implement this proposal in userland?

Not currently. I don't think it's reasonable to deploy something at Facebook that doesn't clean up after itself. We'd have multiple thousand snapshot files at FB at this point if we didn't have the consistency check :(

Hey, I've created this matcher that allows specifying snapshot location in the following way:

expect(value).toMatchSpecificSnapshot('path/to/file.snapshot')

Hope it will help.

Was this page helpful?
0 / 5 - 0 ratings