Skip to content

Commit

Permalink
Merge pull request #1721 from dmitriiabramov/move-toMatchSnapshot-to-…
Browse files Browse the repository at this point in the history
…jest-matchers

Move to match snapshot to jest matchers
  • Loading branch information
aaronabramov authored Sep 20, 2016
2 parents c317daa + c8af10a commit 1ffad00
Show file tree
Hide file tree
Showing 11 changed files with 622 additions and 481 deletions.
2 changes: 1 addition & 1 deletion integration_tests/snapshot/__tests__/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('snapshot', () => {

it('cannot be used with .not', () => {
expect(() => expect('').not.toMatchSnapshot()).toThrow(
new Error('Jest: `.not` can not be used with `.toMatchSnapShot()`.')
'Jest: `.not` can not be used with `.toMatchSnapshot()`.'
);
});
});
15 changes: 10 additions & 5 deletions packages/jest-jasmine2/src/extendJasmineExpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
'use strict';

const jestExpect = require('jest-matchers').expect;
const {addMatchers} = require('jest-matchers');
const {toMatchSnapshot} = require('jest-snapshot');

const jasmineExpect = global.expect;

// extend jasmine matchers with `jest-matchers`
global.expect = actual => {
const jasmineMatchers = jasmineExpect(actual);
const jestMatchers = jestExpect(actual);
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
return Object.assign(jasmineMatchers, jestMatchers, {not});
module.exports = () => {
addMatchers({toMatchSnapshot});
global.expect = actual => {
const jasmineMatchers = jasmineExpect(actual);
const jestMatchers = jestExpect(actual);
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
return Object.assign(jasmineMatchers, jestMatchers, {not});
};
};
18 changes: 6 additions & 12 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type Runtime from 'jest-runtime';
const JasmineReporter = require('./reporter');

const jasmineAsync = require('./jasmine-async');
const snapshot = require('jest-snapshot');
const fs = require('graceful-fs');
const path = require('path');
const vm = require('vm');
Expand Down Expand Up @@ -101,14 +100,6 @@ function jasmine2(
);

env.beforeEach(() => {
jasmine.addMatchers({
toMatchSnapshot: snapshot.matcher(
testPath,
config,
snapshotState,
),
});

if (config.resetModules) {
runtime.resetModules();
}
Expand All @@ -118,16 +109,19 @@ function jasmine2(
}
});

const snapshotState = snapshot.getSnapshotState(jasmine, testPath);

env.addReporter(reporter);

// `jest-matchers` should be required inside test environment (vm).
// Otherwise if they throw, the `Error` class will differ from the `Error`
// class of the test and `error instanceof Error` will return `false`.
runtime.requireInternalModule(
path.resolve(__dirname, './extendJasmineExpect.js'),
);
)();

const snapshotState = runtime.requireInternalModule(
path.resolve(__dirname, './setup-jest-globals.js'),
)({testPath, config});


if (config.setupTestFrameworkScriptFile) {
runtime.requireModule(config.setupTestFrameworkScriptFile);
Expand Down
79 changes: 79 additions & 0 deletions packages/jest-jasmine2/src/setup-jest-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

'use strict';

import type {Config, Path} from 'types/Config';

const {getState, setState} = require('jest-matchers');
const {initializeSnapshotState} = require('jest-snapshot');

// Get suppressed errors form jest-matchers that weren't throw during
// test execution and add them to the test result, potentially failing
// a passing test.
const addSuppressedErrors = result => {
const {suppressedErrors} = getState();
setState({suppressedErrors: []});
if (suppressedErrors.length) {
result.status = 'failed';

result.failedExpectations = suppressedErrors.map(error => ({
message: error.message,
stack: error.stack,
passed: false,
expected: '',
actual: '',
}));
}
};

const patchJasmine = () => {
global.jasmine.Spec = (realSpec => {
const Spec = function Spec(attr) {
const resultCallback = attr.resultCallback;
attr.resultCallback = function(result) {
addSuppressedErrors(result);
resultCallback.call(attr, result);
};

const onStart = attr.onStart;
attr.onStart = context => {
setState({currentTestName: context.getFullName()});
onStart && onStart.call(attr, context);
};

realSpec.call(this, attr);
};

Spec.prototype = realSpec.prototype;
for (const statics in realSpec) {
if (Object.prototype.hasOwnProperty.call(realSpec, statics)) {
Spec[statics] = realSpec[statics];
}
}
return Spec;

})(global.jasmine.Spec);
};

type Options = {
testPath: Path,
config: Config,
};

module.exports = ({testPath, config}: Options) => {
setState({testPath});
patchJasmine();
const snapshotState
= initializeSnapshotState(testPath, config.updateSnapshot);
setState({snapshotState});
// Return it back to the outer scope (test runner outside the VM).
return snapshotState;
};
Loading

0 comments on commit 1ffad00

Please sign in to comment.