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(transformers): use ts factory API for hoisting #3058

Merged
merged 1 commit into from
Nov 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions e2e/__tests__/hoist-jest.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
import * as path from 'path'
import path from 'path'

import execa from 'execa'

import { json as runWithJson } from '../run-jest'
import { runNpmInstall } from '../utils'

const DIR = path.resolve(__dirname, '..', 'hoist-jest')
const { createBundle } = require('../../scripts/lib/bundle')

beforeEach(() => {
runNpmInstall(DIR)
})
const executeTest = (dirName: string): void => {
test(`successfully runs the tests with isolatedModules: false`, () => {
const { json } = runWithJson(dirName)

expect(json.success).toBe(true)
})

test(`successfully runs the tests with isolatedModules true`, () => {
const { json } = runWithJson(dirName, ['-c=jest-isolated.config.js'])

test('successfully runs the tests inside `hoist-jest/` with isolatedModules: false', () => {
const { json } = runWithJson(DIR)
expect(json.success).toBe(true)
})
}

expect(json.success).toBe(true)
describe('non-ts-factory', () => {
const DIR = path.resolve(__dirname, '..', 'hoist-jest', 'non-ts-factory')

beforeAll(() => {
runNpmInstall(DIR)
const bundle = createBundle()
execa.sync('npm', ['install', '--no-package-lock', '--no-shrinkwrap', '--no-save', bundle], {
cwd: DIR,
})
})

executeTest(DIR)
})

test('successfully runs the tests inside `hoist-jest/` with isolatedModules true', () => {
const { json } = runWithJson(DIR, ['-c=jest-isolated.config.js'])
describe('ts-factory', () => {
const DIR = path.resolve(__dirname, '..', 'hoist-jest', 'ts-factory')

beforeAll(() => {
runNpmInstall(DIR)
})

expect(json.success).toBe(true)
executeTest(DIR)
})
12 changes: 5 additions & 7 deletions e2e/hoist-jest/__tests__/import-jest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ aliasedJest.unmock('../__test_modules__/b')
JestGlobals.jest.unmock('../__test_modules__/c')

// These will not be hoisted above imports
// TODO: This is a bug to fix
// {
// const jest = {unmock: () => {}};
// jest.unmock('../__test_modules__/d');
// }
{
const jest = { unmock: () => {} }
jest.unmock('../__test_modules__/d')
}

// tests

Expand All @@ -36,8 +35,7 @@ test('namespace import', () => {
expect(c()).toBe('unmocked')
})

// TODO: Enable this once the TODO above is fixed
test.skip('fake jest, shadowed import', () => {
test('fake jest, shadowed import', () => {
expect(d._isMockFunction).toBe(true)
expect(d()).toBe(undefined)
})
23 changes: 10 additions & 13 deletions e2e/hoist-jest/__tests__/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react'
import Mocked from '../__test_modules__/mocked'
import Unmocked from '../__test_modules__/unmocked'
// TODO: Enable when the 2nd TODO is fixed
// import a from '../__test_modules__/a'
import a from '../__test_modules__/a'
import b from '../__test_modules__/b'
import c from '../__test_modules__/c'
import d from '../__test_modules__/d'
Expand Down Expand Up @@ -48,18 +47,17 @@ jest.mock('virtual-module', () => 'kiwi', { virtual: true })
jest.mock('has-flow-types', () => () => 3, {
virtual: true,
})
jest.mock('../__test_modules__/a')

// 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__/' + 'a')
jest.dontMock('../__test_modules__/mocked')
// TODO: This is a bug to fix
// {
// const jest = {unmock: () => {}};
// // Would error (used before initialization) if hoisted to the top of the scope
// jest.unmock('../__test_modules__/a');
// }

{
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 @@ -122,9 +120,8 @@ describe('hoisting', () => {
expect(Mocked._isMockFunction).toBe(true)
expect(new Mocked().isMocked).toEqual(undefined)

// TODO: Enable this once the TODO above is fixed
// expect(a._isMockFunction).toBe(true);
// expect(a()).toEqual(undefined);
expect(a._isMockFunction).toBe(true)
expect(a()).toEqual(undefined)

expect(b._isMockFunction).toBe(true)
expect(b()).toEqual(undefined)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module.exports = {
},
},
},
roots: ['<rootDir>', '<rootDir>/../__tests__'],
moduleNameMapper: {
react$: '<rootDir>/node_modules/react',
},
transform: {
'^.+.[tj]sx?$': '<rootDir>/../../dist/index.js',
'^.+.[tj]sx?$': 'ts-jest',
},
}
95 changes: 95 additions & 0 deletions e2e/hoist-jest/non-ts-factory/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions e2e/hoist-jest/non-ts-factory/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"dependencies": {
"react": "^17.0.2",
"typescript": "~3.8.3"
},
"jest": {
"automock": true,
"globals": {
"ts-jest": {
"diagnostics": false,
"tsconfig": {
"allowJs": true
}
}
},
"roots": ["<rootDir>", "<rootDir>/../__tests__"],
"moduleNameMapper": {
"react$": "<rootDir>/node_modules/react"
},
"transform": {
"^.+\\.[tj]sx?$": "ts-jest"
}
}
}
18 changes: 18 additions & 0 deletions e2e/hoist-jest/ts-factory/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
automock: true,
globals: {
'ts-jest': {
isolatedModules: true,
tsconfig: {
allowJs: true,
},
},
},
roots: ['<rootDir>', '<rootDir>/../__tests__'],
moduleNameMapper: {
react$: '<rootDir>/node_modules/react',
},
transform: {
'^.+.[tj]sx?$': '<rootDir>/../../../dist/index.js',
},
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
}
}
},
"roots": ["<rootDir>", "<rootDir>/../__tests__"],
"moduleNameMapper": {
"@jest/globals": "<rootDir>/../../node_modules/@jest/globals",
"@types/react": "<rootDir>/../../node_modules/@types/react"
"react$": "<rootDir>/node_modules/react"
},
"transform": {
"^.+\\.[tj]sx?$": "<rootDir>/../../dist/index.js"
"^.+\\.[tj]sx?$": "<rootDir>/../../../dist/index.js"
}
}
}
2 changes: 1 addition & 1 deletion e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const runNpmInstall = (cwd: Config.Path, env?: Record<string, string>): R
fs.writeFileSync(lockfilePath, '')
}

return run(exists ? 'npm ci' : 'npm i', cwd, env)
return run(exists ? 'npm ci --no-optional' : 'npm i --no-optional', cwd, env)
}

const replaceTime = (str: string): string =>
Expand Down
10 changes: 5 additions & 5 deletions src/config/__snapshots__/config-set.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 1,
"version": 2,
},
],
}
Expand All @@ -74,7 +74,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 1,
"version": 2,
},
Object {
"factory": [Function],
Expand All @@ -95,7 +95,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 1,
"version": 2,
},
],
}
Expand All @@ -113,7 +113,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 1,
"version": 2,
},
],
}
Expand All @@ -127,7 +127,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 1,
"version": 2,
},
Object {
"factory": [Function],
Expand Down
Loading