Skip to content

Commit 4875a58

Browse files
authored
fix(config): compute cache key without reading package.json (#1893)
Closes #1892
1 parent 7f1e140 commit 4875a58

3 files changed

Lines changed: 4 additions & 142 deletions

File tree

src/config/__snapshots__/config-set.spec.ts.snap

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"projectDepVersions\\":{\\"dev\\":\\"1.2.5\\",\\"opt\\":\\"1.2.3\\",\\"peer\\":\\"1.2.4\\",\\"std\\":\\"1.2.6\\"},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`;
3+
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`;
44

55
exports[`isTestFile should return a boolean value whether the file matches test pattern 1`] = `true`;
66

@@ -20,9 +20,6 @@ Object {
2020
"globals": Object {},
2121
"name": undefined,
2222
},
23-
"projectDepVersions": Object {
24-
"some-module": "1.2.3",
25-
},
2623
"transformers": Array [
2724
"hoisting-jest-mock@1",
2825
],

src/config/config-set.spec.ts

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { mocked } from '../util'
1616
import { IGNORE_DIAGNOSTIC_CODES, MATCH_NOTHING, TS_JEST_OUT_DIR } from './config-set'
1717
// eslint-disable-next-line no-duplicate-imports
1818
import type { ConfigSet } from './config-set'
19-
import { Deprecations, Errors, interpolate } from '../util/messages'
19+
import { Deprecations } from '../util/messages'
2020

2121
jest.mock('../util/backports')
2222
jest.mock('../index')
@@ -809,13 +809,7 @@ describe('tsCacheDir', () => {
809809
const cacheDir = join(process.cwd(), cacheName)
810810
const partialTsJestCacheDir = join(cacheDir, 'ts-jest')
811811

812-
it.each([
813-
undefined,
814-
Object.create(null),
815-
{ 'ts-jest': { packageJson: undefined } },
816-
{ 'ts-jest': { packageJson: { name: 'foo' } } },
817-
{ 'ts-jest': { packageJson: 'src/__mocks__/package-foo.json' } },
818-
])(
812+
it.each([undefined, Object.create(null)])(
819813
'should return value from which is the combination of ts jest config and jest config when running test with cache',
820814
(data) => {
821815
expect(
@@ -832,73 +826,6 @@ describe('tsCacheDir', () => {
832826
},
833827
)
834828

835-
it('should throw error when running test with cache and cannot resolve package.json path', () => {
836-
const packageJsonPath = 'src/__mocks__/not-exist.json'
837-
838-
expect(
839-
() =>
840-
createConfigSet({
841-
jestConfig: {
842-
cache: true,
843-
cacheDirectory: cacheDir,
844-
globals: {
845-
'ts-jest': { packageJson: packageJsonPath },
846-
},
847-
},
848-
resolve: null,
849-
}).tsCacheDir,
850-
).toThrowError(
851-
new Error(
852-
interpolate(Errors.FileNotFound, {
853-
inputPath: 'src/__mocks__/not-exist.json',
854-
resolvedPath: join(process.cwd(), packageJsonPath),
855-
}),
856-
),
857-
)
858-
})
859-
860-
it(
861-
'should return value and show warning when running test with cache and package.json path is' +
862-
' resolved but the path does not exist',
863-
() => {
864-
const packageJsonPath = 'src/__mocks__/not-exist.json'
865-
const logger = testing.createLoggerMock()
866-
867-
expect(
868-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
869-
createConfigSet({
870-
jestConfig: {
871-
cache: true,
872-
cacheDirectory: cacheDir,
873-
globals: {
874-
'ts-jest': { packageJson: packageJsonPath },
875-
},
876-
},
877-
logger,
878-
}).tsCacheDir!.indexOf(partialTsJestCacheDir),
879-
).toEqual(0)
880-
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
881-
"[level:40] Unable to find the root of the project where ts-jest has been installed.
882-
"
883-
`)
884-
885-
logger.target.clear()
886-
},
887-
)
888-
889-
it('should return value from root packageJson when running test with cache and real path rootDir is the same as tsJest root', () => {
890-
expect(
891-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
892-
createConfigSet({
893-
jestConfig: {
894-
cache: true,
895-
cacheDirectory: cacheDir,
896-
},
897-
resolve: null,
898-
}).tsCacheDir!.indexOf(partialTsJestCacheDir),
899-
).toEqual(0)
900-
})
901-
902829
it('should return undefined when running test without cache', () => {
903830
expect(createConfigSet({ resolve: null }).tsCacheDir).toBeUndefined()
904831
})

src/config/config-set.ts

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
import type { Config } from '@jest/types'
1212
import { LogContexts, Logger } from 'bs-logger'
13-
import { existsSync, readFileSync, realpathSync } from 'fs'
13+
import { existsSync, readFileSync } from 'fs'
1414
import { globsToMatcher } from 'jest-util'
1515
import json5 = require('json5')
1616
import { dirname, extname, isAbsolute, join, normalize, resolve } from 'path'
@@ -128,66 +128,6 @@ const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
128128
}
129129

130130
export class ConfigSet {
131-
/**
132-
* @internal
133-
*/
134-
@Memoize()
135-
private get projectPackageJson(): Record<string, any> {
136-
const {
137-
tsJest: { packageJson },
138-
} = this
139-
if (packageJson) {
140-
if (packageJson.kind === 'inline') {
141-
return packageJson.value
142-
} else if (packageJson.kind === 'file' && packageJson.value) {
143-
const path = this.resolvePath(packageJson.value)
144-
if (existsSync(path)) {
145-
return require(path)
146-
}
147-
148-
this.logger.warn(Errors.UnableToFindProjectRoot)
149-
150-
return {}
151-
}
152-
}
153-
const tsJestRoot = resolve(__dirname, '..', '..')
154-
let pkgPath = resolve(tsJestRoot, '..', '..', 'package.json')
155-
if (existsSync(pkgPath)) {
156-
return require(pkgPath)
157-
}
158-
if (realpathSync(this.rootDir) === realpathSync(tsJestRoot)) {
159-
pkgPath = resolve(tsJestRoot, 'package.json')
160-
if (existsSync(pkgPath)) {
161-
return require(pkgPath)
162-
}
163-
}
164-
165-
this.logger.warn(Errors.UnableToFindProjectRoot)
166-
167-
return {}
168-
}
169-
170-
/**
171-
* @internal
172-
*/
173-
@Memoize()
174-
private get projectDependencies(): Record<string, string> {
175-
const { projectPackageJson: pkg } = this
176-
const names = Object.keys({
177-
...pkg.optionalDependencies,
178-
...pkg.peerDependencies,
179-
...pkg.devDependencies,
180-
...pkg.dependencies,
181-
})
182-
183-
return names.reduce((map, name) => {
184-
const version = getPackageVersion(name)
185-
if (version) map[name] = version
186-
187-
return map
188-
}, {} as Record<string, string>)
189-
}
190-
191131
/**
192132
* @internal
193133
*/
@@ -681,7 +621,6 @@ export class ConfigSet {
681621
stringify({
682622
version: this.compilerModule.version,
683623
digest: this.tsJestDigest,
684-
dependencies: this.projectDependencies,
685624
compiler: this.tsJest.compiler,
686625
compilerOptions: this.parsedTsConfig.options,
687626
isolatedModules: this.tsJest.isolatedModules,
@@ -770,7 +709,6 @@ export class ConfigSet {
770709

771710
return new JsonableValue({
772711
versions: this.versions,
773-
projectDepVersions: this.projectDependencies,
774712
digest: this.tsJestDigest,
775713
transformers: Object.values(this.astTransformers)
776714
.reduce((acc, val) => acc.concat(val), [])

0 commit comments

Comments
 (0)