Skip to content

Commit 6cacbea

Browse files
committedOct 6, 2018
fix(cache): adds project's dep versions to cache key
Includes the real version of each dependency of the containing project in the cache key digest calculation. Closes #785
1 parent 7b2dd01 commit 6cacbea

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed
 

‎src/config/config-set.spec.ts

+46-3
Original file line numberDiff line numberDiff line change
@@ -489,23 +489,63 @@ describe('babelJestTransformer', () => {
489489
})
490490
}) // babelJestTransformer
491491

492+
describe('projectDependencies', () => {
493+
const pkg = {
494+
optionalDependencies: { opt: '1.2.3' },
495+
peerDependencies: { peer: '1.2.4' },
496+
devDependencies: { dev: '1.2.5' },
497+
dependencies: { std: '1.2.6' },
498+
}
499+
const realVersions: any = {
500+
peer: '0.1.0',
501+
dev: '4.3.2',
502+
std: '9.10.2',
503+
}
504+
it('should list all deps with their real version', () => {
505+
jest.doMock('peer/package.json', () => ({ version: realVersions.peer }), { virtual: true })
506+
jest.doMock('dev/package.json', () => ({ version: realVersions.dev }), { virtual: true })
507+
jest.doMock('std/package.json', () => ({ version: realVersions.std }), { virtual: true })
508+
const cs = createConfigSet({
509+
tsJestConfig: { tsConfig: false } as any,
510+
projectPackageJson: pkg,
511+
})
512+
expect(cs.projectDependencies).toEqual(realVersions)
513+
jest.dontMock('peer/package.json')
514+
jest.dontMock('dev/package.json')
515+
jest.dontMock('std/package.json')
516+
})
517+
}) // projectDependencies
518+
492519
describe('cacheKey', () => {
493520
it('should be a string', () => {
494-
const cs = createConfigSet({ tsJestConfig: { tsConfig: false } as any })
521+
const cs = createConfigSet({
522+
tsJestConfig: { tsConfig: false } as any,
523+
projectDependencies: {
524+
opt: '1.2.3',
525+
peer: '1.2.4',
526+
dev: '1.2.5',
527+
std: '1.2.6',
528+
},
529+
})
495530
// we tested those and don't want the snapshot to change all the time we upgrade
496531
const val = cs.jsonValue.value
497532
delete val.versions
498533
cs.jsonValue.value = val
499534
// digest is mocked in src/__mocks__/index.ts
500535
expect(cs.cacheKey).toMatchInlineSnapshot(
501-
`"{\\"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,\\"transformers\\":[]},\\"tsconfig\\":{\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"outDir\\":\\"$$ts-jest$$\\",\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1}}"`,
536+
'"{\\"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,\\"transformers\\":[]},\\"tsconfig\\":{\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"outDir\\":\\"$$ts-jest$$\\",\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1}}"',
502537
)
503538
})
504539
}) // cacheKey
505540

506541
describe('jsonValue', () => {
507542
it('should create jsonValue based on each config and version', () => {
508-
const cs = createConfigSet({ tsJestConfig: { tsConfig: false } as any })
543+
const cs = createConfigSet({
544+
tsJestConfig: { tsConfig: false } as any,
545+
projectDependencies: {
546+
'some-module': '1.2.3',
547+
},
548+
})
509549
const val = cs.jsonValue.valueOf()
510550
expect(cs.toJSON()).toEqual(val)
511551
// it will change each time we upgrade – we tested those in the `version` block
@@ -521,6 +561,9 @@ Object {
521561
"__backported": true,
522562
"globals": Object {},
523563
},
564+
"projectDepVersions": Object {
565+
"some-module": "1.2.3",
566+
},
524567
"transformers": Array [
525568
"hoisting-jest-mock@1",
526569
],

‎src/config/config-set.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* with the complete, object version of it.
1010
*/
1111
import { LogContexts, Logger } from 'bs-logger'
12-
import { existsSync, readFileSync } from 'fs'
12+
import { existsSync, readFileSync, realpathSync } from 'fs'
1313
import json5 from 'json5'
1414
import { dirname, isAbsolute, join, normalize, resolve } from 'path'
1515
import semver from 'semver'
@@ -112,6 +112,41 @@ const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
112112
}
113113

114114
export class ConfigSet {
115+
@Memoize()
116+
get projectPackageJson(): Record<string, any> {
117+
const tsJestRoot = resolve(__dirname, '..', '..')
118+
let pkgPath = resolve(tsJestRoot, '..', '..', 'package.json')
119+
let exists = existsSync(pkgPath)
120+
if (!exists) {
121+
if (realpathSync(this.rootDir) === realpathSync(tsJestRoot)) {
122+
pkgPath = resolve(tsJestRoot, 'package.json')
123+
exists = true
124+
} else {
125+
this.logger.warn(Errors.UnableToFindProjectRoot)
126+
}
127+
}
128+
return exists ? require(pkgPath) : {}
129+
}
130+
131+
@Memoize()
132+
get projectDependencies(): Record<string, string> {
133+
const { projectPackageJson: pkg } = this
134+
const names = Object.keys({
135+
...pkg.optionalDependencies,
136+
...pkg.peerDependencies,
137+
...pkg.devDependencies,
138+
...pkg.dependencies,
139+
})
140+
return names.reduce(
141+
(map, name) => {
142+
const version = getPackageVersion(name)
143+
if (version) map[name] = version
144+
return map
145+
},
146+
{} as Record<string, string>,
147+
)
148+
}
149+
115150
@Memoize()
116151
get jest(): jest.ProjectConfig {
117152
const config = backportJestConfig(this.logger, this._jestConfig)
@@ -440,14 +475,16 @@ export class ConfigSet {
440475
const cacheSuffix = sha1(
441476
stringify({
442477
version: this.compilerModule.version,
478+
digest: this.tsJestDigest,
479+
dependencies: this.projectDependencies,
443480
compiler: this.tsJest.compiler,
444481
compilerOptions: this.typescript.options,
445482
isolatedModules: this.tsJest.isolatedModules,
446483
diagnostics: this.tsJest.diagnostics,
447484
}),
448485
)
449486
const res = join(this.jest.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
450-
logger.debug({ cacheDirectory: res }, `will use file caching`)
487+
logger.debug({ cacheDirectory: res }, 'will use file caching')
451488
return res
452489
}
453490

@@ -519,6 +556,7 @@ export class ConfigSet {
519556

520557
return new JsonableValue({
521558
versions: this.versions,
559+
projectDepVersions: this.projectDependencies,
522560
digest: this.tsJestDigest,
523561
transformers: this.astTransformers.map(t => `${t.name}@${t.version}`),
524562
jest,

‎src/util/messages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export enum Errors {
2121
GotUnknownFileTypeWithoutBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore.',
2222
GotUnknownFileTypeWithBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files.',
2323
ConfigNoModuleInterop = 'If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.',
24+
UnableToFindProjectRoot = 'Unable to find the root of the project where ts-jest has been installed.',
2425
}
2526

2627
/**

0 commit comments

Comments
 (0)
Please sign in to comment.