Skip to content

Commit

Permalink
fix(compiler): check if test file exists before doing type check (#1827)
Browse files Browse the repository at this point in the history
Close #1506
  • Loading branch information
ahnpnl authored Jul 25, 2020
1 parent 6706bd6 commit cc89d5b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/__mocks__/thing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Thing } from './thing'

export const thing: Thing = { a: 1 }
export const thing: Thing = { a: 1, b: 2 }
15 changes: 15 additions & 0 deletions src/compiler/__snapshots__/language-service.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 49 additions & 1 deletion src/compiler/language-service.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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')
Expand Down
10 changes: 7 additions & 3 deletions src/compiler/language-service.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cc89d5b

Please sign in to comment.