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): add file content empty string to compiler cache #2633

Merged
merged 1 commit into from
May 28, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
3 changes: 2 additions & 1 deletion e2e/__external-repos__/simple/with-dependency/package.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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