Skip to content

Commit

Permalink
require jest-matchers in test env
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitrii Abramov committed Jul 9, 2016
1 parent 404635e commit f45bcb2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
11 changes: 2 additions & 9 deletions packages/jest-jasmine2/src/__tests__/matchers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,10 @@
'use strict';

jest.disableAutomock();
const jestExpect = require('jest-matchers').expect;

describe('matchers', () => {
it('proxies matchers to jest-matchers', () => {
// We can't use `expect().toThrow()` because `jest-matchers` error
// is thrown in the framework context, and hence `e instanceof Error` will
// return `false` and break jasmine
try {
expect(1).toBe(2);
throw new Error('should not be thrown');
} catch (e) {
expect(e.message).toMatch(/expected.*to be.*===.*/);
}
expect(() => expect(1).toBe(2)).toThrowError(/expected.*to be.*using '===/);
});
});
22 changes: 22 additions & 0 deletions packages/jest-jasmine2/src/extend_jasmine_expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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.
*/
'use strict';

jest.deepUnmock('jest-matchers');

const expect = require('jest-matchers').expect;

const jasmineExpect = global.expect;

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

const JasmineReporter = require('./reporter');

const expect = require('jest-matchers').expect;
const fs = require('graceful-fs');
const jasminePit = require('./jasmine-pit');
const snapshot = require('jest-snapshot');
const vm = require('vm');
const path = require('path');

const CALL_PRINT_LIMIT = 3;
const LAST_CALL_PRINT_LIMIT = 1;
Expand Down Expand Up @@ -293,18 +293,15 @@ function jasmine2(
}
});

// extend jasmine matchers with `jest-matchers`
const jasmineExpect = env.expect;
env.expect = actual => {
const jasmineMatchers = jasmineExpect(actual);
const jestMatchers = expect(actual);
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
return Object.assign(jasmineMatchers, jestMatchers, {not});
};

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.requireModule(path.resolve(__dirname, './extend_jasmine_expect.js'));

runtime.requireModule(testPath);
env.execute();
return reporter.getResults().then(results => {
Expand Down

0 comments on commit f45bcb2

Please sign in to comment.