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

refactor(transformer): correctly detect jest import name bindings #1955

Merged
merged 1 commit into from Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/__cases__/hoisting/import-jest.spec.ts
@@ -1,4 +1,4 @@
import {jest} from '@jest/globals'
import {test,jest} from '@jest/globals'
import {jest as aliasedJest} from '@jest/globals'
import * as JestGlobals from '@jest/globals'

Expand Down
4 changes: 2 additions & 2 deletions src/config/__snapshots__/config-set.spec.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"transformers\\":[\\"hoisting-jest-mock@3\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`;
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"transformers\\":[\\"hoisting-jest-mock@4\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`;

exports[`isTestFile should return a boolean value whether the file matches test pattern 1`] = `true`;

Expand All @@ -21,7 +21,7 @@ Object {
"name": undefined,
},
"transformers": Array [
"hoisting-jest-mock@3",
"hoisting-jest-mock@4",
],
"tsJest": Object {
"babelConfig": undefined,
Expand Down
3 changes: 3 additions & 0 deletions src/transformers/__snapshots__/hoist-jest.spec.ts.snap
Expand Up @@ -3,6 +3,7 @@
exports[`hoisting should hoist correctly jest methods 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
var globals_1 = require(\\"@jest/globals\\");
// These will all be hoisted above imports
jest.unmock('react');
jest.deepUnmock('../__test_modules__/Unmocked');
Expand Down Expand Up @@ -69,6 +70,7 @@ console.log(d_1.default);
console.log(e);
console.log(virtualModule);
console.log(jestBackticks_1.default);
console.log(globals_1.it);
"
`;

Expand All @@ -95,5 +97,6 @@ console.log(a_1.default);
console.log(b_1.default);
console.log(c_1.default);
console.log(d_1.default);
console.log(globals_1.it);
"
`;
5 changes: 4 additions & 1 deletion src/transformers/hoist-jest.spec.ts
Expand Up @@ -7,6 +7,7 @@ const CODE_WITH_HOISTING_NO_JEST_GLOBALS = `
import React from 'react'
import Unmocked from '../__test_modules__/Unmocked'
import Mocked from '../__test_modules__/Mocked'
import {it} from '@jest/globals'
import a from '../__test_modules__/a'
import b from '../__test_modules__/b'
import c from '../__test_modules__/c'
Expand Down Expand Up @@ -81,12 +82,13 @@ console.log(d)
console.log(e)
console.log(virtualModule)
console.log(jestBackticks)
console.log(it)
`
const CODE_WITH_HOISTING_HAS_JEST_GLOBALS = `
import a from '../__test_modules__/a'
import b from '../__test_modules__/b'

import {jest} from '@jest/globals'
import {it, jest} from '@jest/globals'
import {jest as aliasedJest} from '@jest/globals'
import * as JestGlobals from '@jest/globals'

Expand All @@ -110,6 +112,7 @@ const CODE_WITH_HOISTING_HAS_JEST_GLOBALS = `
console.log(b)
console.log(c)
console.log(d)
console.log(it)
`

const logger = testing.createLoggerMock()
Expand Down
13 changes: 9 additions & 4 deletions src/transformers/hoist-jest.ts
Expand Up @@ -29,7 +29,7 @@ export const name = 'hoisting-jest-mock'
*
* @internal
*/
export const version = 3
export const version = 4

/**
* The factory of hoisting transformer factory
Expand Down Expand Up @@ -147,9 +147,14 @@ export function factory(cs: ConfigSet): (ctx: TransformationContext) => Transfor
ts.isNamedImports(resultNode.importClause.namedBindings))
) {
const { namedBindings } = resultNode.importClause
importNames.push(
ts.isNamespaceImport(namedBindings) ? namedBindings.name.text : namedBindings.elements[0].name.text,
)
const jestImportName = ts.isNamespaceImport(namedBindings)
? namedBindings.name.text
: namedBindings.elements.find(
(element) => element.name.text === JEST_GLOBAL_NAME || element.propertyName?.text === JEST_GLOBAL_NAME,
)?.name.text
if (jestImportName) {
importNames.push(jestImportName)
}
}
// check if we have something to hoist in this level
if (hoisted[level] && hoisted[level].length) {
Expand Down