-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support tsconfig references #4689
base: main
Are you sure you want to change the base?
Changes from all commits
daec5b7
066aea0
e26a7fa
84df1d6
28d916d
bafe9d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest' | ||
|
||
export default { | ||
displayName: 'tsconfig-projects-compiler-cjs', | ||
transform: { | ||
[TS_JS_TRANSFORM_PATTERN]: 'ts-jest', | ||
}, | ||
} satisfies JestConfigWithTsJest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest' | ||
|
||
export default { | ||
displayName: 'tsconfig-projects-compiler-esm', | ||
extensionsToTreatAsEsm: ['.ts'], | ||
transform: { | ||
[TS_JS_TRANSFORM_PATTERN]: [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
} satisfies JestConfigWithTsJest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest' | ||
|
||
export default { | ||
displayName: 'tsconfig-projects-transpiler-cjs', | ||
transform: { | ||
[TS_JS_TRANSFORM_PATTERN]: 'ts-jest', | ||
}, | ||
} satisfies JestConfigWithTsJest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest' | ||
|
||
export default { | ||
displayName: 'tsconfig-projects-transpiler-esm', | ||
extensionsToTreatAsEsm: ['.ts'], | ||
transform: { | ||
[TS_JS_TRANSFORM_PATTERN]: [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
} satisfies JestConfigWithTsJest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "tsconfig-projects", | ||
"private": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { foo } from './foo' | ||
|
||
describe('foo', () => { | ||
it('should return "foo"', () => { | ||
expect(foo()).toEqual('foo') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function foo() { | ||
return 'foo' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": [], | ||
"references": [ | ||
{ "path": "./tsconfig.src.json" }, | ||
{ "path": "./tsconfig.spec.json" } | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "Node16", | ||
"typeRoots": ["../node_modules"], | ||
"types": ["@types/jest", "@types/node"], | ||
}, | ||
"include": ["src/**/*.spec.ts"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "Node16" | ||
}, | ||
"include": ["src"], | ||
"exclude": ["src/**/*.spec.ts"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -790,7 +790,6 @@ describe('_resolveTsConfig', () => { | |
expect(conf.options.configFilePath).toBeUndefined() | ||
expect(findConfig).not.toHaveBeenCalled() | ||
expect(readConfig.mock.calls[0][0]).toBe('/foo/tsconfig.bar.json') | ||
expect(parseConfig).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
|
@@ -918,7 +917,6 @@ describe('_resolveTsConfig', () => { | |
|
||
const conf = cs.parsedTsConfig | ||
expect(conf.options.path).toBe(tscfgPathStub) | ||
expect(findConfig).not.toHaveBeenCalled() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will now be called to find out if we there are project references in the tsconfig to check. |
||
expect(readConfig.mock.calls[0][0]).toBe(tscfgPathStub) | ||
expect(parseConfig.mock.calls[0][2]).toBe('/foo') | ||
expect(parseConfig.mock.calls[0][4]).toBe(tscfgPathStub) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,8 +202,6 @@ export class ConfigSet { | |
|
||
this.logger.debug({ compilerModule: this.compilerModule }, 'normalized compiler module config via ts-jest option') | ||
|
||
this._setupConfigSet(options) | ||
this._resolveTsCacheDir() | ||
this._matchablePatterns = [...this._jestCfg.testMatch, ...this._jestCfg.testRegex].filter( | ||
(pattern) => | ||
/** | ||
|
@@ -218,6 +216,9 @@ export class ConfigSet { | |
this._matchTestFilePath = globsToMatcher( | ||
this._matchablePatterns.filter((pattern: string | RegExp) => typeof pattern === 'string') as string[], | ||
) | ||
|
||
this._setupConfigSet(options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this._resolveTsCacheDir() | ||
} | ||
|
||
/** | ||
|
@@ -554,9 +555,7 @@ export class ConfigSet { | |
let basePath = normalizeSlashes(this.rootDir) | ||
const ts = this.compilerModule | ||
// Read project configuration when available. | ||
const configFileName: string | undefined = resolvedConfigFile | ||
? normalizeSlashes(resolvedConfigFile) | ||
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists) | ||
const configFileName: string | undefined = this._findTsconfigFile(resolvedConfigFile) | ||
if (configFileName) { | ||
this.logger.debug({ tsConfigFileName: configFileName }, 'readTsConfig(): reading', configFileName) | ||
|
||
|
@@ -576,6 +575,55 @@ export class ConfigSet { | |
} | ||
|
||
// parse json, merge config extending others, ... | ||
return this._parseTsconfig(config, basePath, configFileName) | ||
} | ||
|
||
protected _findTsconfigFile(resolvedConfigFile?: string) { | ||
const ts = this.compilerModule | ||
|
||
const configFileName: string | undefined = resolvedConfigFile | ||
? normalizeSlashes(resolvedConfigFile) | ||
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists) | ||
|
||
const newTsconfigFile = this._findReferenceTsconfig(configFileName) | ||
|
||
return newTsconfigFile ?? configFileName | ||
} | ||
|
||
protected _findReferenceTsconfig(tsconfigFileName?: string): string | undefined { | ||
const ts = this.compilerModule | ||
|
||
if (!tsconfigFileName) return | ||
|
||
const parsedTsconfig = this._parseTsconfig( | ||
ts.readConfigFile(tsconfigFileName, ts.sys.readFile).config || {}, | ||
dirname(tsconfigFileName), | ||
tsconfigFileName, | ||
) | ||
|
||
if (this._includesTestFilesInConfig(parsedTsconfig)) return tsconfigFileName | ||
|
||
if (parsedTsconfig.projectReferences) { | ||
for (const ref of parsedTsconfig.projectReferences) { | ||
const filePath = ts.resolveProjectReferencePath(ref) | ||
|
||
if (ts.sys.fileExists(filePath)) { | ||
const newTsconfigFileName = this._findReferenceTsconfig(ref.path) | ||
if (newTsconfigFileName) return newTsconfigFileName | ||
} | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
protected _includesTestFilesInConfig(parsedConfig?: ts.ParsedCommandLine) { | ||
return parsedConfig?.fileNames?.length ? parsedConfig.fileNames.some((path) => this.isTestFile(path)) : false | ||
} | ||
|
||
protected _parseTsconfig(config: unknown, basePath: string, configFileName?: string) { | ||
const ts = this.compilerModule | ||
|
||
return ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will now be called to find out if we there are project references in the tsconfig to check.