Skip to content

Commit

Permalink
Simplify implementation and decouple it from expect
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeliog committed Jul 3, 2019
1 parent 8f103bd commit d1fe537
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
2 changes: 0 additions & 2 deletions packages/expect/src/index.ts
Expand Up @@ -43,7 +43,6 @@ import {
INTERNAL_MATCHER_FLAG,
getState,
setState,
clearState,
getMatchers,
setMatchers,
} from './jestMatchersObject';
Expand Down Expand Up @@ -403,7 +402,6 @@ expect.assertions = assertions;
expect.hasAssertions = hasAssertions;
expect.getState = getState;
expect.setState = setState;
expect.clearState = clearState;
expect.extractExpectedAssertionsErrors = extractExpectedAssertionsErrors;

const expectExport = expect as Expect;
Expand Down
4 changes: 0 additions & 4 deletions packages/expect/src/jestMatchersObject.ts
Expand Up @@ -37,10 +37,6 @@ export const setState = (state: object) => {
Object.assign((global as any)[JEST_MATCHERS_OBJECT].state, state);
};

export const clearState = () => {
getState().snapshotState.clear();
};

export const getMatchers = () => (global as any)[JEST_MATCHERS_OBJECT].matchers;

export const setMatchers = (
Expand Down
2 changes: 0 additions & 2 deletions packages/expect/src/types.ts
Expand Up @@ -6,7 +6,6 @@
*
*/
import {Config} from '@jest/types';
import {SnapshotStateType} from 'jest-snapshot';
import * as jestMatcherUtils from 'jest-matcher-utils';
import {INTERNAL_MATCHER_FLAG} from './jestMatchersObject';

Expand Down Expand Up @@ -45,7 +44,6 @@ export type MatcherState = {
isExpectingAssertions?: boolean;
isNot: boolean;
promise: string;
snapshotState: SnapshotStateType;
suppressedErrors: Array<Error>;
testPath?: Config.Path;
utils: typeof jestMatcherUtils & {
Expand Down
4 changes: 0 additions & 4 deletions packages/jest-circus/src/eventHandler.ts
Expand Up @@ -148,11 +148,7 @@ const eventHandler: Circus.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
global.expect.clearState();

break;
}
case 'run_start': {
Expand Down
Expand Up @@ -131,6 +131,8 @@ export const initialize = ({
});
setState({snapshotState, testPath});

addEventHandler(handleSnapshotStateAfterRetry(snapshotState));

// Return it back to the outer scope (test runner outside the VM).
return {globals, snapshotState};
};
Expand Down Expand Up @@ -243,6 +245,17 @@ export const runAndTransformResultsToJestFormat = async ({
};
};

const handleSnapshotStateAfterRetry = (snapshotState: SnapshotState) => (
event: Circus.Event,
) => {
switch (event.name) {
case 'test_retry': {
// Clear any snapshot data that occurred in previous test run
snapshotState.clear();
}
}
};

const eventHandler = (event: Circus.Event) => {
switch (event.name) {
case 'test_start': {
Expand Down
24 changes: 10 additions & 14 deletions packages/jest-snapshot/src/State.ts
Expand Up @@ -42,12 +42,12 @@ export default class SnapshotState {
private _index: number;
private _updateSnapshot: Config.SnapshotUpdateState;
private _snapshotData: SnapshotData;
private _initialData: SnapshotData;
private _snapshotPath: Config.Path;
private _inlineSnapshots: Array<InlineSnapshot>;
private _uncheckedKeys: Set<string>;
private _getBabelTraverse: () => Function;
private _getPrettier: () => null | any;
private _reinitializeData: () => void;

added: number;
expand: boolean;
Expand All @@ -61,7 +61,7 @@ export default class SnapshotState {
this._snapshotPath,
options.updateSnapshot,
);

this._initialData = data;
this._snapshotData = data;
this._dirty = dirty;
this._getBabelTraverse = options.getBabelTraverse;
Expand All @@ -76,17 +76,6 @@ export default class SnapshotState {
this.unmatched = 0;
this._updateSnapshot = options.updateSnapshot;
this.updated = 0;

this._reinitializeData = () => {
this._snapshotData = data;
this._inlineSnapshots = [];
this._counters = new Map();
this._index = 0;
this.added = 0;
this.matched = 0;
this.unmatched = 0;
this.updated = 0;
};
}

markSnapshotsAsCheckedForTest(testName: string) {
Expand Down Expand Up @@ -122,7 +111,14 @@ export default class SnapshotState {
}

clear() {
this._reinitializeData();
this._snapshotData = this._initialData;
this._inlineSnapshots = [];
this._counters = new Map();
this._index = 0;
this.added = 0;
this.matched = 0;
this.unmatched = 0;
this.updated = 0;
}

save() {
Expand Down

0 comments on commit d1fe537

Please sign in to comment.