-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathlanguage-service.spec.ts
249 lines (202 loc) · 8.09 KB
/
language-service.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
import { LogLevels } from 'bs-logger'
import { readFileSync } from 'fs'
import { writeFileSync } from 'fs-extra'
import { makeCompiler } from '../__helpers__/fakers'
import { logTargetMock } from '../__helpers__/mocks'
import { tempDir } from '../__helpers__/path'
import ProcessedSource from '../__helpers__/processed-source'
import { normalizeSlashes } from '../util/normalize-slashes'
import * as compilerUtils from './compiler-utils'
const logTarget = logTargetMock()
describe('Language service', () => {
beforeEach(() => {
logTarget.clear()
})
describe('cache resolved modules', () => {
let spy: jest.SpyInstance<any>
const tmp = tempDir('compiler')
const fileName = 'src/__mocks__/unchanged-modules/main.spec.ts'
const source = `import { Thing } from './main'
export const thing: Thing = { a: 1 }`
beforeEach(() => {
spy = jest.spyOn(compilerUtils, 'cacheResolvedModules').mockImplementation(() => {})
})
afterEach(() => {
spy.mockRestore()
})
it('should cache resolved modules for test file with testMatchPatterns from jest config when match', () => {
const compiler = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp, testRegex: [/.*\.(spec|test)\.[jt]sx?$/] as any[] },
tsJestConfig: { tsConfig: false },
})
compiler.compile(source, fileName)
expect(spy).toHaveBeenCalled()
expect(spy.mock.calls[0][0]).toEqual(normalizeSlashes(fileName))
expect(spy.mock.calls[0][1]).toEqual(source)
})
it("shouldn't cache resolved modules for test file with testMatchPatterns from jest config when not match", () => {
const compiler = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp, testRegex: [/.*\.(foo|bar)\.[jt]sx?$/] as any[] },
tsJestConfig: { tsConfig: false },
})
compiler.compile(source, fileName)
expect(compilerUtils.cacheResolvedModules).not.toHaveBeenCalled()
})
})
describe('allowJs option', () => {
const fileName = 'test-allow-js.js'
const source = 'export default 42'
it('should compile js file for allowJs true with outDir', () => {
const compiler = makeCompiler({
tsJestConfig: { tsConfig: { allowJs: true, outDir: '$$foo$$' } },
})
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
})
it('should compile js file for allowJs true without outDir', () => {
const compiler = makeCompiler({
tsJestConfig: { tsConfig: { allowJs: true } },
})
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
})
})
describe('jsx option', () => {
const fileName = 'test-jsx.tsx'
const source = `
const App = () => {
return <>Test</>
}
`
it('should compile tsx file for jsx preserve', () => {
const compiler = makeCompiler({
tsJestConfig: {
tsConfig: {
jsx: 'preserve' as any,
},
},
})
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
})
it('should compile tsx file for other jsx options', () => {
const compiler = makeCompiler({
tsJestConfig: {
tsConfig: {
jsx: 'react' as any,
},
},
})
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
})
})
describe('source maps', () => {
const source = 'const gsm = (v: number) => v\nconst h: number = gsm(5)'
const fileName = 'test-source-map.ts'
it('should have correct source maps without mapRoot', () => {
const compiler = makeCompiler({ tsJestConfig: { tsConfig: false } })
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName).outputSourceMaps).toMatchObject({
file: fileName,
sources: [fileName],
sourcesContent: [source],
})
})
it('should have correct source maps with mapRoot', () => {
const compiler = makeCompiler({
tsJestConfig: {
tsConfig: {
mapRoot: './',
},
},
})
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName).outputSourceMaps).toMatchObject({
file: fileName,
sources: [fileName],
sourcesContent: [source],
})
})
})
it('should not do type check for the test file which is already finished type checking before', () => {
const tmp = tempDir('compiler')
const testFileName = 'src/__mocks__/unchanged-modules/main.spec.ts'
const testFileSrc = readFileSync(testFileName, 'utf-8')
const importedModuleSrc = readFileSync('src/__mocks__/unchanged-modules/main.ts', 'utf-8')
const compiler = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp, testMatch: ['src/__mocks__/unchanged-modules/*.spec.ts'] },
tsJestConfig: { tsConfig: false },
})
compiler.compile(testFileSrc, testFileName)
logTarget.clear()
compiler.compile(importedModuleSrc, require.resolve('../__mocks__/unchanged-modules/main.ts'))
expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchInlineSnapshot(`
Array [
"[level:20] compileAndCacheResult(): get compile output
",
"[level:20] compileFn(): compiling using language service
",
"[level:20] updateMemoryCache(): update memory cache for language service
",
"[level:20] visitSourceFileNode(): hoisting
",
"[level:20] compileFn(): computing diagnostics using language service
",
]
`)
})
it('should do type check for the test file when imported module has changed', () => {
const tmp = tempDir('compiler')
const testFileName = 'src/__mocks__/changed-modules/main.spec.ts'
const testFileSrc = readFileSync(testFileName, 'utf-8')
const importedModulePath = 'src/__mocks__/changed-modules/main.ts'
const importedModuleSrc = readFileSync(importedModulePath, 'utf-8')
const newImportedModuleSrc = 'export interface Thing { a: number, b: number }'
const compiler1 = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp, testMatch: ['src/__mocks__/changed-modules/*.spec.ts'] },
tsJestConfig: { tsConfig: false },
})
compiler1.compile(testFileSrc, testFileName)
writeFileSync(importedModulePath, 'export interface Thing { a: number, b: number }')
const compiler2 = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp, testMatch: ['src/__mocks__/changed-modules/*.spec.ts'] },
tsJestConfig: { tsConfig: false },
})
expect(() =>
compiler2.compile(newImportedModuleSrc, require.resolve('../__mocks__/changed-modules/main.ts')),
).toThrowErrorMatchingSnapshot()
writeFileSync(importedModulePath, importedModuleSrc)
})
describe('diagnostics', () => {
const fileName = 'test-diagnostics.ts'
const source = `
const g = (v: number) => v
const x: string = g(5)
`
it('should report diagnostics related to typings with pathRegex config matches file name', () => {
const compiler = makeCompiler({
tsJestConfig: { tsConfig: false, diagnostics: { pathRegex: fileName } },
})
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
})
it('should not report diagnostics related to typings with pathRegex config does not match file name', () => {
const compiler = makeCompiler({
tsJestConfig: { tsConfig: false, diagnostics: { pathRegex: 'bar.ts' } },
})
expect(() => compiler.compile(source, fileName)).not.toThrowError()
})
})
it('should throw error when cannot compile', () => {
const fileName = 'test-cannot-compile.d.ts'
const source = `
interface Foo {
a: string
}
`
const compiler = makeCompiler({
tsJestConfig: { tsConfig: false },
})
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
})
})