diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 4331144fa4..9fbe4cdb0f 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -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' @@ -112,6 +112,41 @@ const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => { } export class ConfigSet { + @Memoize() + get projectPackageJson(): Record { + 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 { + 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, + ) + } + @Memoize() get jest(): jest.ProjectConfig { const config = backportJestConfig(this.logger, this._jestConfig) @@ -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 } @@ -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, diff --git a/src/util/messages.ts b/src/util/messages.ts index 8f371158bc..1033e7bfcf 100644 --- a/src/util/messages.ts +++ b/src/util/messages.ts @@ -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.', } /**