Skip to content

Commit

Permalink
fix(config): fixes a bug in the tsconfig file resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Sep 6, 2018
1 parent 629ca07 commit 3910f2c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
44 changes: 43 additions & 1 deletion src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path'
import { ModuleKind, ScriptTarget } from 'typescript'
import ts, { ModuleKind, ScriptTarget } from 'typescript'

import * as fakers from '../__helpers__/fakers'
import { mocked } from '../__helpers__/mocks'
Expand Down Expand Up @@ -251,3 +251,45 @@ describe('resolvePath', () => {
expect(doResolve('<rootDir>/bar.js')).toBe(resolve('/root//bar.js'))
})
})

describe('readTsConfig', () => {
let findConfig!: jest.SpyInstance<typeof ts.findConfigFile>
let readConfig!: jest.SpyInstance<typeof ts.readConfigFile>
let parseConfig!: jest.SpyInstance<typeof ts.parseJsonSourceFileConfigFileContent>
let cs!: ConfigSet
beforeAll(() => {
findConfig = jest.spyOn(ts, 'findConfigFile')
readConfig = jest.spyOn(ts, 'readConfigFile')
parseConfig = jest.spyOn(ts, 'parseJsonConfigFileContent')
cs = createConfigSet({ jestConfig: { rootDir: '/root', cwd: '/cwd' } as any })
findConfig.mockImplementation(p => `${p}/tsconfig.json`)
readConfig.mockImplementation(p => ({ config: { path: p, compilerOptions: {} } }))
parseConfig.mockImplementation((conf: any) => ({ options: conf }))
})
beforeEach(() => {
findConfig.mockClear()
readConfig.mockClear()
parseConfig.mockClear()
})
afterAll(() => {
findConfig.mockRestore()
readConfig.mockRestore()
parseConfig.mockRestore()
})
it('should use correct paths when searching', () => {
const conf = cs.readTsConfig()
expect(conf.input.path).toBe('/root/tsconfig.json')
expect(findConfig.mock.calls[0][0]).toBe('/root')
expect(readConfig.mock.calls[0][0]).toBe('/root/tsconfig.json')
expect(parseConfig.mock.calls[0][2]).toBe('/root')
expect(parseConfig.mock.calls[0][4]).toBe('/root/tsconfig.json')
})
it('should use given tsconfig path', () => {
const conf = cs.readTsConfig(undefined, '/foo/tsconfig.bar.json')
expect(conf.input.path).toBe('/foo/tsconfig.bar.json')
expect(findConfig).not.toBeCalled()
expect(readConfig.mock.calls[0][0]).toBe('/foo/tsconfig.bar.json')
expect(parseConfig.mock.calls[0][2]).toBe('/foo')
expect(parseConfig.mock.calls[0][4]).toBe('/foo/tsconfig.bar.json')
})
})
14 changes: 9 additions & 5 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,13 @@ export class ConfigSet {
return !!process.env.TS_JEST_DOCTOR
}

readTsConfig(compilerOptions?: object, project?: string | null, noProject?: boolean | null): ReadTsConfigResult {
readTsConfig(
compilerOptions?: object,
resolvedConfigFile?: string | null,
noProject?: boolean | null,
): ReadTsConfigResult {
let config = { compilerOptions: {} }
let basePath = normalizeSlashes(this.cwd)
let basePath = normalizeSlashes(this.rootDir)
let configFileName: string | undefined
const ts = this.compilerModule
let input: any
Expand All @@ -453,9 +457,9 @@ export class ConfigSet {
input = { compilerOptions: { ...compilerOptions } }
} else {
// Read project configuration when available.
configFileName = project
? normalizeSlashes(resolve(this.cwd, project))
: ts.findConfigFile(normalizeSlashes(this.cwd), ts.sys.fileExists)
configFileName = resolvedConfigFile
? normalizeSlashes(resolvedConfigFile)
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists)

if (configFileName) {
this.logger.debug({ tsConfigFileName: configFileName }, 'readTsConfig(): reading', configFileName)
Expand Down

0 comments on commit 3910f2c

Please sign in to comment.