-
Notifications
You must be signed in to change notification settings - Fork 449
/
general-hoisting.spec.ts
49 lines (40 loc) · 1.44 KB
/
general-hoisting.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)
});