Skip to content

Commit 9cef67b

Browse files
committed
Remove jasmine dependency from jest-snapshot
1 parent 2abf128 commit 9cef67b

File tree

12 files changed

+613
-470
lines changed

12 files changed

+613
-470
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
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

14-
console.log(jasmine.Spec);
15-
1616
// extend jasmine matchers with `jest-matchers`
17-
module.exports = matchersContext => {
17+
module.exports = () => {
18+
addMatchers({toMatchSnapshot});
1819
global.expect = actual => {
1920
const jasmineMatchers = jasmineExpect(actual);
20-
const jestMatchers = jestExpect(actual, matchersContext);
21+
const jestMatchers = jestExpect(actual);
2122
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
2223
return Object.assign(jasmineMatchers, jestMatchers, {not});
2324
};

packages/jest-jasmine2/src/index.js

Lines changed: 6 additions & 4 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');
@@ -110,16 +109,19 @@ function jasmine2(
110109
}
111110
});
112111

113-
const snapshotState = snapshot.getSnapshotState(jasmine, testPath);
114-
115112
env.addReporter(reporter);
116113

117114
// `jest-matchers` should be required inside test environment (vm).
118115
// Otherwise if they throw, the `Error` class will differ from the `Error`
119116
// class of the test and `error instanceof Error` will return `false`.
120117
runtime.requireInternalModule(
121118
path.resolve(__dirname, './extendJasmineExpect.js'),
122-
)({snapshotState, updateSnapshot: config.updateSnapshot});
119+
)();
120+
121+
const snapshotState = runtime.requireInternalModule(
122+
path.resolve(__dirname, './setup-jest-globals.js'),
123+
)({testPath, config});
124+
123125

124126
if (config.setupTestFrameworkScriptFile) {
125127
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+
};

packages/jest-matchers/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"dependencies": {
1111
"jest-diff": "^15.1.0",
1212
"jest-matcher-utils": "^15.1.0",
13-
"jest-snapshot": "^15.1.1",
1413
"jest-util": "^15.1.1"
1514
},
1615
"devDependencies": {

0 commit comments

Comments
 (0)