Skip to content

Commit ec70365

Browse files
authored
fix: burst cache when file’s deps change (#2167)
Construct cache key with additional information is resolved module name + resolved module's last modified time. When one of imported modules of a file changes, the file needs to be reprocessed to have type check properly Closes #2118 Closes #1122 Closes #943
1 parent f32ca9a commit ec70365

File tree

9 files changed

+351
-364
lines changed

9 files changed

+351
-364
lines changed

e2e/__tests__/__snapshots__/logger.test.ts.snap

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ Array [
2020
"[level:20] normalized typescript config via ts-jest option",
2121
"[level:20] normalized custom AST transformers via ts-jest option",
2222
"[level:20] file caching disabled",
23+
"[level:20] created language service",
2324
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
25+
"[level:20] getting resolved modules from TypeScript API for <cwd>/Hello.spec.ts",
26+
"[level:20] updateMemoryCache: update memory cache for language service",
2427
"[level:20] processing <cwd>/Hello.spec.ts",
25-
"[level:20] created language service",
2628
"[level:20] getCompiledOutput(): compiling using language service",
2729
"[level:20] updateMemoryCache: update memory cache for language service",
2830
"[level:20] visitSourceFileNode(): hoisting",
2931
"[level:20] getCompiledOutput(): computing diagnostics using language service",
3032
"[level:20] computing cache key for <cwd>/Hello.ts",
33+
"[level:20] getting resolved modules from TypeScript API for <cwd>/Hello.ts",
34+
"[level:20] updateMemoryCache: update memory cache for language service",
3135
"[level:20] processing <cwd>/Hello.ts",
3236
"[level:20] getCompiledOutput(): compiling using language service",
3337
"[level:20] updateMemoryCache: update memory cache for language service",
@@ -60,15 +64,19 @@ Array [
6064
"[level:20] normalized typescript config via ts-jest option",
6165
"[level:20] normalized custom AST transformers via ts-jest option",
6266
"[level:20] file caching disabled",
67+
"[level:20] created language service",
6368
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
69+
"[level:20] getting resolved modules from TypeScript API for <cwd>/Hello.spec.ts",
70+
"[level:20] updateMemoryCache: update memory cache for language service",
6471
"[level:20] processing <cwd>/Hello.spec.ts",
65-
"[level:20] created language service",
6672
"[level:20] getCompiledOutput(): compiling using language service",
6773
"[level:20] updateMemoryCache: update memory cache for language service",
6874
"[level:20] visitSourceFileNode(): hoisting",
6975
"[level:20] getCompiledOutput(): computing diagnostics using language service",
7076
"[level:20] calling babel-jest processor",
7177
"[level:20] computing cache key for <cwd>/Hello.ts",
78+
"[level:20] getting resolved modules from TypeScript API for <cwd>/Hello.ts",
79+
"[level:20] updateMemoryCache: update memory cache for language service",
7280
"[level:20] processing <cwd>/Hello.ts",
7381
"[level:20] getCompiledOutput(): compiling using language service",
7482
"[level:20] updateMemoryCache: update memory cache for language service",
@@ -103,15 +111,19 @@ Array [
103111
"[level:20] normalized typescript config via ts-jest option",
104112
"[level:20] normalized custom AST transformers via ts-jest option",
105113
"[level:20] file caching disabled",
114+
"[level:20] created language service",
106115
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
116+
"[level:20] getting resolved modules from TypeScript API for <cwd>/Hello.spec.ts",
117+
"[level:20] updateMemoryCache: update memory cache for language service",
107118
"[level:20] processing <cwd>/Hello.spec.ts",
108-
"[level:20] created language service",
109119
"[level:20] getCompiledOutput(): compiling using language service",
110120
"[level:20] updateMemoryCache: update memory cache for language service",
111121
"[level:20] visitSourceFileNode(): hoisting",
112122
"[level:20] getCompiledOutput(): computing diagnostics using language service",
113123
"[level:20] calling babel-jest processor",
114124
"[level:20] computing cache key for <cwd>/Hello.ts",
125+
"[level:20] getting resolved modules from TypeScript API for <cwd>/Hello.ts",
126+
"[level:20] updateMemoryCache: update memory cache for language service",
115127
"[level:20] processing <cwd>/Hello.ts",
116128
"[level:20] getCompiledOutput(): compiling using language service",
117129
"[level:20] updateMemoryCache: update memory cache for language service",

src/compiler/__snapshots__/ts-compiler.spec.ts.snap

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -36,49 +36,6 @@ exports[`TsCompiler isolatedModule false allowJs option should compile js file f
3636
================================================================================
3737
`;
3838

39-
exports[`TsCompiler isolatedModule false diagnostics should not report diagnostics for test file which doesn't exist when compiling import module file 1`] = `
40-
Array [
41-
"[level:20] getCompiledOutput(): compiling using language service
42-
",
43-
"[level:20] updateMemoryCache: update memory cache for language service
44-
",
45-
"[level:20] visitSourceFileNode(): hoisting
46-
",
47-
"[level:20] getCompiledOutput(): computing diagnostics using language service
48-
",
49-
]
50-
`;
51-
52-
exports[`TsCompiler isolatedModule false diagnostics should only report diagnostics for imported modules but not test files without cache 1`] = `
53-
Array [
54-
"[level:20] getCompiledOutput(): compiling using language service
55-
",
56-
"[level:20] updateMemoryCache: update memory cache for language service
57-
",
58-
"[level:20] visitSourceFileNode(): hoisting
59-
",
60-
"[level:20] getCompiledOutput(): computing diagnostics using language service
61-
",
62-
]
63-
`;
64-
65-
exports[`TsCompiler isolatedModule false diagnostics should report diagnostics for imported modules as well as test files which use imported modules with cache 1`] = `
66-
Array [
67-
"[level:20] getCompiledOutput(): compiling using language service
68-
",
69-
"[level:20] updateMemoryCache: update memory cache for language service
70-
",
71-
"[level:20] visitSourceFileNode(): hoisting
72-
",
73-
"[level:20] getCompiledOutput(): computing diagnostics using language service
74-
",
75-
"[level:20] updateMemoryCache: update memory cache for language service
76-
",
77-
"[level:20] getCompiledOutput(): computing diagnostics using language service for test file which uses the module
78-
",
79-
]
80-
`;
81-
8239
exports[`TsCompiler isolatedModule false diagnostics should throw error when cannot compile 1`] = `
8340
"Unable to require \`.d.ts\` file for file: test-cannot-compile.d.ts.
8441
This is usually the result of a faulty configuration or import. Make sure there is a \`.js\`, \`.json\` or another executable extension available alongside \`test-cannot-compile.d.ts\`."
@@ -97,25 +54,6 @@ Array [
9754
]
9855
`;
9956

100-
exports[`TsCompiler isolatedModule false diagnostics shouldn't report diagnostics for test file name that has been type checked before 1`] = `
101-
Array [
102-
"[level:20] getCompiledOutput(): compiling using language service
103-
",
104-
"[level:20] updateMemoryCache: update memory cache for language service
105-
",
106-
"[level:20] visitSourceFileNode(): hoisting
107-
",
108-
"[level:20] getCompiledOutput(): computing diagnostics using language service
109-
",
110-
"[level:20] getCompiledOutput(): compiling using language service
111-
",
112-
"[level:20] updateMemoryCache: update memory cache for language service
113-
",
114-
"[level:20] visitSourceFileNode(): hoisting
115-
",
116-
]
117-
`;
118-
11957
exports[`TsCompiler isolatedModule false jsx option should compile tsx file for jsx preserve 1`] = `
12058
===[ FILE: test-jsx.tsx ]=======================================================
12159
"use strict";

src/compiler/ts-compiler.spec.ts

Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { readFileSync, renameSync } from 'fs'
1+
import { readFileSync } from 'fs'
22
import { LogLevels } from 'bs-logger'
3-
import { removeSync } from 'fs-extra'
4-
import { join } from 'path'
53

64
import { TS_JEST_OUT_DIR } from '../config/config-set'
75
import { makeCompiler } from '../__helpers__/fakers'
@@ -284,131 +282,6 @@ const t: string = f(5)
284282
const importedFileName = require.resolve('../__mocks__/thing.ts')
285283
const importedFileContent = readFileSync(importedFileName, 'utf-8')
286284

287-
it(`should report diagnostics for imported modules as well as test files which use imported modules with cache`, async () => {
288-
const testFileName = require.resolve('../__mocks__/thing1.spec.ts')
289-
const testFileContent = readFileSync(testFileName, 'utf-8')
290-
const cacheDir = join(process.cwd(), 'tmp')
291-
/**
292-
* Run the 1st compilation with Promise resolve setTimeout to stimulate 2 different test runs to test cached
293-
* resolved modules
294-
*/
295-
async function firstCompile() {
296-
return new Promise<void>((resolve) => {
297-
const compiler1 = makeCompiler({
298-
jestConfig: {
299-
cache: true,
300-
cacheDirectory: cacheDir,
301-
},
302-
tsJestConfig: baseTsJestConfig,
303-
})
304-
305-
logTarget.clear()
306-
compiler1.getCompiledOutput(testFileContent, testFileName)
307-
308-
// probably 300ms is enough to stimulate 2 separated runs after each other
309-
setTimeout(() => resolve(), 300)
310-
})
311-
}
312-
313-
await firstCompile()
314-
315-
const compiler2 = makeCompiler({
316-
jestConfig: {
317-
cache: true,
318-
cacheDirectory: cacheDir,
319-
},
320-
tsJestConfig: baseTsJestConfig,
321-
})
322-
logTarget.clear()
323-
324-
compiler2.getCompiledOutput(importedFileContent, importedFileName)
325-
326-
expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchSnapshot()
327-
328-
removeSync(cacheDir)
329-
})
330-
331-
it(`should not report diagnostics for test file which doesn't exist when compiling import module file`, async () => {
332-
const testFileName = require.resolve('../__mocks__/thing.spec.ts')
333-
const testFileContent = readFileSync(testFileName, 'utf-8')
334-
const cacheDir = join(process.cwd(), 'tmp')
335-
/**
336-
* Run the 1st compilation with Promise resolve setTimeout to stimulate 2 different test runs to test cached
337-
* resolved modules
338-
*/
339-
async function firstCompile() {
340-
return new Promise<void>((resolve) => {
341-
const compiler1 = makeCompiler({
342-
jestConfig: {
343-
cache: true,
344-
cacheDirectory: cacheDir,
345-
},
346-
tsJestConfig: baseTsJestConfig,
347-
})
348-
349-
logTarget.clear()
350-
compiler1.getCompiledOutput(testFileContent, testFileName)
351-
352-
// probably 300ms is enough to stimulate 2 separated runs after each other
353-
setTimeout(() => resolve(), 300)
354-
})
355-
}
356-
357-
await firstCompile()
358-
359-
const newTestFileName = testFileName.replace('thing', 'thing2')
360-
renameSync(testFileName, newTestFileName)
361-
362-
const compiler2 = makeCompiler({
363-
jestConfig: {
364-
cache: true,
365-
cacheDirectory: cacheDir,
366-
},
367-
tsJestConfig: baseTsJestConfig,
368-
})
369-
logTarget.clear()
370-
371-
compiler2.getCompiledOutput(importedFileContent, importedFileName)
372-
373-
expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchSnapshot()
374-
375-
renameSync(newTestFileName, testFileName)
376-
removeSync(cacheDir)
377-
})
378-
379-
it(`should only report diagnostics for imported modules but not test files without cache`, () => {
380-
const testFileName = require.resolve('../__mocks__/thing1.spec.ts')
381-
const testFileContent = readFileSync(testFileName, 'utf-8')
382-
const compiler1 = makeCompiler({
383-
tsJestConfig: baseTsJestConfig,
384-
})
385-
logTarget.clear()
386-
compiler1.getCompiledOutput(testFileContent, testFileName)
387-
388-
const compiler2 = makeCompiler({
389-
tsJestConfig: baseTsJestConfig,
390-
})
391-
logTarget.clear()
392-
393-
compiler2.getCompiledOutput(importedFileContent, importedFileName)
394-
395-
expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchSnapshot()
396-
})
397-
398-
it(`shouldn't report diagnostics for test file name that has been type checked before`, () => {
399-
const testFileName = require.resolve('../__mocks__/thing1.spec.ts')
400-
const testFileContent = readFileSync(testFileName, 'utf-8')
401-
const compiler1 = makeCompiler({
402-
tsJestConfig: baseTsJestConfig,
403-
})
404-
logTarget.clear()
405-
406-
compiler1.getCompiledOutput(testFileContent, testFileName)
407-
compiler1.getCompiledOutput(testFileContent, testFileName)
408-
409-
expect(logTarget.filteredLines(LogLevels.debug, Infinity)).toMatchSnapshot()
410-
})
411-
412285
it(`shouldn't report diagnostics when file name doesn't match diagnostic file pattern`, () => {
413286
const compiler = makeCompiler({
414287
tsJestConfig: {

0 commit comments

Comments
 (0)