Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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",
Expand Down
2 changes: 1 addition & 1 deletion 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 }
15 changes: 15 additions & 0 deletions src/compiler/__snapshots__/language-service.spec.ts.snap
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
@@ -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
@@ -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