-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[proposal] Fixture/snapshot tests - write in separate files #6383
Comments
Is the |
The problem with current approach is that snapshots are colocated to the file that is doing an
Taking |
I also need this for Engrafo, a LaTeX to HTML converter. I use Jest snapshots to ensure the converted HTML is correct, but it would be really nice if the HTML snapshots were real files on disk so I could open them up in a browser, use them in other tools, etc. |
Is it possible to implement this in userland, kinda like what https://github.com/americanexpress/jest-image-snapshot is doing? Something like this might prove useful for both transpilers and bundlers Potential API if the matcher is implemented in user-land as const inputFiles = glob
.sync('test/fixtures/**/actual.js')
.map(filename => ({ filename, code: fs.readFileSync(filename, 'utf8') }));
describe('transform files', () => {
test.each(inputFiles)('testing my file', ({ filename, code }) => {
const result = babel.transform(code, { filename }).code;
expect(result).toMatchFileSnapshot();
});
}); |
I had the same use case and wrote this: https://github.com/satya164/jest-file-snapshot The code was mostly from what jest-image-snapshot has. But I have to use this private API: https://github.com/satya164/jest-file-snapshot/blob/master/index.js#L19 Would be nice if I could implement it in userland without using the private API. cc @thymikee |
@satya164 this is amazing! i would still argue that this should be the part of the jest's core though, super useful feature for multitude of tooling |
@satya164 This is great! What would you need in the snapshotstate to implement it the way you want? |
@rickhanlonii thoughts on the API? |
@Andarist sorry for the delay Just for clarity, can you add an example snapshot file output to OP? How would multiple snapshot tests in one test file work? |
For my use case, each test will have a different snapshot file regardless of where it is. The library I published requires you to specify the path of the file. Maybe it can also automatically determine a filename based on the title of the test in future. I'm also writing a tool for testing babel plugins where you can specify a directory containing fixtures like so: .
├── function-expression
│ ├── code.js
│ └── output.js
├── invalid-syntax
│ ├── code.js
│ └── error.js
└── simple-variable
├── code.js
└── output.js So the specifying the name of the file part is abstracted away. What I need in the matcher is just an API to determine if we're updating a snapshot instead to avoid accessing private properties. |
Can you be more specific about what you need? |
I'm cool with making that public if you want to submit the PR |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
ping |
We should stick links to https://github.com/americanexpress/jest-image-snapshot and https://github.com/satya164/jest-file-snapshot in https://jestjs.io/docs/snapshot-testing. PR is still welcome to make |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
It still would be cool to have this feature - bump for a stale bot |
The requested is somewhat similar to #12734. |
I think this is more involved than "just publicize Returning to the initial request independent of
( Additionally, to make this similarly ergonomic and pleasant to use as the existing snapshot matchers, I think it should also:
So here's a new proposal for how this could look. // file.test.js
it('should do something', () => {
// default case
// __tests__/__snapshots__/file.test.js/should_do_something_1.snap
expect(someValue).toMatchStandaloneSnapshot();
// a la existing snapshot helpers: allow passing property matchers to fail fast
// __tests__/__snapshots__/file.test.js/should_do_something_2.snap (2: second matcher in this test)
expect(someValue).toMatchStandaloneSnapshot({ field: expect.any(Date) });
// you can also specify the desired extension to assist other tools out of the box (e.g. syntax highlighters)
// __tests__/__snapshots__/file.test.js/should_do_something_3.snap.xml
expect(someValue).toMatchStandaloneSnapshot('xml');
// matchers, then extension
// __tests__/__snapshots__/file.test.js/should_do_something_4.snap.xml
expect(someValue).toMatchStandaloneSnapshot({ field: expect.any(Date) }, 'xml');
// you can also pass a filename hint
// __tests__/__snapshots__/file.test.js/should_do_something_just_checking_5.snap.xml
expect(someValue).toMatchStandaloneSnapshot('just checking', 'xml');
// or all three arguments
// __tests__/__snapshots__/file.test.js/should_do_something_just_checking_6.snap.xml
expect(someValue).toMatchStandaloneSnapshot({ field: expect.any(Date) }, 'just checking', 'xml');
// the numbering "namespace" is distinct between regular snapshots and standalone ones to avoid
// unnecessary file churn if you rearrange the matchers relative to each other
// __tests__/__snapshots__/file.test.js.snap > exports['should do something 1'] = ...;
expect(someValue).toMatchSnapshot();
}); This proposal introduces no new configuration, in particular, there is no separate configuration for where standalone snapshots go. My understanding is that you can configure the snapshot directory, and then Jest is assumed to own all the files in there. This simplifying assumption is important to make cleaning up snapshots for deleted matchers work. Jest can distinguish between the existing snapshots and the new standalone snapshots by how deep they are in the file tree: regular snapshots are in (The matcher's name is, of course, subject to bikeshed. I avoided "fixture" since I think that can be a number of things, and it isn't immediately clear what would distinguish a "fixture"-type snapshot from an unqualified snapshot. I avoided "file" because regular snapshots also end up in a file.) I poked around a preliminary implementation, but don't have anything to show yet. I'd like to get some opinions on this direction before I pursue this any farther. |
I think your ideas sounds great! Would love to see a PR exploring this. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
🚀 Feature Proposal
Allow each fixture/snapshot test to be written to separate file WITHOUT wrapping snapshot output into
exports
property.Motivation
It would be highly useful for projects like babel to treat their current fixture tests (actual.js / expected.js pairs) as Jest snapshots. This would allow integrating with jest more easily and using snapshot features for those tests (like
-u
)Example
API would have to be decided, this is just a one proposal:
Pitch
This with inline snapshot features would make a nice, complete package and cover most use cases.
The text was updated successfully, but these errors were encountered: