Skip to content

Commit 498a843

Browse files
authored
Merge pull request jestjs#1721 from dmitriiabramov/move-toMatchSnapshot-to-jest-matchers
Move to match snapshot to jest matchers
2 parents dfded00 + 9cef67b commit 498a843

File tree

11 files changed

+622
-481
lines changed

11 files changed

+622
-481
lines changed

integration_tests/snapshot/__tests__/snapshot-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('snapshot', () => {
3333

3434
it('cannot be used with .not', () => {
3535
expect(() => expect('').not.toMatchSnapshot()).toThrow(
36-
new Error('Jest: `.not` can not be used with `.toMatchSnapShot()`.')
36+
'Jest: `.not` can not be used with `.toMatchSnapshot()`.'
3737
);
3838
});
3939
});

packages/jest-jasmine2/src/extendJasmineExpect.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
'use strict';
99

1010
const jestExpect = require('jest-matchers').expect;
11+
const {addMatchers} = require('jest-matchers');
12+
const {toMatchSnapshot} = require('jest-snapshot');
1113

1214
const jasmineExpect = global.expect;
1315

1416
// extend jasmine matchers with `jest-matchers`
15-
global.expect = actual => {
16-
const jasmineMatchers = jasmineExpect(actual);
17-
const jestMatchers = jestExpect(actual);
18-
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
19-
return Object.assign(jasmineMatchers, jestMatchers, {not});
17+
module.exports = () => {
18+
addMatchers({toMatchSnapshot});
19+
global.expect = actual => {
20+
const jasmineMatchers = jasmineExpect(actual);
21+
const jestMatchers = jestExpect(actual);
22+
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
23+
return Object.assign(jasmineMatchers, jestMatchers, {not});
24+
};
2025
};

packages/jest-jasmine2/src/index.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import type Runtime from 'jest-runtime';
1717
const JasmineReporter = require('./reporter');
1818

1919
const jasmineAsync = require('./jasmine-async');
20-
const snapshot = require('jest-snapshot');
2120
const fs = require('graceful-fs');
2221
const path = require('path');
2322
const vm = require('vm');
@@ -101,14 +100,6 @@ function jasmine2(
101100
);
102101

103102
env.beforeEach(() => {
104-
jasmine.addMatchers({
105-
toMatchSnapshot: snapshot.matcher(
106-
testPath,
107-
config,
108-
snapshotState,
109-
),
110-
});
111-
112103
if (config.resetModules) {
113104
runtime.resetModules();
114105
}
@@ -118,16 +109,19 @@ function jasmine2(
118109
}
119110
});
120111

121-
const snapshotState = snapshot.getSnapshotState(jasmine, testPath);
122-
123112
env.addReporter(reporter);
124113

125114
// `jest-matchers` should be required inside test environment (vm).
126115
// Otherwise if they throw, the `Error` class will differ from the `Error`
127116
// class of the test and `error instanceof Error` will return `false`.
128117
runtime.requireInternalModule(
129118
path.resolve(__dirname, './extendJasmineExpect.js'),
130-
);
119+
)();
120+
121+
const snapshotState = runtime.requireInternalModule(
122+
path.resolve(__dirname, './setup-jest-globals.js'),
123+
)({testPath, config});
124+
131125

132126
if (config.setupTestFrameworkScriptFile) {
133127
runtime.requireModule(config.setupTestFrameworkScriptFile);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
'use strict';
12+
13+
import type {Config, Path} from 'types/Config';
14+
15+
const {getState, setState} = require('jest-matchers');
16+
const {initializeSnapshotState} = require('jest-snapshot');
17+
18+
// Get suppressed errors form jest-matchers that weren't throw during
19+
// test execution and add them to the test result, potentially failing
20+
// a passing test.
21+
const addSuppressedErrors = result => {
22+
const {suppressedErrors} = getState();
23+
setState({suppressedErrors: []});
24+
if (suppressedErrors.length) {
25+
result.status = 'failed';
26+
27+
result.failedExpectations = suppressedErrors.map(error => ({
28+
message: error.message,
29+
stack: error.stack,
30+
passed: false,
31+
expected: '',
32+
actual: '',
33+
}));
34+
}
35+
};
36+
37+
const patchJasmine = () => {
38+
global.jasmine.Spec = (realSpec => {
39+
const Spec = function Spec(attr) {
40+
const resultCallback = attr.resultCallback;
41+
attr.resultCallback = function(result) {
42+
addSuppressedErrors(result);
43+
resultCallback.call(attr, result);
44+
};
45+
46+
const onStart = attr.onStart;
47+
attr.onStart = context => {
48+
setState({currentTestName: context.getFullName()});
49+
onStart && onStart.call(attr, context);
50+
};
51+
52+
realSpec.call(this, attr);
53+
};
54+
55+
Spec.prototype = realSpec.prototype;
56+
for (const statics in realSpec) {
57+
if (Object.prototype.hasOwnProperty.call(realSpec, statics)) {
58+
Spec[statics] = realSpec[statics];
59+
}
60+
}
61+
return Spec;
62+
63+
})(global.jasmine.Spec);
64+
};
65+
66+
type Options = {
67+
testPath: Path,
68+
config: Config,
69+
};
70+
71+
module.exports = ({testPath, config}: Options) => {
72+
setState({testPath});
73+
patchJasmine();
74+
const snapshotState
75+
= initializeSnapshotState(testPath, config.updateSnapshot);
76+
setState({snapshotState});
77+
// Return it back to the outer scope (test runner outside the VM).
78+
return snapshotState;
79+
};

0 commit comments

Comments
 (0)