Skip to content

Commit

Permalink
Fix babel-plugin-jest-hoist (jestjs#1754)
Browse files Browse the repository at this point in the history
* Move toMatchSnapshot to jest-matchers package

* Remove jasmine dependency from jest-snapshot

* Fixes for babel-plugin-jest-hoist.
  • Loading branch information
cpojer authored and mthmulders committed Sep 26, 2016
1 parent 95eb927 commit efca67b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (process.platform !== 'win32') {
}

it('sucessfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
const {json} = runJest.json(DIR, ['--no-cache']);
const {json} = runJest.json(DIR, ['--no-cache', '--coverage']);
expect(json.success).toBe(true);
expect(json.numTotalTestSuites).toBe(2);
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import e from '../__test_modules__/e';

// These will all be hoisted above imports
jest.unmock('react');
jest.unmock('../__test_modules__/Unmocked');
jest.deepUnmock('../__test_modules__/Unmocked');
jest
.unmock('../__test_modules__/c')
.unmock('../__test_modules__/d');
Expand Down Expand Up @@ -111,4 +111,10 @@ describe('babel-plugin-jest-hoist', () => {
expect(b._isMockFunction).toBe(true);
expect(b()).toEqual(undefined);
});

it('requires modules that also call jest.mock', () => {
require('../mock-file');
const mock = require('../banana');
expect(mock).toEqual('apple');
});
});
9 changes: 9 additions & 0 deletions integration_tests/babel-plugin-jest-hoist/banana.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 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.
*/

module.exports = 'banana';
12 changes: 12 additions & 0 deletions integration_tests/babel-plugin-jest-hoist/mock-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.
*/

jest.mock('./banana', () => {
const exports = 'apple';
return exports;
});
5 changes: 4 additions & 1 deletion packages/babel-plugin-jest-hoist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ FUNCTIONS.mock = args => {
if (!found) {
invariant(
(scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) ||
/^mock/.test(name),
/^mock/.test(name) ||
// Allow istanbul's coverage variable to pass.
//^(?:__)?cov/.test(name),
'The module factory of `jest.mock()` is not allowed to ' +
'reference any out-of-scope variables.\n' +
'Invalid variable access: ' + name + '\n' +
Expand All @@ -125,6 +127,7 @@ FUNCTIONS.mock = args => {
};

FUNCTIONS.unmock = args => args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.deepUnmock = args => args.length === 1 && args[0].isStringLiteral();

FUNCTIONS.disableAutomock =
FUNCTIONS.enableAutomock =
Expand Down

0 comments on commit efca67b

Please sign in to comment.