|
1 | 1 | import { resolve } from 'path' |
2 | | -import { ModuleKind, ScriptTarget } from 'typescript' |
| 2 | +import ts, { ModuleKind, ScriptTarget } from 'typescript' |
3 | 3 |
|
4 | 4 | import * as fakers from '../__helpers__/fakers' |
5 | 5 | import { mocked } from '../__helpers__/mocks' |
@@ -251,3 +251,45 @@ describe('resolvePath', () => { |
251 | 251 | expect(doResolve('<rootDir>/bar.js')).toBe(resolve('/root//bar.js')) |
252 | 252 | }) |
253 | 253 | }) |
| 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 | +}) |
0 commit comments