Skip to content

Commit cc89d5b

Browse files
authored
fix(compiler): check if test file exists before doing type check (#1827)
Close #1506
1 parent 6706bd6 commit cc89d5b

5 files changed

Lines changed: 73 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-jest",
3-
"version": "26.1.3",
3+
"version": "26.1.4-alpha.0",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"bin": "cli.js",

src/__mocks__/thing.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { Thing } from './thing'
22

3-
export const thing: Thing = { a: 1 }
3+
export const thing: Thing = { a: 1, b: 2 }

src/compiler/__snapshots__/language-service.spec.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ exports[`Language service allowJs option should compile js file for allowJs true
3636
================================================================================
3737
`;
3838

39+
exports[`Language service diagnostics should not report diagnostics for test file which doesn't exist when compiling import module file 1`] = `
40+
Array [
41+
"[level:20] compileAndUpdateOutput(): get compile output
42+
",
43+
"[level:20] compileFn(): compiling using language service
44+
",
45+
"[level:20] updateMemoryCache(): update memory cache for language service
46+
",
47+
"[level:20] visitSourceFileNode(): hoisting
48+
",
49+
"[level:20] compileFn(): computing diagnostics using language service
50+
",
51+
]
52+
`;
53+
3954
exports[`Language service diagnostics should only report diagnostics for imported modules but not test files without cache 1`] = `
4055
Array [
4156
"[level:20] compileAndUpdateOutput(): get compile output

src/compiler/language-service.spec.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LogLevels } from 'bs-logger'
2-
import { readFileSync } from 'fs'
2+
import { readFileSync, renameSync } from 'fs'
33
import { removeSync } from 'fs-extra'
44
import { join } from 'path'
55

@@ -174,6 +174,54 @@ describe('Language service', () => {
174174
removeSync(cacheDir)
175175
})
176176

177+
it(`should not report diagnostics for test file which doesn't exist when compiling import module file`, async () => {
178+
const testFileName = require.resolve('../__mocks__/thing.spec.ts')
179+
const testFileContent = readFileSync(testFileName, 'utf-8')
180+
const cacheDir = join(process.cwd(), 'tmp')
181+
/**
182+
* Run the 1st compilation with Promise resolve setTimeout to stimulate 2 different test runs to test cached
183+
* resolved modules
184+
*/
185+
async function firstCompile() {
186+
return new Promise((resolve) => {
187+
const compiler1 = makeCompiler({
188+
jestConfig: {
189+
cache: true,
190+
cacheDirectory: cacheDir,
191+
},
192+
tsJestConfig: baseTsJestConfig,
193+
})
194+
195+
logTarget.clear()
196+
compiler1.compile(testFileContent, testFileName)
197+
198+
// probably 300ms is enough to stimulate 2 separated runs after each other
199+
setTimeout(() => resolve(), 300)
200+
})
201+
}
202+
203+
await firstCompile()
204+
205+
const newTestFileName = testFileName.replace('thing', 'thing2')
206+
renameSync(testFileName, newTestFileName)
207+
208+
const compiler2 = makeCompiler({
209+
jestConfig: {
210+
cache: true,
211+
cacheDirectory: cacheDir,
212+
},
213+
tsJestConfig: baseTsJestConfig,
214+
})
215+
logTarget.clear()
216+
217+
compiler2.compile(importedFileContent, importedFileName)
218+
219+
expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchSnapshot()
220+
221+
renameSync(newTestFileName, testFileName)
222+
removeSync(cacheDir)
223+
})
224+
177225
it(`should only report diagnostics for imported modules but not test files without cache`, () => {
178226
const testFileName = require.resolve('../__mocks__/thing1.spec.ts')
179227
const testFileContent = readFileSync(testFileName, 'utf-8')

src/compiler/language-service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LogContexts, Logger, LogLevels } from 'bs-logger'
2-
import { readFileSync, writeFile } from 'fs'
2+
import { existsSync, readFileSync, writeFile } from 'fs'
33
import { basename, normalize, relative, join } from 'path'
44
import memoize = require('lodash.memoize')
55
import mkdirp = require('mkdirp')
@@ -221,8 +221,12 @@ export const initializeLanguageServiceInstance = (configs: ConfigSet, logger: Lo
221221
/* istanbul ignore next (already covered with unit tests) */
222222
if (!configs.isTestFile(fileName)) {
223223
for (const [testFileName, resolvedModules] of memoryCache.resolvedModules.entries()) {
224-
// Only do type checking for test files which haven't been type checked before
225-
if (resolvedModules.includes(fileName) && !diagnosedFiles.includes(testFileName)) {
224+
// Only do type checking for test files which haven't been type checked before as well as the file must exist
225+
if (
226+
resolvedModules.includes(fileName) &&
227+
!diagnosedFiles.includes(testFileName) &&
228+
existsSync(testFileName)
229+
) {
226230
const testFileContent = memoryCache.files.get(testFileName)?.text
227231
if (!testFileContent) {
228232
// Must set memory cache before attempting to get diagnostics

0 commit comments

Comments
 (0)