Skip to content

Commit

Permalink
fix(transformer): don't hoist jest methods over import @jest/globals (
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Sep 9, 2020
1 parent f40b567 commit 21dd3b7
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 727 deletions.
7 changes: 7 additions & 0 deletions e2e/__cases__/hoisting/__test_modules__/Mocked.ts
@@ -0,0 +1,7 @@
export default class Mocked {
readonly isMocked: boolean

constructor() {
this.isMocked = true;
}
}
7 changes: 7 additions & 0 deletions e2e/__cases__/hoisting/__test_modules__/Unmocked.ts
@@ -0,0 +1,7 @@
export default class Unmocked {
readonly isUnmocked: boolean

constructor() {
this.isUnmocked = true;
}
}
1 change: 1 addition & 0 deletions e2e/__cases__/hoisting/__test_modules__/a.ts
@@ -0,0 +1 @@
export default () => 'unmocked';
1 change: 1 addition & 0 deletions e2e/__cases__/hoisting/__test_modules__/b.ts
@@ -0,0 +1 @@
export default () => 'unmocked';
1 change: 1 addition & 0 deletions e2e/__cases__/hoisting/__test_modules__/c.ts
@@ -0,0 +1 @@
export default () => 'unmocked';
1 change: 1 addition & 0 deletions e2e/__cases__/hoisting/__test_modules__/d.ts
@@ -0,0 +1 @@
export default () => 'unmocked';
1 change: 1 addition & 0 deletions e2e/__cases__/hoisting/__test_modules__/e.ts
@@ -0,0 +1 @@
export default () => 'unmocked';

This file was deleted.

3 changes: 0 additions & 3 deletions e2e/__cases__/hoisting/disable-automock/disable-automock.ts

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions e2e/__cases__/hoisting/enable-automock/enable-automock.ts

This file was deleted.

49 changes: 49 additions & 0 deletions e2e/__cases__/hoisting/general-hoisting.spec.ts
@@ -0,0 +1,49 @@
import Unmocked from './__test_modules__/Unmocked'
import Mocked from './__test_modules__/Mocked'
import b from './__test_modules__/b'
import c from './__test_modules__/c'
import d from './__test_modules__/d'

// These will all be hoisted above imports
jest.deepUnmock('./__test_modules__/Unmocked')
jest.unmock('./__test_modules__/c').unmock('./__test_modules__/d')

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

// These will not be hoisted
jest.unmock('./__test_modules__/a').dontMock('./__test_modules__/b')
jest.unmock('./__test_modules__/' + 'a')
jest.dontMock('./__test_modules__/Mocked')

it('hoists unmocked modules before imports', () => {
// @ts-expect-error
expect(Unmocked._isMockFunction).toBeUndefined()
expect(new Unmocked().isUnmocked).toEqual(true)

// @ts-expect-error
expect(c._isMockFunction).toBeUndefined()
expect(c()).toEqual('unmocked')

// @ts-expect-error
expect(d._isMockFunction).toBeUndefined()
expect(d()).toEqual('unmocked')

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

it('does not hoist dontMock calls before imports', () => {
// @ts-expect-error
expect(Mocked._isMockFunction).toBe(true)
expect(new Mocked().isMocked).toEqual(undefined)

// @ts-expect-error
expect(b._isMockFunction).toBe(true)
expect(b()).toEqual(undefined)
});
37 changes: 37 additions & 0 deletions e2e/__cases__/hoisting/import-jest.spec.ts
@@ -0,0 +1,37 @@
import {jest} from '@jest/globals'
import {jest as aliasedJest} from '@jest/globals'
import * as JestGlobals from '@jest/globals'

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

// These will be hoisted above imports
jest.unmock('./__test_modules__/a')
aliasedJest.unmock('./__test_modules__/b')
JestGlobals.jest.unmock('./__test_modules__/c')

// tests

test('named import', () => {
// @ts-expect-error
expect(a._isMockFunction).toBeUndefined()
expect(a()).toBe('unmocked')
});

test('aliased named import', () => {
// @ts-expect-error TODO: fix aliased named import
expect(b._isMockFunction).toBe(true)
expect(b()).toBeUndefined()
// expect(b._isMockFunction).toBe(undefined)
// expect(b()).toBe('unmocked')
});

test('namespace import', () => {
// @ts-expect-error TODO: fix namespace import
expect(c._isMockFunction).toBe(true)
expect(c()).toBeUndefined()
// expect(c._isMockFunction).toBe(undefined)
// expect(c()).toBe('unmocked')
});

13 changes: 0 additions & 13 deletions e2e/__cases__/hoisting/mock-unmock/mock-unmock.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions e2e/__cases__/hoisting/mock-unmock/mock-unmock.ts

This file was deleted.

2 changes: 1 addition & 1 deletion e2e/__helpers__/test-case/types.ts
Expand Up @@ -9,7 +9,7 @@ export interface RunTestOptions {
env?: Record<string, unknown>
inject?: (() => any) | string
writeIo?: boolean
jestConfig?: Config.ProjectConfig | any
jestConfig?: Config.ProjectConfig | Record<string, unknown>
tsJestConfig?: TsJestGlobalOptions
noCache?: boolean
jestConfigPath?: string
Expand Down
4 changes: 2 additions & 2 deletions e2e/__templates__/with-babel-7-string-config/babel.config.js
@@ -1,5 +1,5 @@
module.exports = {
presets: [
"@babel/preset-env"
]
'@babel/preset-env',
],
}
4 changes: 2 additions & 2 deletions e2e/__templates__/with-babel-7/babel.config.js
@@ -1,5 +1,5 @@
module.exports = {
presets: [
"@babel/preset-env"
]
'@babel/preset-env',
],
}

0 comments on commit 21dd3b7

Please sign in to comment.