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

Fix test retries that contain snapshots #8019

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/expect/src/types.ts
Expand Up @@ -44,6 +44,7 @@ export type MatcherState = {
isExpectingAssertions?: boolean;
isNot: boolean;
promise: string;
snapshotState: any;
suppressedErrors: Array<Error>;
testPath?: Config.Path;
utils: typeof jestMatcherUtils & {
Expand Down
24 changes: 24 additions & 0 deletions packages/jest-circus/src/eventHandler.ts
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import fs from 'fs';

import {EventHandler, TEST_TIMEOUT_SYMBOL} from './types';

import {
Expand Down Expand Up @@ -147,7 +149,29 @@ const eventHandler: EventHandler = (event, state): void => {
break;
}
case 'test_retry': {
// Clear errors so tests can be retried (and not immediately fail)
event.test.errors = [];

// Clear any snapshot data that occurred in previous test run
const tmpSnapshotState = global.expect.getState().snapshotState;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense for global.expect to expose clearState, call it here and let it do the job of cleaning up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree. I should have some time this week to look at this some more.


if (tmpSnapshotState) {
if (
tmpSnapshotState._snapshotPath &&
Copy link
Contributor

@rogeliog rogeliog Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the freedom to start working on finishing this PR (but let me know if you prefer me not to), I've made some good progress but one thing that is still not clear is: Why do we need to call fs.unlinkSync(tmpSnapshotState._snapshotPath);?

cc: @thymikee @palmerj3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Please feel free to work on it :) It's all of our code not my code.

Ultimately the issue this PR tries to fix is the fact that a snapshot file for a test will contain n-copies depending on how many times it's rerun. So this was one way we could fix that by removing the snapshot file before new data is written.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I'll keep working on it and update the PR once I have something 😄

fs.existsSync(tmpSnapshotState._snapshotPath)
) {
fs.unlinkSync(tmpSnapshotState._snapshotPath);
}
global.expect.getState().snapshotState._snapshotData = {};
global.expect.getState().snapshotState._inlineSnapshots = [];
global.expect.getState().snapshotState._counters = new Map([]);
global.expect.getState().snapshotState._index = 0;
global.expect.getState().snapshotState.added = 0;
global.expect.getState().snapshotState.matched = 0;
global.expect.getState().snapshotState.unmatched = 0;
global.expect.getState().snapshotState.updated = 0;
}

break;
}
case 'run_start': {
Expand Down