/
ts-jest-transformer.spec.ts
264 lines (251 loc) · 7.88 KB
/
ts-jest-transformer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { Config } from '@jest/types'
import stringify = require('fast-json-stable-stringify')
import { sep } from 'path'
import { ParsedCommandLine } from 'typescript'
import { logTargetMock } from './__helpers__/mocks'
import { ConfigSet } from './config/config-set'
import { TsJestTransformer } from './ts-jest-transformer'
describe('configFor', () => {
it('should return the same config-set for same values', () => {
const obj1 = { cwd: '/foo/.', rootDir: '/bar//dummy/..', globals: {} }
const obj2 = { ...obj1 }
const str = stringify(obj1)
const cs1 = new TsJestTransformer().configsFor(obj1 as any)
const cs2 = new TsJestTransformer().configsFor(obj2 as any)
const cs3 = new TsJestTransformer().configsFor(str)
expect(cs1.cwd).toBe(`${sep}foo`)
expect(cs1.rootDir).toBe(`${sep}bar`)
expect(cs2).toBe(cs1)
expect(cs3).toBe(cs1)
})
})
describe('lastTransformerId', () => {
it('should increment for each instance', () => {
const start = TsJestTransformer.lastTransformerId
const id1 = new TsJestTransformer()
const id2 = new TsJestTransformer()
expect(id1).not.toBe(start)
expect(id2).not.toBe(start)
expect(id2).not.toBe(id1)
})
})
describe('process', () => {
let tr: TsJestTransformer
let babel: any
let typescript: ParsedCommandLine
let args: [string, string, any, any]
const config = {
get typescript() {
return typescript
},
shouldStringifyContent: jest.fn(),
get babelJestTransformer() {
return babel
},
tsCompiler: { compile: jest.fn() },
hooks: {},
}
const INPUT = 'export default "foo"'
const FILE = '/foo/bar.ts'
const JEST_CONFIG = {} as Config.ProjectConfig
const OPTIONS = { instrument: false }
const process = () => tr.process(...args)
beforeEach(() => {
tr = new TsJestTransformer()
args = [INPUT, FILE, JEST_CONFIG, OPTIONS]
jest
.spyOn(tr, 'configsFor')
.mockImplementation(() => (config as unknown) as ConfigSet)
.mockClear()
config.shouldStringifyContent.mockImplementation(() => false).mockClear()
babel = null
config.tsCompiler.compile.mockImplementation(s => `ts:${s}`).mockClear()
typescript = { options: {} } as any
})
it('should process ts input without babel', () => {
expect(process()).toBe(`ts:${INPUT}`)
expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/foo/bar.ts",
],
]
`)
expect(config.tsCompiler.compile.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"export default \\"foo\\"",
"/foo/bar.ts",
],
]
`)
})
it('should process js input without babel', () => {
typescript.options.allowJs = true
args[1] = '/foo/bar.js'
expect(process()).toBe(`ts:${INPUT}`)
expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/foo/bar.js",
],
]
`)
expect(config.tsCompiler.compile.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"export default \\"foo\\"",
"/foo/bar.js",
],
]
`)
})
it('should process ts input with babel', () => {
babel = { process: jest.fn(s => `babel:${s}`) }
expect(process()).toBe(`babel:ts:${INPUT}`)
expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/foo/bar.ts",
],
]
`)
expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"ts:export default \\"foo\\"",
"/foo/bar.ts",
Object {},
Object {
"instrument": false,
},
],
]
`)
expect(config.tsCompiler.compile.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"export default \\"foo\\"",
"/foo/bar.ts",
],
]
`)
})
it('should process js input with babel', () => {
typescript.options.allowJs = true
babel = { process: jest.fn(s => `babel:${s}`) }
args[1] = '/foo/bar.js'
expect(process()).toBe(`babel:ts:${INPUT}`)
expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/foo/bar.js",
],
]
`)
expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"ts:export default \\"foo\\"",
"/foo/bar.js",
Object {},
Object {
"instrument": false,
},
],
]
`)
expect(config.tsCompiler.compile.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"export default \\"foo\\"",
"/foo/bar.js",
],
]
`)
})
it('should return stringified version of file', () => {
config.shouldStringifyContent.mockImplementation(() => true)
expect(process()).toMatchInlineSnapshot(`"module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`)
})
it('should warn when trying to process js but allowJs is false', () => {
args[1] = '/foo/bar.js'
typescript.options.allowJs = false
const logs = logTargetMock()
logs.clear()
expect(process()).toBe(INPUT)
expect(logs.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] Got a \`.js\` file to compile while \`allowJs\` option is not set to \`true\` (file: /foo/bar.js). To fix this:
- if you want TypeScript to process JS files, set \`allowJs\` to \`true\` in your TypeScript config (usually tsconfig.json)
- if you do not want TypeScript to process your \`.js\` files, in your Jest config change the \`transform\` key which value is \`ts-jest\` so that it does not match \`.js\` files anymore
",
]
`)
})
it('should warn when trying to process unknown file types', () => {
args[1] = '/foo/bar.jest'
const logs = logTargetMock()
logs.clear()
expect(process()).toBe(INPUT)
expect(logs.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] Got a unknown file type to compile (file: /foo/bar.jest). To fix this, in your Jest config change the \`transform\` key which value is \`ts-jest\` so that it does not match this kind of files anymore.
",
]
`)
logs.clear()
babel = { process: jest.fn(s => `babel:${s}`) }
expect(process()).toBe(`babel:${INPUT}`)
expect(logs.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] Got a unknown file type to compile (file: /foo/bar.jest). To fix this, in your Jest config change the \`transform\` key which value is \`ts-jest\` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the \`transform\` option with value \`babel-jest\` which key matches this type of files.
",
]
`)
})
it('should not pass the instrument option to babel-jest', () => {
babel = { process: jest.fn(s => `babel:${s}`) }
args[3] = { instrument: true }
expect(process()).toBe(`babel:ts:${INPUT}`)
expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"ts:export default \\"foo\\"",
"/foo/bar.ts",
Object {},
Object {
"instrument": false,
},
],
]
`)
})
})
describe('getCacheKey', () => {
it('should be different for each argument value', () => {
const tr = new TsJestTransformer()
jest
.spyOn(tr, 'configsFor')
.mockImplementation(jestConfigStr => (({ cacheKey: jestConfigStr } as unknown) as ConfigSet))
const input = {
fileContent: 'export default "foo"',
fileName: 'foo.ts',
jestConfigStr: '{"foo": "bar"}',
options: { instrument: false, rootDir: '/foo' },
}
const keys = [
tr.getCacheKey(input.fileContent, input.fileName, input.jestConfigStr, input.options),
tr.getCacheKey(input.fileContent, 'bar.ts', input.jestConfigStr, input.options),
tr.getCacheKey(input.fileContent, input.fileName, '{}', input.options),
tr.getCacheKey(input.fileContent, input.fileName, '{}', { ...input.options, instrument: true }),
tr.getCacheKey(input.fileContent, input.fileName, '{}', { ...input.options, rootDir: '/bar' }),
]
// each key should have correct length
for (const key of keys) {
expect(key).toHaveLength(40)
}
// unique array should have same length
expect(keys.filter((k, i, all) => all.indexOf(k) === i)).toHaveLength(keys.length)
})
})