Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hoist imports of @jest/globals correctly #9806

Merged
merged 23 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ exports[`prints console.logs when run with forceExit 3`] = `
console.log
Hey

at Object.<anonymous> (__tests__/a-banana.js:1:1)
at Object.log (__tests__/a-banana.js:1:30)
jeysal marked this conversation as resolved.
Show resolved Hide resolved

`;
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exports[`on node >=10 prints coverage 1`] = `
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Thing.js | 100 | 100 | 100 | 100 |
All files | 100 | 100 | 0 | 100 |
Thing.js | 100 | 100 | 0 | 100 |
jeysal marked this conversation as resolved.
Show resolved Hide resolved
x.css | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------"
`;
4 changes: 2 additions & 2 deletions e2e/__tests__/babelPluginJestHoist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ beforeEach(() => {
run('yarn', DIR);
});

it('sucessfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
it('successfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
const {json} = runWithJson(DIR, ['--no-cache', '--coverage']);
expect(json.success).toBe(true);
expect(json.numTotalTestSuites).toBe(3);
expect(json.numTotalTestSuites).toBe(4);
});
2 changes: 1 addition & 1 deletion e2e/__tests__/resolveBrowserField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test('preserves module identity for symlinks when using browser field resolution
console.log
needs-preserved-id executed

at Object.<anonymous> (packages/needs-preserved-id/index.js:1:13)
at Object.log (packages/needs-preserved-id/index.js:1:9)
`);
expect(exitCode).toEqual(0);
});
53 changes: 53 additions & 0 deletions e2e/babel-plugin-jest-hoist/__tests__/importJest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

/* eslint-disable import/no-duplicates */
import {jest} from '@jest/globals';
jeysal marked this conversation as resolved.
Show resolved Hide resolved
import {jest as aliasedJest} from '@jest/globals';
import * as JestGlobals from '@jest/globals';
/* eslint-enable import/no-duplicates */

import a from '../__test_modules__/a';
import b from '../__test_modules__/b';
import c from '../__test_modules__/c';
import d from '../__test_modules__/d';

// These will be hoisted above imports

jest.unmock('../__test_modules__/a');
aliasedJest.unmock('../__test_modules__/b');
JestGlobals.jest.unmock('../__test_modules__/c');

// These will not be hoisted above imports

{
const jest = {unmock: () => {}};
jest.unmock('../__test_modules__/d');
}

// tests

test('named import', () => {
expect(a._isMockFunction).toBe(undefined);
expect(a()).toBe('unmocked');
});

test('aliased named import', () => {
expect(b._isMockFunction).toBe(undefined);
expect(b()).toBe('unmocked');
});

test('namespace import', () => {
expect(c._isMockFunction).toBe(undefined);
expect(c()).toBe('unmocked');
});

test('fake jest, shadowed import', () => {
expect(d._isMockFunction).toBe(true);
expect(d()).toBe(undefined);
});
31 changes: 24 additions & 7 deletions e2e/babel-plugin-jest-hoist/__tests__/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import a from '../__test_modules__/a';
import b from '../__test_modules__/b';
import c from '../__test_modules__/c';
import d from '../__test_modules__/d';
import e from '../__test_modules__/e';
import f from '../__test_modules__/f';
jeysal marked this conversation as resolved.
Show resolved Hide resolved
import jestBackticks from '../__test_modules__/jestBackticks';

// The virtual mock call below will be hoisted above this `require` call.
Expand All @@ -25,7 +25,16 @@ const virtualModule = require('virtual-module');
jest.unmock('react');
jest.deepUnmock('../__test_modules__/Unmocked');
jest.unmock('../__test_modules__/c').unmock('../__test_modules__/d');
jest.mock('../__test_modules__/e', () => {

let e;
(function () {
const _getJestObj = 42;
e = require('../__test_modules__/e').default;
// hoisted to the top of the function scope
jest.unmock('../__test_modules__/e');
})();

jest.mock('../__test_modules__/f', () => {
if (!global.CALLS) {
global.CALLS = 0;
}
Expand All @@ -52,8 +61,13 @@ jest.mock('has-flow-types', () => (props: {children: mixed}) => 3, {
// These will not be hoisted
jest.unmock('../__test_modules__/a').dontMock('../__test_modules__/b');
// eslint-disable-next-line no-useless-concat
jest.unmock('../__test_modules__/' + 'c');
jest.unmock('../__test_modules__/' + 'a');
jest.dontMock('../__test_modules__/Mocked');
{
const jest = {unmock: () => {}};
// Would error (used before initialization) if hoisted to the top of the scope
jest.unmock('../__test_modules__/a');
}

// This must not throw an error
const myObject = {mock: () => {}};
Expand Down Expand Up @@ -84,14 +98,17 @@ describe('babel-plugin-jest-hoist', () => {

expect(d._isMockFunction).toBe(undefined);
expect(d()).toEqual('unmocked');

expect(e._isMock).toBe(undefined);
expect(e()).toEqual('unmocked');
});

it('hoists mock call with 2 arguments', () => {
const path = require('path');

expect(e._isMock).toBe(true);
expect(f._isMock).toBe(true);

const mockFn = e.fn();
const mockFn = f.fn();
expect(mockFn()).toEqual([path.sep, undefined, undefined]);
});

Expand All @@ -100,10 +117,10 @@ describe('babel-plugin-jest-hoist', () => {

global.CALLS = 0;

require('../__test_modules__/e');
require('../__test_modules__/f');
expect(global.CALLS).toEqual(1);

require('../__test_modules__/e');
require('../__test_modules__/f');
expect(global.CALLS).toEqual(1);

delete global.CALLS;
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-jest-hoist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
}
},
"dependencies": {
"@babel/template": "^7.3.3",
jeysal marked this conversation as resolved.
Show resolved Hide resolved
"@babel/types": "^7.3.3",
"@types/babel__traverse": "^7.0.6"
},
"devDependencies": {
"@babel/types": "^7.3.3",
"@types/node": "*"
},
"publishConfig": {
Expand Down
Loading