From 3910f2cdd5209ceb76512d58399375b26184c692 Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Thu, 6 Sep 2018 21:21:34 +0200 Subject: [PATCH] fix(config): fixes a bug in the tsconfig file resolver --- src/config/config-set.spec.ts | 44 ++++++++++++++++++++++++++++++++++- src/config/config-set.ts | 14 +++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 4d0ce545e1..d8f84e2f7c 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -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' @@ -251,3 +251,45 @@ describe('resolvePath', () => { expect(doResolve('/bar.js')).toBe(resolve('/root//bar.js')) }) }) + +describe('readTsConfig', () => { + let findConfig!: jest.SpyInstance + let readConfig!: jest.SpyInstance + let parseConfig!: jest.SpyInstance + 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') + }) +}) diff --git a/src/config/config-set.ts b/src/config/config-set.ts index b0a2f27fc4..bdcd9e4e3c 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -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 @@ -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)