Skip to content

Commit

Permalink
fix(compiler): add empty string file content to compiler cache (#2633)
Browse files Browse the repository at this point in the history
Closes #2625
  • Loading branch information
ahnpnl committed May 28, 2021
1 parent e6fb636 commit 0feb556
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 43 deletions.
Empty file.
Empty file.
3 changes: 2 additions & 1 deletion e2e/__external-repos__/simple/with-dependency/package.json
@@ -1,6 +1,7 @@
{
"name": "with-dependency",
"version": "0.0.1",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"test": "jest --no-cache"
},
Expand Down
8 changes: 3 additions & 5 deletions e2e/__external-repos__/simple/with-dependency/tsconfig.json
Expand Up @@ -13,9 +13,7 @@
"downlevelIteration": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
},
"include": [
"./src"
]
"esModuleInterop": true,
"noEmitOnError": true
}
}
38 changes: 13 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -119,6 +119,7 @@
"eslint-plugin-prettier": "latest",
"execa": "latest",
"fs-extra": "10.x",
"glob": "^7.1.7",
"glob-gitignore": "latest",
"husky": "4.x",
"jest": "^27.0.0",
Expand Down
15 changes: 7 additions & 8 deletions scripts/lib/paths.js
@@ -1,5 +1,7 @@
const os = require('os')
const path = require('path')
const glob = require('glob')
const fs = require('fs')

const rootDir = path.resolve(__dirname, '..', '..')
const cacheDir = path.join(rootDir, '.cache')
Expand All @@ -16,14 +18,11 @@ const e2eWorkDir = process.env.TS_JEST_E2E_WORKDIR
: path.join(os.tmpdir(), '--ts-jest-temp-e2e--')
const e2eWorkTemplatesDir = path.join(e2eWorkDir, '__templates__')
const e2eWorkDirLink = path.join(e2eRootDir, '__workdir_synlink__')
const projectsToRun = [
`${e2eExternalRepoDir}/custom-typings`,
`${e2eExternalRepoDir}/memory-usage`,
`${e2eExternalRepoDir}/path-mapping`,
`${e2eExternalRepoDir}/simple/with-dependency`,
`${e2eExternalRepoDir}/simple-project-references`,
`${e2eExternalRepoDir}/yarn-workspace-composite`,
]
const projectsToRun = glob
.sync(`${e2eExternalRepoDir}/*`)
.filter((e2ePath) => fs.lstatSync(e2ePath).isDirectory() && fs.existsSync(path.join(e2ePath, 'package.json')))
.sort()
projectsToRun.push(`${e2eExternalRepoDir}/simple/with-dependency`)
const rawCompilerOptionsFileName = path.join('src', 'raw-compiler-options.ts')
const generatedPath = path.join(process.cwd(), rawCompilerOptionsFileName)

Expand Down
Empty file added src/__mocks__/empty.ts
Empty file.
15 changes: 13 additions & 2 deletions src/compiler/ts-compiler.spec.ts
Expand Up @@ -3,12 +3,13 @@ import { basename, join, normalize } from 'path'

import { DiagnosticCategory, EmitOutput, ModuleKind, ScriptTarget, TranspileOutput } from 'typescript'

import { makeCompiler } from '../__helpers__/fakers'
import { createConfigSet, makeCompiler } from '../__helpers__/fakers'
import { mockFolder } from '../__helpers__/path'
import type { DepGraphInfo } from '../types'
import { Errors, interpolate } from '../utils/messages'

import { updateOutput } from './compiler-utils'
import { TsCompiler } from './ts-compiler'

const baseTsJestConfig = { tsconfig: join(process.cwd(), 'tsconfig.spec.json') }

Expand Down Expand Up @@ -163,9 +164,12 @@ describe('TsCompiler', () => {
const sourceMap = '{}'

test.each([true, false])('should compile codes with useESM %p', (useESM) => {
const compiler = makeCompiler({
const configSet = createConfigSet({
tsJestConfig: { ...baseTsJestConfig, useESM },
})
const emptyFile = join(mockFolder, 'empty.ts')
configSet.parsedTsConfig.fileNames.push(emptyFile)
const compiler = new TsCompiler(configSet, new Map())
// @ts-expect-error testing purpose
compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({
outputFiles: [{ text: sourceMap }, { text: jsOutput }],
Expand Down Expand Up @@ -193,6 +197,13 @@ describe('TsCompiler', () => {
expect(esModuleInterop).toEqual(useESM ? true : esModuleInterop)
expect(allowSyntheticDefaultImports).toEqual(useESM ? true : allowSyntheticDefaultImports)
expect(output).toEqual(updateOutput(jsOutput, fileName, sourceMap))

// @ts-expect-error testing purpose
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
compiler._languageService!.getSemanticDiagnostics(fileName)

// @ts-expect-error testing purpose
expect(compiler._fileContentCache.has(emptyFile)).toBe(true)
})

test('should show a warning message and return original file content for non ts/tsx files if emitSkipped is true', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ts-compiler.ts
Expand Up @@ -266,15 +266,15 @@ export class TsCompiler implements TsCompilerInstance {

this._logger.trace({ normalizedFileName, cacheHit: hit }, 'getScriptSnapshot():', 'cache', hit ? 'hit' : 'miss')

// Read contents from TypeScript memory cache.
// Read file content from either memory cache or Jest runtime cache or fallback to file system read
if (!hit) {
const fileContent =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._fileContentCache!.get(normalizedFileName) ??
this._runtimeCacheFS.get(normalizedFileName) ??
this._cachedReadFile?.(normalizedFileName) ??
undefined
if (fileContent) {
if (fileContent !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._fileContentCache!.set(normalizedFileName, fileContent)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down

0 comments on commit 0feb556

Please sign in to comment.