diff --git a/package.json b/package.json index fa0b73bff7..f0442ccd11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-jest", - "version": "26.1.3", + "version": "26.1.4-alpha.0", "main": "dist/index.js", "types": "dist/index.d.ts", "bin": "cli.js", diff --git a/src/__mocks__/thing.spec.ts b/src/__mocks__/thing.spec.ts index 0ae3f7e9d1..74c8df1e4f 100644 --- a/src/__mocks__/thing.spec.ts +++ b/src/__mocks__/thing.spec.ts @@ -1,3 +1,3 @@ import { Thing } from './thing' -export const thing: Thing = { a: 1 } +export const thing: Thing = { a: 1, b: 2 } diff --git a/src/compiler/__snapshots__/language-service.spec.ts.snap b/src/compiler/__snapshots__/language-service.spec.ts.snap index 8230db1e0c..a8721b9545 100644 --- a/src/compiler/__snapshots__/language-service.spec.ts.snap +++ b/src/compiler/__snapshots__/language-service.spec.ts.snap @@ -36,6 +36,21 @@ exports[`Language service allowJs option should compile js file for allowJs true ================================================================================ `; +exports[`Language service diagnostics should not report diagnostics for test file which doesn't exist when compiling import module file 1`] = ` +Array [ + "[level:20] compileAndUpdateOutput(): 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 +", +] +`; + exports[`Language service diagnostics should only report diagnostics for imported modules but not test files without cache 1`] = ` Array [ "[level:20] compileAndUpdateOutput(): get compile output diff --git a/src/compiler/language-service.spec.ts b/src/compiler/language-service.spec.ts index 73a6b031b3..29e48f4312 100644 --- a/src/compiler/language-service.spec.ts +++ b/src/compiler/language-service.spec.ts @@ -1,5 +1,5 @@ import { LogLevels } from 'bs-logger' -import { readFileSync } from 'fs' +import { readFileSync, renameSync } from 'fs' import { removeSync } from 'fs-extra' import { join } from 'path' @@ -174,6 +174,54 @@ describe('Language service', () => { removeSync(cacheDir) }) + it(`should not report diagnostics for test file which doesn't exist when compiling import module file`, async () => { + const testFileName = require.resolve('../__mocks__/thing.spec.ts') + const testFileContent = readFileSync(testFileName, 'utf-8') + const cacheDir = join(process.cwd(), 'tmp') + /** + * Run the 1st compilation with Promise resolve setTimeout to stimulate 2 different test runs to test cached + * resolved modules + */ + async function firstCompile() { + return new Promise((resolve) => { + const compiler1 = makeCompiler({ + jestConfig: { + cache: true, + cacheDirectory: cacheDir, + }, + tsJestConfig: baseTsJestConfig, + }) + + logTarget.clear() + compiler1.compile(testFileContent, testFileName) + + // probably 300ms is enough to stimulate 2 separated runs after each other + setTimeout(() => resolve(), 300) + }) + } + + await firstCompile() + + const newTestFileName = testFileName.replace('thing', 'thing2') + renameSync(testFileName, newTestFileName) + + const compiler2 = makeCompiler({ + jestConfig: { + cache: true, + cacheDirectory: cacheDir, + }, + tsJestConfig: baseTsJestConfig, + }) + logTarget.clear() + + compiler2.compile(importedFileContent, importedFileName) + + expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchSnapshot() + + renameSync(newTestFileName, testFileName) + removeSync(cacheDir) + }) + it(`should only report diagnostics for imported modules but not test files without cache`, () => { const testFileName = require.resolve('../__mocks__/thing1.spec.ts') const testFileContent = readFileSync(testFileName, 'utf-8') diff --git a/src/compiler/language-service.ts b/src/compiler/language-service.ts index e5f934d79e..aba4e477e8 100644 --- a/src/compiler/language-service.ts +++ b/src/compiler/language-service.ts @@ -1,5 +1,5 @@ import { LogContexts, Logger, LogLevels } from 'bs-logger' -import { readFileSync, writeFile } from 'fs' +import { existsSync, readFileSync, writeFile } from 'fs' import { basename, normalize, relative, join } from 'path' import memoize = require('lodash.memoize') import mkdirp = require('mkdirp') @@ -221,8 +221,12 @@ export const initializeLanguageServiceInstance = (configs: ConfigSet, logger: Lo /* istanbul ignore next (already covered with unit tests) */ if (!configs.isTestFile(fileName)) { for (const [testFileName, resolvedModules] of memoryCache.resolvedModules.entries()) { - // Only do type checking for test files which haven't been type checked before - if (resolvedModules.includes(fileName) && !diagnosedFiles.includes(testFileName)) { + // Only do type checking for test files which haven't been type checked before as well as the file must exist + if ( + resolvedModules.includes(fileName) && + !diagnosedFiles.includes(testFileName) && + existsSync(testFileName) + ) { const testFileContent = memoryCache.files.get(testFileName)?.text if (!testFileContent) { // Must set memory cache before attempting to get diagnostics