Skip to content
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

Snapshots sorted alphabetically instead of the order generated in the test #3936

Closed
joshclowater opened this issue Jun 29, 2017 · 9 comments
Closed

Comments

@joshclowater
Copy link

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
Snapshots are listed in the .snap file alphabetically.

What is the expected behavior?
Snapshots are listed in the .snap file the same order they are created in the test, so it easy to line them up when tests have many snapshots. Maybe the sorting method could be defined as a config option.

@joshclowater joshclowater changed the title Snapshots sorted alphabetically instead of the order in the test Snapshots sorted alphabetically instead of the order generated in the test Jun 29, 2017
@josh-stevens
Copy link

Agreed - see comment #3581 (comment)

@cpojer
Copy link
Member

cpojer commented Jun 29, 2017

We won't be changing this. You can pass a name to toMatchSnapshot to label snapshots the way you want to.

@cpojer cpojer closed this as completed Jun 29, 2017
@joshclowater
Copy link
Author

@cpojer Why would you not consider changing this? Even if we pass in a label, we'll have to duplicate all of the strings in the describe blocks and then prepend every label with 001, 002, etc. just to get it to sort properly.

@josh-stevens
Copy link

@cpojer The issue isn't the labels of the snapshots, I actually like the labels being the same as the describe/it blocks.

@cpojer
Copy link
Member

cpojer commented Jun 29, 2017

We aren't going to make this breaking change at this point and we won't be making this configurable. Sorting the snapshots alphabetically makes them stable, for example when tests are disabled. There is an escape hatch to provide the name of the snapshot and you can build your own matcher on top of this to make handling it easier.

@josh-stevens
Copy link

@cpojer I'm sorry but I don't understand how this would be a breaking change to make it configurable (since it could just default to its current behavior). I also am not sure what you mean when you say alphabetical sorting makes the snapshots stable.

Regardless of that, can you please provide more info on this escape hatch and how one could go about handling what we're asking for?

@joshclowater
Copy link
Author

@cpojer By disabled tests, do you mean ones resulting obsolete snapshots? Could those just be sorted to the bottom in this case?

@echenley
Copy link

echenley commented Jan 27, 2020

In case anyone else really wants their snapshots in order, you can shim it:

// `i` is reset for each test suite, though you could conceivably
// add something like `it.reset()`
let i = 1;

function orderedIt(description, cb) {
  return it(`${i++}. ${description}`, cb);
};

for (const [key, value] of Object.entries(it)) {
  orderedIt[key] = value;
}

export default orderedIt;
// test.js
orderedIt('snapshots one thing first', () => {
  expect(true).toMatchSnapshot();
});

orderedIt('snapshots another thing second', () => {
  expect(false).toMatchSnapshot();
});

// test.js.snap
exports[`1. snapshots one thing first 1`] = `true`;

exports[`2. snapshots another thing second 1`] = `false`;

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants