/
index.spec.ts
134 lines (126 loc) · 4.48 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// tslint:disable:max-line-length
import { testing } from 'bs-logger'
import * as tsJest from '.'
import { logTargetMock } from './__helpers__/mocks'
import { TsJestTransformer } from './ts-jest-transformer'
jest.mock('./ts-jest-transformer', () => {
class TsJestTransformer {
process: jest.Mock = jest.fn()
getCacheKey: jest.Mock = jest.fn()
constructor(public opt?: any) {}
}
return { TsJestTransformer }
})
jest.mock('./config/create-jest-preset', () => ({
createJestPreset: () => ({ jestPreset: true }),
}))
describe('ts-jest', () => {
it('should export a `createTransformer` function', () => {
expect(typeof tsJest.createTransformer).toBe('function')
})
it('should export a `process` function', () => {
expect(typeof tsJest.process).toBe('function')
})
it('should export a `getCacheKey` function', () => {
expect(typeof tsJest.getCacheKey).toBe('function')
})
it('should export a `createJestPreset` function', () => {
expect(typeof tsJest.createJestPreset).toBe('function')
})
it('should export a `mocked` function', () => {
expect(typeof tsJest.mocked).toBe('function')
})
it('should export a `pathsToModuleNameMapper` function', () => {
expect(typeof tsJest.pathsToModuleNameMapper).toBe('function')
})
it('should export a `jestPreset` object', () => {
expect(tsJest.jestPreset).toEqual({ jestPreset: true })
})
})
describe('old entry point', () => {
const MANIFEST = { tsJestIndex: true }
const spy = jest.spyOn(console, 'warn')
spy.mockImplementation(() => undefined)
afterAll(() => {
spy.mockRestore()
})
it('should warn when using old path to ts-jest', () => {
jest.mock('../dist/index', () => MANIFEST)
expect(require('../preprocessor.js')).toBe(MANIFEST)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"ts-jest[main] (WARN) Replace any occurrences of \\"ts-jest/dist/preprocessor.js\\" or \\"<rootDir>/node_modules/ts-jest/preprocessor.js\\" in the 'transform' section of your Jest config with just \\"ts-jest\\".",
]
`)
})
})
describe('moved helpers', () => {
let target: testing.LogTargetMock
beforeEach(() => {
target = logTargetMock()
target.clear()
})
it('should warn when using mocked', () => {
tsJest.mocked(42)
expect(target.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] The \`mocked\` helper has been moved to \`ts-jest/utils\`. Use \`import { mocked } from 'ts-jest/utils'\` instead.
",
]
`)
})
it('should warn when using createJestPreset', () => {
tsJest.createJestPreset()
expect(target.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] The \`createJestPreset\` helper has been moved to \`ts-jest/utils\`. Use \`import { createJestPreset } from 'ts-jest/utils'\` instead.
",
]
`)
})
it('should warn when using pathsToModuleNameMapper', () => {
tsJest.pathsToModuleNameMapper({})
expect(target.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] The \`pathsToModuleNameMapper\` helper has been moved to \`ts-jest/utils\`. Use \`import { pathsToModuleNameMapper } from 'ts-jest/utils'\` instead.
",
]
`)
})
})
describe('createTransformer', () => {
it('should create different instances', () => {
const tr1 = tsJest.createTransformer()
const tr2 = tsJest.createTransformer()
expect(tr1).toBeInstanceOf(TsJestTransformer)
expect(tr2).toBeInstanceOf(TsJestTransformer)
expect(tr1).not.toBe(tr2)
})
it('should accept base options', () => {
expect((tsJest.createTransformer({ isolatedModules: true }) as any).opt).toEqual({
isolatedModules: true,
})
})
})
describe('bare methods', () => {
beforeEach(() => {
tsJest.__resetModule()
})
it('should use the same instance of transformer for `process`', () => {
tsJest.process('foo')
expect(tsJest.__singleton().process).toHaveBeenCalledTimes(1)
expect(tsJest.__singleton().process).toHaveBeenCalledWith('foo')
tsJest.process('bar')
expect(tsJest.__singleton().process).toHaveBeenCalledTimes(2)
expect(tsJest.__singleton().process).toHaveBeenLastCalledWith('bar')
})
it('should use the same instance of transformer for `getCacheKey`', () => {
tsJest.getCacheKey('foo')
expect(tsJest.__singleton().getCacheKey).toHaveBeenCalledTimes(1)
expect(tsJest.__singleton().getCacheKey).toHaveBeenCalledWith('foo')
tsJest.getCacheKey('bar')
expect(tsJest.__singleton().getCacheKey).toHaveBeenCalledTimes(2)
expect(tsJest.__singleton().getCacheKey).toHaveBeenLastCalledWith('bar')
})
})