Jest: Snapshot summaries can be made even more delightful

Created on 16 May 2017  路  6Comments  路  Source: facebook/jest

After performing a test run jest always shows a Snapshot Summary as follows:

screen shot 2017-05-16 at 13 45 17

It would be really nice to have some more information about which test created the snapshot. The same goes for obsolete snapshots, it simply shows "1 obsolete snapshot found", I think it is really useful to have some more information such as the name of the obsolete snapshot for example to prevent users from accidentally deleting snapshots during refactoring for example.

I think that adding some more information to the summary can make the snapshot experience even more awesome!

What do you guys think?

If you feel that this feature is a worthy addition to Jest then I would be happy to work on it:)

Most helpful comment

I am using snapshots to test complex objects and arrays, not only react components.
When I run tests for the first time and I always must find them in the snap file and verify.
It would be great if the console output can provide more information.
For example: mapping between the line number and the test name.

btw
There is also another problem. All snapshots are always sorted alphabetically in the snap file. Is it possible to match the same order from the test.js file?

All 6 comments

I am using snapshots to test complex objects and arrays, not only react components.
When I run tests for the first time and I always must find them in the snap file and verify.
It would be great if the console output can provide more information.
For example: mapping between the line number and the test name.

btw
There is also another problem. All snapshots are always sorted alphabetically in the snap file. Is it possible to match the same order from the test.js file?

I agree that this could be improved, for large snapshot files I also find myself searching for the specific snapshot. If you have some ideas that you would like to contribute here then I encourage you to create a PR!

I created a PR #3660 to try and solve the issue I highlighted, would be great to hear your feedback

Is it possible to add an option to disable creating new snapshots?

I started developing in this way:

  1. Create a dummy snapshot.
describe('sample', () => {
  it('foo', () => {
    expect('').toMatchSnapshot();
  });
});
  1. It will create a new snapshot with empty string.
  2. Implement code
  3. Update the test
describe('sample', () => {
  it('foo', () => {
    const component = shallow(
      <Foo />
    );
    expect(something).toMatchSnapshot();
  });
});

  1. Check if the test diff looks good.
  2. Press u.

When using this approach I don't need to find the code in snapshot files, but I think 1. step could be omitted.
There is already --ci flag that doesn't create new snapshots, but it doesn't output the result.

See #3693

+1 for configuring the order that snapshot files are generated. When I test react saga's, it would be very nice to be able to read the snapshot in the order the steps occur rather than alphabetical. The way it works now, it groups all of my dispatch tests together, all of my selector tests together, etc. because of alphabetical sorting "it should dispatch", "it should select"...

It would be nice to have it sort in the same order as the test file, so I could put the snapshot file side by side with the test file and make sure everything is coming out properly.

Will be in Jest 21

Was this page helpful?
0 / 5 - 0 ratings