Skip to content

Commit 313fb53

Browse files
committed
Simplify implementation and decouple it from expect
1 parent 8f103bd commit 313fb53

File tree

6 files changed

+24
-26
lines changed

6 files changed

+24
-26
lines changed

packages/expect/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {
4343
INTERNAL_MATCHER_FLAG,
4444
getState,
4545
setState,
46-
clearState,
4746
getMatchers,
4847
setMatchers,
4948
} from './jestMatchersObject';
@@ -403,7 +402,6 @@ expect.assertions = assertions;
403402
expect.hasAssertions = hasAssertions;
404403
expect.getState = getState;
405404
expect.setState = setState;
406-
expect.clearState = clearState;
407405
expect.extractExpectedAssertionsErrors = extractExpectedAssertionsErrors;
408406

409407
const expectExport = expect as Expect;

packages/expect/src/jestMatchersObject.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ export const setState = (state: object) => {
3737
Object.assign((global as any)[JEST_MATCHERS_OBJECT].state, state);
3838
};
3939

40-
export const clearState = () => {
41-
getState().snapshotState.clear();
42-
};
43-
4440
export const getMatchers = () => (global as any)[JEST_MATCHERS_OBJECT].matchers;
4541

4642
export const setMatchers = (

packages/expect/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*
77
*/
88
import {Config} from '@jest/types';
9-
import {SnapshotStateType} from 'jest-snapshot';
109
import * as jestMatcherUtils from 'jest-matcher-utils';
1110
import {INTERNAL_MATCHER_FLAG} from './jestMatchersObject';
1211

@@ -45,7 +44,6 @@ export type MatcherState = {
4544
isExpectingAssertions?: boolean;
4645
isNot: boolean;
4746
promise: string;
48-
snapshotState: SnapshotStateType;
4947
suppressedErrors: Array<Error>;
5048
testPath?: Config.Path;
5149
utils: typeof jestMatcherUtils & {

packages/jest-circus/src/eventHandler.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ const eventHandler: Circus.EventHandler = (event, state): void => {
148148
break;
149149
}
150150
case 'test_retry': {
151-
// Clear errors so tests can be retried (and not immediately fail)
152151
event.test.errors = [];
153-
// Clear any snapshot data that occurred in previous test run
154-
global.expect.clearState();
155-
156152
break;
157153
}
158154
case 'run_start': {

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
1212
import {formatExecError, formatResultsErrors} from 'jest-message-util';
1313
import {
1414
SnapshotState,
15+
SnapshotStateType,
1516
addSerializer,
1617
buildSnapshotResolver,
1718
} from 'jest-snapshot';
@@ -131,6 +132,8 @@ export const initialize = ({
131132
});
132133
setState({snapshotState, testPath});
133134

135+
addEventHandler(handleSnapshotStateAfterRetry(snapshotState));
136+
134137
// Return it back to the outer scope (test runner outside the VM).
135138
return {globals, snapshotState};
136139
};
@@ -243,6 +246,17 @@ export const runAndTransformResultsToJestFormat = async ({
243246
};
244247
};
245248

249+
const handleSnapshotStateAfterRetry = (snapshotState: SnapshotStateType) => (
250+
event: Circus.Event,
251+
) => {
252+
switch (event.name) {
253+
case 'test_retry': {
254+
// Clear any snapshot data that occurred in previous test run
255+
snapshotState.clear();
256+
}
257+
}
258+
};
259+
246260
const eventHandler = (event: Circus.Event) => {
247261
switch (event.name) {
248262
case 'test_start': {

packages/jest-snapshot/src/State.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export default class SnapshotState {
4242
private _index: number;
4343
private _updateSnapshot: Config.SnapshotUpdateState;
4444
private _snapshotData: SnapshotData;
45+
private _initialData: SnapshotData;
4546
private _snapshotPath: Config.Path;
4647
private _inlineSnapshots: Array<InlineSnapshot>;
4748
private _uncheckedKeys: Set<string>;
4849
private _getBabelTraverse: () => Function;
4950
private _getPrettier: () => null | any;
50-
private _reinitializeData: () => void;
5151

5252
added: number;
5353
expand: boolean;
@@ -61,7 +61,7 @@ export default class SnapshotState {
6161
this._snapshotPath,
6262
options.updateSnapshot,
6363
);
64-
64+
this._initialData = data;
6565
this._snapshotData = data;
6666
this._dirty = dirty;
6767
this._getBabelTraverse = options.getBabelTraverse;
@@ -76,17 +76,6 @@ export default class SnapshotState {
7676
this.unmatched = 0;
7777
this._updateSnapshot = options.updateSnapshot;
7878
this.updated = 0;
79-
80-
this._reinitializeData = () => {
81-
this._snapshotData = data;
82-
this._inlineSnapshots = [];
83-
this._counters = new Map();
84-
this._index = 0;
85-
this.added = 0;
86-
this.matched = 0;
87-
this.unmatched = 0;
88-
this.updated = 0;
89-
};
9079
}
9180

9281
markSnapshotsAsCheckedForTest(testName: string) {
@@ -122,7 +111,14 @@ export default class SnapshotState {
122111
}
123112

124113
clear() {
125-
this._reinitializeData();
114+
this._snapshotData = this._initialData;
115+
this._inlineSnapshots = [];
116+
this._counters = new Map();
117+
this._index = 0;
118+
this.added = 0;
119+
this.matched = 0;
120+
this.unmatched = 0;
121+
this.updated = 0;
126122
}
127123

128124
save() {

0 commit comments

Comments
 (0)