Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformer): don't hoist jest methods over import
@jest/globals
(
- Loading branch information
Showing
24 changed files
with
274 additions
and
727 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default class Mocked { | ||
readonly isMocked: boolean | ||
|
||
constructor() { | ||
this.isMocked = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default class Unmocked { | ||
readonly isUnmocked: boolean | ||
|
||
constructor() { | ||
this.isUnmocked = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => 'unmocked'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => 'unmocked'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => 'unmocked'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => 'unmocked'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => 'unmocked'; |
9 changes: 0 additions & 9 deletions
9
e2e/__cases__/hoisting/disable-automock/disable-automock.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
e2e/__cases__/hoisting/enable-automock/enable-automock.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}); | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
presets: [ | ||
"@babel/preset-env" | ||
] | ||
'@babel/preset-env', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
presets: [ | ||
"@babel/preset-env" | ||
] | ||
'@babel/preset-env', | ||
], | ||
} |
Oops, something went wrong.