Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"devDependencies": {
"@formatjs/ts-transformer": "^3.13.31",
"@testing-library/react": "^14.3.1",
"@types/jest": "^29.5.14",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.5",
"esbuild": "~0.24.2",
Expand Down
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/jest-compiler-cjs.config.ts
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
14 changes: 14 additions & 0 deletions e2e/tsconfig-projects/jest-compiler-esm.config.ts
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
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/jest-transpiler-cjs.config.ts
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
14 changes: 14 additions & 0 deletions e2e/tsconfig-projects/jest-transpiler-esm.config.ts
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
4 changes: 4 additions & 0 deletions e2e/tsconfig-projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "tsconfig-projects",
"private": true
}
7 changes: 7 additions & 0 deletions e2e/tsconfig-projects/src/foo.spec.ts
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')
})
})
3 changes: 3 additions & 0 deletions e2e/tsconfig-projects/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 'foo'
}
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/tsconfig.json
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" }
]
}
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/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"]
}
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/tsconfig.src.json
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"]
}
2 changes: 0 additions & 2 deletions src/legacy/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Author

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.

})
})

Expand Down Expand Up @@ -918,7 +917,6 @@ describe('_resolveTsConfig', () => {

const conf = cs.parsedTsConfig
expect(conf.options.path).toBe(tscfgPathStub)
expect(findConfig).not.toHaveBeenCalled()
Copy link
Author

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.

expect(readConfig.mock.calls[0][0]).toBe(tscfgPathStub)
expect(parseConfig.mock.calls[0][2]).toBe('/foo')
expect(parseConfig.mock.calls[0][4]).toBe(tscfgPathStub)
Expand Down
58 changes: 53 additions & 5 deletions src/legacy/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
/**
Expand All @@ -218,6 +216,9 @@ export class ConfigSet {
this._matchTestFilePath = globsToMatcher(
this._matchablePatterns.filter((pattern: string | RegExp) => typeof pattern === 'string') as string[],
)

this._setupConfigSet(options)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_setupConfigSet now depends on matchablePatterns to exist when its called, because isTestFile is used in finding the appropriate tsconfig reference project to use for ts compile/transpile process.

this._resolveTsCacheDir()
}

/**
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}

Expand Down