Skip to content

Commit 3910f2c

Browse files
committed
fix(config): fixes a bug in the tsconfig file resolver
1 parent 629ca07 commit 3910f2c

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/config/config-set.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { resolve } from 'path'
2-
import { ModuleKind, ScriptTarget } from 'typescript'
2+
import ts, { ModuleKind, ScriptTarget } from 'typescript'
33

44
import * as fakers from '../__helpers__/fakers'
55
import { mocked } from '../__helpers__/mocks'
@@ -251,3 +251,45 @@ describe('resolvePath', () => {
251251
expect(doResolve('<rootDir>/bar.js')).toBe(resolve('/root//bar.js'))
252252
})
253253
})
254+
255+
describe('readTsConfig', () => {
256+
let findConfig!: jest.SpyInstance<typeof ts.findConfigFile>
257+
let readConfig!: jest.SpyInstance<typeof ts.readConfigFile>
258+
let parseConfig!: jest.SpyInstance<typeof ts.parseJsonSourceFileConfigFileContent>
259+
let cs!: ConfigSet
260+
beforeAll(() => {
261+
findConfig = jest.spyOn(ts, 'findConfigFile')
262+
readConfig = jest.spyOn(ts, 'readConfigFile')
263+
parseConfig = jest.spyOn(ts, 'parseJsonConfigFileContent')
264+
cs = createConfigSet({ jestConfig: { rootDir: '/root', cwd: '/cwd' } as any })
265+
findConfig.mockImplementation(p => `${p}/tsconfig.json`)
266+
readConfig.mockImplementation(p => ({ config: { path: p, compilerOptions: {} } }))
267+
parseConfig.mockImplementation((conf: any) => ({ options: conf }))
268+
})
269+
beforeEach(() => {
270+
findConfig.mockClear()
271+
readConfig.mockClear()
272+
parseConfig.mockClear()
273+
})
274+
afterAll(() => {
275+
findConfig.mockRestore()
276+
readConfig.mockRestore()
277+
parseConfig.mockRestore()
278+
})
279+
it('should use correct paths when searching', () => {
280+
const conf = cs.readTsConfig()
281+
expect(conf.input.path).toBe('/root/tsconfig.json')
282+
expect(findConfig.mock.calls[0][0]).toBe('/root')
283+
expect(readConfig.mock.calls[0][0]).toBe('/root/tsconfig.json')
284+
expect(parseConfig.mock.calls[0][2]).toBe('/root')
285+
expect(parseConfig.mock.calls[0][4]).toBe('/root/tsconfig.json')
286+
})
287+
it('should use given tsconfig path', () => {
288+
const conf = cs.readTsConfig(undefined, '/foo/tsconfig.bar.json')
289+
expect(conf.input.path).toBe('/foo/tsconfig.bar.json')
290+
expect(findConfig).not.toBeCalled()
291+
expect(readConfig.mock.calls[0][0]).toBe('/foo/tsconfig.bar.json')
292+
expect(parseConfig.mock.calls[0][2]).toBe('/foo')
293+
expect(parseConfig.mock.calls[0][4]).toBe('/foo/tsconfig.bar.json')
294+
})
295+
})

src/config/config-set.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,13 @@ export class ConfigSet {
442442
return !!process.env.TS_JEST_DOCTOR
443443
}
444444

445-
readTsConfig(compilerOptions?: object, project?: string | null, noProject?: boolean | null): ReadTsConfigResult {
445+
readTsConfig(
446+
compilerOptions?: object,
447+
resolvedConfigFile?: string | null,
448+
noProject?: boolean | null,
449+
): ReadTsConfigResult {
446450
let config = { compilerOptions: {} }
447-
let basePath = normalizeSlashes(this.cwd)
451+
let basePath = normalizeSlashes(this.rootDir)
448452
let configFileName: string | undefined
449453
const ts = this.compilerModule
450454
let input: any
@@ -453,9 +457,9 @@ export class ConfigSet {
453457
input = { compilerOptions: { ...compilerOptions } }
454458
} else {
455459
// Read project configuration when available.
456-
configFileName = project
457-
? normalizeSlashes(resolve(this.cwd, project))
458-
: ts.findConfigFile(normalizeSlashes(this.cwd), ts.sys.fileExists)
460+
configFileName = resolvedConfigFile
461+
? normalizeSlashes(resolvedConfigFile)
462+
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists)
459463

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

0 commit comments

Comments
 (0)