-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Test after retry should check the same snapshot, not create a new one #5312
Comments
Thanks for raising an issue. Not sure if it's possible to support this but it looks like a weird bug. I made a reproduction on stackblitz with comments to explain the issue: import { test, expect } from 'vitest';
test('file', { retry: 1 }, () => {
//
// Initially
// expect('first').toMatchSnapshot();
///
// which will create snapshot file with:
// exports[`file 1`] = `"first"`;
//
// Changing it to "second" will create a following snapshot
// exports[`file 1`] = `"first"`;
// exports[`file 2`] = `"second"`;
//
// After that, running "u" (update snapshot) cannot remove the first one.
//
expect('second').toMatchSnapshot();
}); |
I found something similar in the jest repo: |
Interesting, maybe we can borrow some ideas from their PR jestjs/jest#8629. Thanks for the pointer! |
We need to rewrite how the snapshot client works to make this work. Since #4796 there is only a single snapshot state per file, so we cannot rollback changes depending on the test state (in jest, the state is per test, but it's also a singleton which doesn't support I think we need to have a map of states based on the test ID (or the combination of
Then, the global test state can aggregate all results from smaller states inside itself. |
Is there a way to make |
You can run Vitest with |
As I understand this flag will not prevent creating a new snapshot. For example, there can be a case when there is a function which got refactored and changed the name and if the function name is in the test then the old snapshot will get obsolete. In the CI the test will not fail because a new snapshot will be created. But there is a high possibility of the code being broken. |
Did you run Vitest with this flag before reporting this? |
Code: export function something() {
return 'something';
} Test File: import { describe, expect, it } from 'vitest';
import { something } from './something.ts';
describe('Lets test some snapshots', () => {
describe(`${something.name}`, () => {
it('should return something', () => {
expect(something()).toMatchSnapshot();
});
});
}); First Run:
Changed the code: export function somethingElse() {
return 'something' + 'else';
} Test file: import { describe, expect, it } from 'vitest';
import { somethingElse } from './something.ts';
describe('Lets test some snapshots', () => {
describe(`${somethingElse.name}`, () => {
it('should return something', () => {
expect(somethingElse()).toMatchSnapshot();
});
});
}); Test run:
Both run's snapshots didn't exist before the run. I know maybe putting the function name into the description is not the best idea but still. Snapshots are created all the time. |
|
I did check it here and it indeed doesn't work as expected: https://stackblitz.com/edit/vitest-dev-vitest-2kw17c?file=package.json&initialPath=__vitest__/ Vitest shouldn't create snapshot files in CI (update is false there by default) - this is a bug, it didn't work like that before. |
Thanks for verifying. Yeah this has to be fixed :( |
I also checked with |
@Rihyx Ok, I checked the implementation. |
All right, thanks a lot. I will create a separate issue for that. |
I'm experiencing the same issue. When I set the |
Describe the bug
If I add retry to the test and for some reason it didn't match the snapshot on the first loop, it shouldn't create a new snapshot and passing the test, but check the same snapshot again.
Reproduction
test.spec.js
test.spec.js.snap
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: