Skip to content
Permalink
Browse files

fix(compiler): check if test file exists before doing type check (#1827)

Close #1506
  • Loading branch information
ahnpnl committed Jul 25, 2020
1 parent 6706bd6 commit cc89d5b1f912975cd29114c5b3b0bf18426816da
@@ -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",
@@ -1,3 +1,3 @@
import { Thing } from './thing'

export const thing: Thing = { a: 1 }
export const thing: Thing = { a: 1, b: 2 }
@@ -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
@@ -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')
@@ -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

0 comments on commit cc89d5b

Please sign in to comment.
You can’t perform that action at this time.