Skip to content

Commit

Permalink
fix(cache): adds project's dep versions to cache key
Browse files Browse the repository at this point in the history
Includes the real version of each dependency of the containing project
in the cache key digest calculation.

Closes kulshekhar#785
  • Loading branch information
huafu committed Oct 6, 2018
1 parent d1de3bf commit e7efc96
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/config/config-set.ts
Expand Up @@ -9,7 +9,7 @@
* with the complete, object version of it.
*/
import { LogContexts, Logger } from 'bs-logger'
import { existsSync, readFileSync } from 'fs'
import { existsSync, readFileSync, realpathSync } from 'fs'
import json5 from 'json5'
import { dirname, isAbsolute, join, normalize, resolve } from 'path'
import semver from 'semver'
Expand Down Expand Up @@ -112,6 +112,41 @@ const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
}

export class ConfigSet {
@Memoize()
get projectPackageJson(): Record<string, any> {
const tsJestRoot = resolve(__dirname, '..', '..')
let pkgPath = resolve(tsJestRoot, '..', '..', 'package.json')
let exists = existsSync(pkgPath)
if (!exists) {
if (realpathSync(this.rootDir) === realpathSync(tsJestRoot)) {
pkgPath = resolve(tsJestRoot, 'package.json')
exists = true
} else {
this.logger.warn(Errors.UnableToFindProjectRoot)
}
}
return exists ? require(pkgPath) : {}
}

@Memoize()
get projectDependencies(): Record<string, string> {
const { projectPackageJson: pkg } = this
const names = Object.keys({
...pkg.optionalDependencies,
...pkg.peerDependencies,
...pkg.devDependencies,
...pkg.dependencies,
})
return names.reduce(
(map, name) => {
const version = getPackageVersion(name)
if (version) map[name] = version
return map
},
{} as Record<string, string>,
)
}

@Memoize()
get jest(): jest.ProjectConfig {
const config = backportJestConfig(this.logger, this._jestConfig)
Expand Down Expand Up @@ -447,7 +482,7 @@ export class ConfigSet {
}),
)
const res = join(this.jest.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
logger.debug({ cacheDirectory: res }, `will use file caching`)
logger.debug({ cacheDirectory: res }, 'will use file caching')
return res
}

Expand Down Expand Up @@ -519,6 +554,7 @@ export class ConfigSet {

return new JsonableValue({
versions: this.versions,
projectDepVersions: this.projectDependencies,
digest: this.tsJestDigest,
transformers: this.astTransformers.map(t => `${t.name}@${t.version}`),
jest,
Expand Down
1 change: 1 addition & 0 deletions src/util/messages.ts
Expand Up @@ -21,6 +21,7 @@ export enum Errors {
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.',
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.',
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.',
UnableToFindProjectRoot = 'Unable to find the root of the project where ts-jest has been installed.',
}

/**
Expand Down

0 comments on commit e7efc96

Please sign in to comment.