Skip to content

Commit 0feb556

Browse files
authoredMay 28, 2021
fix(compiler): add empty string file content to compiler cache (#2633)
Closes #2625
1 parent e6fb636 commit 0feb556

File tree

10 files changed

+41
-43
lines changed

10 files changed

+41
-43
lines changed
 

‎e2e/__external-repos__/custom-typings/foo.ts

Whitespace-only changes.

‎e2e/__external-repos__/simple/with-dependency/empty.ts

Whitespace-only changes.

‎e2e/__external-repos__/simple/with-dependency/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "with-dependency",
3-
"version": "0.0.1",
3+
"version": "0.0.0",
4+
"license": "MIT",
45
"scripts": {
56
"test": "jest --no-cache"
67
},

‎e2e/__external-repos__/simple/with-dependency/tsconfig.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
"downlevelIteration": true,
1414
"strict": true,
1515
"moduleResolution": "node",
16-
"esModuleInterop": true
17-
},
18-
"include": [
19-
"./src"
20-
]
16+
"esModuleInterop": true,
17+
"noEmitOnError": true
18+
}
2119
}

‎package-lock.json

+13-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"eslint-plugin-prettier": "latest",
120120
"execa": "latest",
121121
"fs-extra": "10.x",
122+
"glob": "^7.1.7",
122123
"glob-gitignore": "latest",
123124
"husky": "4.x",
124125
"jest": "^27.0.0",

‎scripts/lib/paths.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const os = require('os')
22
const path = require('path')
3+
const glob = require('glob')
4+
const fs = require('fs')
35

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

‎src/__mocks__/empty.ts

Whitespace-only changes.

‎src/compiler/ts-compiler.spec.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { basename, join, normalize } from 'path'
33

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

6-
import { makeCompiler } from '../__helpers__/fakers'
6+
import { createConfigSet, makeCompiler } from '../__helpers__/fakers'
77
import { mockFolder } from '../__helpers__/path'
88
import type { DepGraphInfo } from '../types'
99
import { Errors, interpolate } from '../utils/messages'
1010

1111
import { updateOutput } from './compiler-utils'
12+
import { TsCompiler } from './ts-compiler'
1213

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

@@ -163,9 +164,12 @@ describe('TsCompiler', () => {
163164
const sourceMap = '{}'
164165

165166
test.each([true, false])('should compile codes with useESM %p', (useESM) => {
166-
const compiler = makeCompiler({
167+
const configSet = createConfigSet({
167168
tsJestConfig: { ...baseTsJestConfig, useESM },
168169
})
170+
const emptyFile = join(mockFolder, 'empty.ts')
171+
configSet.parsedTsConfig.fileNames.push(emptyFile)
172+
const compiler = new TsCompiler(configSet, new Map())
169173
// @ts-expect-error testing purpose
170174
compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({
171175
outputFiles: [{ text: sourceMap }, { text: jsOutput }],
@@ -193,6 +197,13 @@ describe('TsCompiler', () => {
193197
expect(esModuleInterop).toEqual(useESM ? true : esModuleInterop)
194198
expect(allowSyntheticDefaultImports).toEqual(useESM ? true : allowSyntheticDefaultImports)
195199
expect(output).toEqual(updateOutput(jsOutput, fileName, sourceMap))
200+
201+
// @ts-expect-error testing purpose
202+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
203+
compiler._languageService!.getSemanticDiagnostics(fileName)
204+
205+
// @ts-expect-error testing purpose
206+
expect(compiler._fileContentCache.has(emptyFile)).toBe(true)
196207
})
197208

198209
test('should show a warning message and return original file content for non ts/tsx files if emitSkipped is true', () => {

‎src/compiler/ts-compiler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,15 @@ export class TsCompiler implements TsCompilerInstance {
266266

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

269-
// Read contents from TypeScript memory cache.
269+
// Read file content from either memory cache or Jest runtime cache or fallback to file system read
270270
if (!hit) {
271271
const fileContent =
272272
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
273273
this._fileContentCache!.get(normalizedFileName) ??
274274
this._runtimeCacheFS.get(normalizedFileName) ??
275275
this._cachedReadFile?.(normalizedFileName) ??
276276
undefined
277-
if (fileContent) {
277+
if (fileContent !== undefined) {
278278
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
279279
this._fileContentCache!.set(normalizedFileName, fileContent)
280280
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

0 commit comments

Comments
 (0)
Please sign in to comment.