Skip to content

Commit 52c5c92

Browse files
authored
Fix babel-plugin-jest-hoist (jestjs#1754)
* Move toMatchSnapshot to jest-matchers package * Remove jasmine dependency from jest-snapshot * Fixes for babel-plugin-jest-hoist.
1 parent 498a843 commit 52c5c92

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

integration_tests/__tests__/babel-plugin-jest-hoist-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if (process.platform !== 'win32') {
2727
}
2828

2929
it('sucessfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
30-
const {json} = runJest.json(DIR, ['--no-cache']);
30+
const {json} = runJest.json(DIR, ['--no-cache', '--coverage']);
3131
expect(json.success).toBe(true);
3232
expect(json.numTotalTestSuites).toBe(2);
3333
});

integration_tests/babel-plugin-jest-hoist/__tests__/integration-test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import e from '../__test_modules__/e';
2323

2424
// These will all be hoisted above imports
2525
jest.unmock('react');
26-
jest.unmock('../__test_modules__/Unmocked');
26+
jest.deepUnmock('../__test_modules__/Unmocked');
2727
jest
2828
.unmock('../__test_modules__/c')
2929
.unmock('../__test_modules__/d');
@@ -111,4 +111,10 @@ describe('babel-plugin-jest-hoist', () => {
111111
expect(b._isMockFunction).toBe(true);
112112
expect(b()).toEqual(undefined);
113113
});
114+
115+
it('requires modules that also call jest.mock', () => {
116+
require('../mock-file');
117+
const mock = require('../banana');
118+
expect(mock).toEqual('apple');
119+
});
114120
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
9+
module.exports = 'banana';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
9+
jest.mock('./banana', () => {
10+
const exports = 'apple';
11+
return exports;
12+
});

packages/babel-plugin-jest-hoist/src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ FUNCTIONS.mock = args => {
106106
if (!found) {
107107
invariant(
108108
(scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) ||
109-
/^mock/.test(name),
109+
/^mock/.test(name) ||
110+
// Allow istanbul's coverage variable to pass.
111+
//^(?:__)?cov/.test(name),
110112
'The module factory of `jest.mock()` is not allowed to ' +
111113
'reference any out-of-scope variables.\n' +
112114
'Invalid variable access: ' + name + '\n' +
@@ -125,6 +127,7 @@ FUNCTIONS.mock = args => {
125127
};
126128

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

129132
FUNCTIONS.disableAutomock =
130133
FUNCTIONS.enableAutomock =

0 commit comments

Comments
 (0)