Skip to content

Commit

Permalink
feat(config): specify package.json location (kulshekhar#823)
Browse files Browse the repository at this point in the history
Updates loading of package.json to allow specifying of file location or
entire contents in addition to existing behavior.
  • Loading branch information
JD Huntington committed Mar 1, 2019
1 parent 2dc4576 commit 147abe4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/config/config-set.spec.ts
@@ -1,5 +1,5 @@
import { testing } from 'bs-logger'
import { resolve } from 'path'
import { join, resolve } from 'path'
import ts, { Diagnostic, DiagnosticCategory, ModuleKind, ScriptTarget } from 'typescript'

import * as _myModule from '..'
Expand Down Expand Up @@ -402,6 +402,20 @@ describe('readTsConfig', () => {
})
}) // readTsConfig

describe('readPackageJson', () => {
it('should return inlined package definitiopn', () => {
const cs = createConfigSet({ tsJestConfig: { packageJson: { foo: 'bar' } } as any })
const packageJson = cs.projectPackageJson
expect(packageJson.foo).toEqual('bar')
})

it('should use given package.json path', () => {
const expected = require(join(__dirname, '..', '..', 'package.json'))
const cs = createConfigSet({ tsJestConfig: { packageJson: join(__dirname, '..', '..', 'package.json') } as any })
expect(cs.projectPackageJson).toEqual(expected)
})
}) // readPackageJson

describe('versions', () => {
describe('without babel', () => {
it('should return correct version map', () => {
Expand Down
36 changes: 28 additions & 8 deletions src/config/config-set.ts
Expand Up @@ -114,18 +114,38 @@ const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
export class ConfigSet {
@Memoize()
get projectPackageJson(): Record<string, any> {
const parsedConfig = this.jest
const { globals = {} } = parsedConfig as any
const options: TsJestGlobalOptions = { ...globals['ts-jest'] }

if (options.packageJson && typeof options.packageJson === 'string') {
const path = this.resolvePath(options.packageJson)
if (existsSync(path)) {
return require(path)
}
this.logger.warn(Errors.UnableToFindProjectRoot)
return {}
}

if (options.packageJson && typeof options.packageJson === 'object') {
return options.packageJson
}

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)
if (existsSync(pkgPath)) {
return require(pkgPath)
}

if (realpathSync(this.rootDir) === realpathSync(tsJestRoot)) {
pkgPath = resolve(tsJestRoot, 'package.json')
if (existsSync(pkgPath)) {
return require(pkgPath)
}
}
return exists ? require(pkgPath) : {}

this.logger.warn(Errors.UnableToFindProjectRoot)
return {}
}

@Memoize()
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Expand Up @@ -21,6 +21,14 @@ export interface TsJestGlobalOptions {
*/
tsConfig?: boolean | string | CompilerOptions

/**
* packageJson. It can be:
* - `true` (or `undefined`, it's the default): use default package.json file
* - `path/to/package.json`: path to a specific package.json file (<rootDir> can be used)
* - `{...}`: contents of a package.json
*/
packageJson?: boolean | string | object

/**
* Whether to compile files as isolated modules (disables some features and type-checking, default to `false`):
*/
Expand Down

0 comments on commit 147abe4

Please sign in to comment.