/
general-hoisting.spec.ts
72 lines (58 loc) · 2.06 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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'
// The virtual mock call below will be hoisted above this `require` call.
const virtualModule = require('virtual-module')
// 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')
})()
jest.mock('virtual-module', () => 'kiwi', {virtual: true})
// 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('does not throw during transform', () => {
const object = {};
// @ts-expect-error
object.__defineGetter__('foo', () => 'bar');
// @ts-expect-error
expect(object.foo).toEqual('bar');
})
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).toBeUndefined()
expect(e()).toEqual('unmocked')
})
it('does not hoist dontMock calls before imports', () => {
// @ts-expect-error
expect(Mocked._isMockFunction).toBe(true)
expect(new Mocked().isMocked).toBeUndefined()
// @ts-expect-error
expect(b._isMockFunction).toBe(true)
expect(b()).toBeUndefined()
})
it('requires modules that also call jest.mock', () => {
require('./__test_modules__/mockFile')
const mock = require('./__test_modules__/banana')
expect(mock).toEqual('apple')
})
it('works with virtual modules', () => {
expect(virtualModule).toBe('kiwi')
})