-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathhoist-jest.spec.ts
More file actions
90 lines (86 loc) · 2.73 KB
/
hoist-jest.spec.ts
File metadata and controls
90 lines (86 loc) · 2.73 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { testing } from 'bs-logger'
import * as tsc from 'typescript'
import * as hoist from './hoist-jest'
const CODE_WITH_HOISTING = `
const foo = 'foo'
console.log(foo)
jest.enableAutomock()
jest.disableAutomock()
jest.mock('./foo')
jest.mock('./foo/bar', () => 'bar')
jest.unmock('./bar/foo').dontMock('./bar/bar')
jest.deepUnmock('./foo')
jest.mock('./foo').mock('./bar')
const func = () => {
const bar = 'bar'
console.log(bar)
jest.unmock('./foo')
jest.mock('./bar')
jest.mock('./bar/foo', () => 'foo')
jest.unmock('./foo/bar')
jest.unmock('./bar/foo').dontMock('./bar/bar')
jest.deepUnmock('./bar')
jest.mock('./foo').mock('./bar')
}
const func2 = () => {
const bar = 'bar'
console.log(bar)
jest.mock('./bar')
jest.unmock('./foo/bar')
jest.mock('./bar/foo', () => 'foo')
jest.unmock('./foo')
jest.unmock('./bar/foo').dontMock('./bar/bar')
jest.deepUnmock('./bar')
jest.mock('./foo').mock('./bar')
}
`
const logger = testing.createLoggerMock()
const createFactory = () => {
return hoist.factory({ logger, compilerModule: tsc } as any)
}
const transpile = (source: string) => tsc.transpileModule(source, { transformers: { before: [createFactory()] } })
describe('hoisting', () => {
it('should have correct signature', () => {
expect(hoist.name).toBe('hoisting-jest-mock')
expect(typeof hoist.version).toBe('number')
expect(hoist.version).toBeGreaterThan(0)
expect(typeof hoist.factory).toBe('function')
})
it('should hoist jest.mock(), unmock(), disableAutomock() and enableAutomock()', () => {
const out = transpile(CODE_WITH_HOISTING)
expect(out.outputText).toMatchInlineSnapshot(`
"jest.enableAutomock();
jest.disableAutomock();
jest.mock('./foo');
jest.mock('./foo/bar', function () { return 'bar'; });
jest.deepUnmock('./foo');
jest.mock('./foo').mock('./bar');
var foo = 'foo';
console.log(foo);
jest.unmock('./bar/foo').dontMock('./bar/bar');
var func = function () {
jest.unmock('./foo');
jest.mock('./bar');
jest.mock('./bar/foo', function () { return 'foo'; });
jest.unmock('./foo/bar');
jest.deepUnmock('./bar');
jest.mock('./foo').mock('./bar');
var bar = 'bar';
console.log(bar);
jest.unmock('./bar/foo').dontMock('./bar/bar');
};
var func2 = function () {
jest.mock('./bar');
jest.unmock('./foo/bar');
jest.mock('./bar/foo', function () { return 'foo'; });
jest.unmock('./foo');
jest.deepUnmock('./bar');
jest.mock('./foo').mock('./bar');
var bar = 'bar';
console.log(bar);
jest.unmock('./bar/foo').dontMock('./bar/bar');
};
"
`)
})
})