Skip to content

Commit e891608

Browse files
committedSep 22, 2018
fix(cache): resolved tsconfig in cache key + pkg digest
- Includes the resolved tsconfig object and not the input one in the jest cache key. - Includes a digest of ts-jest's package in the jest cache key. - Cascade typescript compiler cache dir Closes #749
1 parent a1b77a1 commit e891608

12 files changed

+77
-21
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ tests/simple-long-path/long-src-path
5050
# is linked to the temp dir of the os
5151
e2e/__workdir_synlink__
5252
**/.ts-jest-e2e.json
53+
.ts-jest-digest
5354
# while refactoring...
5455
old/
5556

‎.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ tslint.json
7474
docs
7575

7676
commitlint.config.js
77+
78+
# ensure we do not omit the digest
79+
!.ts-jest-digest

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
"scripts": {
99
"prebuild": "node scripts/clean-dist.js",
1010
"build": "tsc -p tsconfig.build.json",
11+
"postbuild": "node scripts/post-build.js",
1112
"build:watch": "tsc -p tsconfig.build.json -w",
1213
"clean": "node scripts/clean.js",
13-
"pretest": "run-s typecheck lint",
14+
"pretest": "npm run lint",
1415
"test": "run-s -s test:e2e \"test:unit -- {@}\" --",
1516
"test:prepare": "npm run test:e2e -- --prepareOnly",
1617
"test:e2e": "node scripts/e2e.js",

‎scripts/e2e.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const path = require('path')
88
const Paths = require('./lib/paths')
99
const { createHash } = require('crypto')
1010
const logger = require('./lib/logger')
11-
const { createBundle, packageDigest } = require('./lib/bundle')
11+
const { createBundle, readPackageDigest } = require('./lib/bundle')
1212
const npm = require('./lib/npm')
1313

1414
const configFile = path.join(Paths.e2eRootDir, 'jest.config.js')
@@ -49,8 +49,7 @@ function setupE2e() {
4949
)
5050

5151
// get the hash of the bundle (to know if we should install it again or not)
52-
// we need to compute it ourselfs as the npm pack creates different tgz even tho content has not changed
53-
const bundleHash = packageDigest(bundle)
52+
const bundleHash = readPackageDigest()
5453
log('ts-jest digest:', bundleHash)
5554

5655
// ensure directory exists before copying over

‎scripts/lib/bundle.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const npm = require('./npm')
22
const logger = require('./logger')
3-
const { rootDir } = require('./paths')
3+
const { rootDir, pkgDigestFile } = require('./paths')
44
const { join, resolve } = require('path')
5-
const { readFileSync, statSync } = require('fs')
5+
const { readFileSync, statSync, writeFileSync, existsSync } = require('fs-extra')
66
const { createHash } = require('crypto')
77
const { sync: globIgnore } = require('glob-gitignore')
88

@@ -19,7 +19,11 @@ function createBundle(log = logger.log.bind(logger)) {
1919
return join(rootDir, res.stdout.toString().trim())
2020
}
2121

22-
function packageDigest() {
22+
function readPackageDigest() {
23+
return existsSync(pkgDigestFile) ? readFileSync(pkgDigestFile, 'utf8') : undefined
24+
}
25+
26+
function computePackageDigest(noWriteFile = false) {
2327
const files = globIgnore(join(rootDir, '**'), {
2428
ignore: readFileSync(join(rootDir, '.npmignore'))
2529
.toString('utf8')
@@ -28,14 +32,19 @@ function packageDigest() {
2832
})
2933
const hash = createHash('sha1')
3034
files.sort().forEach(file => {
31-
if (statSync(file).isDirectory()) return
35+
if (file === pkgDigestFile || statSync(file).isDirectory()) return
3236
hash.update(readFileSync(resolve(file)))
3337
hash.update('\x00')
3438
})
35-
return hash.digest('hex')
39+
const digest = hash.digest('hex')
40+
if (!noWriteFile) {
41+
writeFileSync(pkgDigestFile, digest, 'utf8')
42+
}
43+
return digest
3644
}
3745

3846
module.exports = {
3947
createBundle,
40-
packageDigest,
48+
computePackageDigest,
49+
readPackageDigest,
4150
}

‎scripts/lib/paths.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare const rootDir: string
2+
declare const pkgDigestFile: string
23
declare const e2eSourceDir: string
34
declare const e2eRootDir: string
45
declare const e2eWorkDir: string
@@ -11,6 +12,7 @@ declare const e2eTestsDir: string
1112

1213
export {
1314
rootDir,
15+
pkgDigestFile,
1416
e2eSourceDir,
1517
e2eRootDir,
1618
e2eWorkDir,

‎scripts/lib/paths.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const os = require('os')
33

44
const rootDir = path.resolve(__dirname, '..', '..')
5+
const pkgDigestFile = path.join(rootDir, '.ts-jest-digest')
56
const distDir = path.join(rootDir, 'dist')
67
const testsRootDir = path.join(rootDir, 'tests')
78
const e2eRootDir = path.join(rootDir, 'e2e')
@@ -15,6 +16,7 @@ const e2eWorkTemplatesDir = path.join(e2eWorkDir, '__templates__')
1516
const e2eWotkDirLink = path.join(e2eRootDir, '__workdir_synlink__')
1617

1718
module.exports = {
19+
pkgDigestFile,
1820
rootDir,
1921
e2eSourceDir,
2022
e2eRootDir,

‎scripts/post-build.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { computePackageDigest } = require('./lib/bundle')
2+
3+
computePackageDigest()

‎src/__mocks__/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const version = '1.2.3-test.0'
2+
export const digest = 'a0d51ca854194df8191d0e65c0ca4730f510f332'
3+
export const createTransformer = jest.fn()
4+
export const jestPreset = { jestPreset: true }
5+
export const createJestPreset = jest.fn()
6+
export const pathsToModuleNameMapper = jest.fn()

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

+26-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { testing } from 'bs-logger'
22
import { resolve } from 'path'
33
import ts, { Diagnostic, ModuleKind, ScriptTarget } from 'typescript'
44

5-
import { version as currentVersion } from '../../package.json'
5+
import * as _myModule from '..'
66
import * as fakers from '../__helpers__/fakers'
77
import { logTargetMock, mocked } from '../__helpers__/mocks'
88
import { TsJestGlobalOptions } from '../types'
@@ -12,8 +12,10 @@ import { normalizeSlashes } from '../util/normalize-slashes'
1212
import { ConfigSet, IGNORE_DIAGNOSTIC_CODES, MATCH_NOTHING } from './config-set'
1313

1414
jest.mock('../util/backports')
15+
jest.mock('../index')
1516

1617
const backports = mocked(_backports)
18+
const myModule = mocked(_myModule)
1719

1820
backports.backportJestConfig.mockImplementation((_, config) => ({
1921
...config,
@@ -385,7 +387,7 @@ describe('versions', () => {
385387
it('should return correct version map', () => {
386388
expect(createConfigSet().versions).toEqual({
387389
jest: pkgVersion('jest'),
388-
'ts-jest': currentVersion,
390+
'ts-jest': myModule.version,
389391
typescript: pkgVersion('typescript'),
390392
})
391393
})
@@ -397,13 +399,19 @@ describe('versions', () => {
397399
'babel-core': pkgVersion('babel-core'),
398400
'babel-jest': pkgVersion('babel-jest'),
399401
jest: pkgVersion('jest'),
400-
'ts-jest': currentVersion,
402+
'ts-jest': myModule.version,
401403
typescript: pkgVersion('typescript'),
402404
})
403405
})
404406
})
405407
}) // versions
406408

409+
describe('tsJestDigest', () => {
410+
it('should be the package digest', () => {
411+
expect(createConfigSet().tsJestDigest).toBe(myModule.digest)
412+
})
413+
}) // tsJestDigest
414+
407415
describe('tsconfig', () => {
408416
it('should return input tsconfig', () => {
409417
const cs = createConfigSet({ tsJestConfig: { tsConfig: { target: 'ES6' } } as any })
@@ -468,8 +476,9 @@ describe('cacheKey', () => {
468476
const val = cs.jsonValue.value
469477
delete val.versions
470478
cs.jsonValue.value = val
479+
// digest is mocked in src/__mocks__/index.ts
471480
expect(cs.cacheKey).toMatchInlineSnapshot(
472-
`"{\\"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\\":{\\"compilerOptions\\":{}}}"`,
481+
`"{\\"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}}"`,
473482
)
474483
})
475484
}) // cacheKey
@@ -479,13 +488,15 @@ describe('jsonValue', () => {
479488
const cs = createConfigSet({ tsJestConfig: { tsConfig: false } as any })
480489
const val = cs.jsonValue.valueOf()
481490
expect(cs.toJSON()).toEqual(val)
482-
// it will change each time we upgrade and we tested those in the `version` block
491+
// it will change each time we upgrade we tested those in the `version` block
483492
expect(val.versions).toEqual(cs.versions)
484493
delete val.versions
485494

495+
// digest is mocked in src/__mocks__/index.ts
486496
expect(val).toMatchInlineSnapshot(`
487497
Object {
488498
"babel": undefined,
499+
"digest": "a0d51ca854194df8191d0e65c0ca4730f510f332",
489500
"jest": Object {
490501
"__backported": true,
491502
"globals": Object {},
@@ -511,7 +522,16 @@ Object {
511522
"tsConfig": undefined,
512523
},
513524
"tsconfig": Object {
514-
"compilerOptions": Object {},
525+
"configFilePath": undefined,
526+
"declaration": false,
527+
"inlineSourceMap": false,
528+
"inlineSources": true,
529+
"module": 1,
530+
"noEmit": false,
531+
"outDir": "$$ts-jest$$",
532+
"removeComments": false,
533+
"sourceMap": true,
534+
"target": 1,
515535
},
516536
}
517537
`)

‎src/config/config-set.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
SourceFile,
2424
} from 'typescript'
2525

26-
import { version as myVersion } from '..'
26+
import { digest as MY_DIGEST, version as MY_VERSION } from '..'
2727
import { createCompiler } from '../compiler'
2828
import { internals as internalAstTransformers } from '../transformers'
2929
import {
@@ -229,7 +229,7 @@ export class ConfigSet {
229229
map[name] = getPackageVersion(name) || '-'
230230
return map
231231
},
232-
{ 'ts-jest': myVersion } as Record<string, string>,
232+
{ 'ts-jest': MY_VERSION } as Record<string, string>,
233233
)
234234
}
235235

@@ -438,10 +438,10 @@ export class ConfigSet {
438438
compiler: this.tsJest.compiler,
439439
compilerOptions: this.typescript.options,
440440
isolatedModules: this.tsJest.isolatedModules,
441-
ignoreDiagnostics: this.tsJest.diagnostics.ignoreCodes,
441+
diagnostics: this.tsJest.diagnostics,
442442
}),
443443
)
444-
const res = join(this.jest.cacheDirectory, `ts-jest-${cacheSuffix}`)
444+
const res = join(this.jest.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
445445
logger.debug({ cacheDirectory: res }, `will use file caching`)
446446
return res
447447
}
@@ -489,6 +489,11 @@ export class ConfigSet {
489489
return !!process.env.TS_JEST_DOCTOR
490490
}
491491

492+
@Memoize()
493+
get tsJestDigest(): string {
494+
return MY_DIGEST
495+
}
496+
492497
/**
493498
* @internal
494499
*/
@@ -505,11 +510,12 @@ export class ConfigSet {
505510

506511
return new JsonableValue({
507512
versions: this.versions,
513+
digest: this.tsJestDigest,
508514
transformers: this.astTransformers.map(t => `${t.name}@${t.version}`),
509515
jest,
510516
tsJest: this.tsJest,
511517
babel: this.babel,
512-
tsconfig: this.tsconfig,
518+
tsconfig: this.typescript.options,
513519
})
514520
}
515521

‎src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { readFileSync } from 'fs'
2+
import { resolve } from 'path'
3+
14
import { createJestPreset } from './config/create-jest-preset'
25
import { pathsToModuleNameMapper } from './config/paths-to-module-name-mapper'
36
import { TsJestTransformer } from './ts-jest-transformer'
@@ -6,6 +9,7 @@ import { VersionCheckers } from './util/version-checkers'
69

710
// tslint:disable-next-line:no-var-requires
811
export const version: string = require('../package.json').version
12+
export const digest: string = readFileSync(resolve(__dirname, '..', '.ts-jest-digest'), 'utf8')
913

1014
let transformer!: TsJestTransformer
1115
function defaultTransformer(): TsJestTransformer {

0 commit comments

Comments
 (0)
Please sign in to comment.