/
language-service.spec.ts
189 lines (155 loc) · 5.3 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
import { LogLevels } from 'bs-logger'
import { removeSync, 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'
const logTarget = logTargetMock()
describe('language service', () => {
beforeEach(() => {
logTarget.clear()
})
it('should use the cache', () => {
const tmp = tempDir('compiler'),
compiler = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp },
tsJestConfig: { tsConfig: false },
}),
source = 'console.log("hello")',
fileName = 'test-cache.ts'
writeFileSync(fileName, source, 'utf8')
logTarget.clear()
const compiled1 = compiler.compile(source, fileName)
expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchInlineSnapshot(`
Array [
"[level:20] readThrough(): cache miss
",
"[level:20] updateMemoryCache() for language service
",
"[level:20] compiler rebuilt Program instance when getting output
",
"[level:20] visitSourceFileNode(): hoisting
",
"[level:20] getOutput(): computing diagnostics for language service
",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true
",
"[level:20] readThrough(): writing caches
",
]
`)
logTarget.clear()
const compiled2 = compiler.compile(source, fileName)
expect(logTarget.lines).toMatchInlineSnapshot(`
Array [
"[level:20] readThrough(): cache hit
",
]
`)
expect(new ProcessedSource(compiled1, fileName)).toMatchSnapshot()
expect(compiled2).toBe(compiled1)
removeSync(fileName)
})
it('should compile js file for allowJs true', () => {
const fileName = `test-allow-js.js`,
compiler = makeCompiler({
tsJestConfig: { tsConfig: { allowJs: true, outDir: '$$ts-jest$$' } },
}),
source = 'export default 42'
writeFileSync(fileName, source, 'utf8')
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
removeSync(fileName)
})
it('should compile tsx file for jsx preserve', () => {
const fileName = 'test-jsx-preserve.tsx',
compiler = makeCompiler({
tsJestConfig: {
tsConfig: {
jsx: 'preserve' as any,
},
},
}),
source = `
const App = () => {
return <>Test</>
}
`
writeFileSync(fileName, source, 'utf8')
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
removeSync(fileName)
})
it('should compile tsx file for other jsx options', () => {
const fileName = 'test-jsx-options.tsx',
compiler = makeCompiler({
tsJestConfig: {
tsConfig: {
jsx: 'react' as any,
},
},
}),
source = `
const App = () => {
return <>Test</>
}
`
writeFileSync(fileName, source, 'utf8')
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
removeSync(fileName)
})
it('should have correct source maps', () => {
const compiler = makeCompiler({ tsJestConfig: { tsConfig: false } }),
source = 'const gsm = (v: number) => v\nconst h: number = gsm(5)',
fileName = 'test-source-map.ts'
writeFileSync(fileName, source, 'utf8')
const compiled = compiler.compile(source, fileName)
expect(new ProcessedSource(compiled, fileName).outputSourceMaps).toMatchObject({
file: fileName,
sources: [fileName],
sourcesContent: [source],
})
removeSync(fileName)
})
it('should report diagnostics related to typings with pathRegex config matches file name', () => {
const fileName = 'test-match-regex-diagnostics.ts',
source = `
const g = (v: number) => v
const x: string = g(5)
`,
compiler = makeCompiler({
tsJestConfig: { tsConfig: false, diagnostics: { pathRegex: fileName } },
})
writeFileSync(fileName, source, 'utf8')
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
removeSync(fileName)
})
it('should not report diagnostics related to typings with pathRegex config does not match file name', () => {
const fileName = 'test-non-match-regex-diagnostics.ts',
source = `
const f = (v: number) => v
const t: string = f(5)
`,
compiler = makeCompiler({
tsJestConfig: { tsConfig: false, diagnostics: { pathRegex: 'bar.ts' } },
})
writeFileSync(fileName, source, 'utf8')
expect(() => compiler.compile(source, fileName)).not.toThrowError()
removeSync(fileName)
})
it('should throw error when cannot compile', () => {
const fileName = 'test-cannot-compile.d.ts',
source = `
interface Foo {
a: string
}
`,
compiler = makeCompiler({
tsJestConfig: { tsConfig: false },
})
writeFileSync(fileName, source, 'utf8')
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
removeSync(fileName)
})
})