Skip to content

Commit ab15575

Browse files
authored
feat: allow passing TS config loader options (#15234)
1 parent 6c56895 commit ab15575

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
- `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584))
1313
- `[jest-config]` Loads config file from provided path in `package.json` ([#14044](https://github.com/facebook/jest/pull/14044))
1414
- `[jest-config]` Allow loading `jest.config.cts` files ([#14070](https://github.com/facebook/jest/pull/14070))
15-
- `[jest-config]` Added an option to disable `ts-node` typechecking ([#15161](https://github.com/jestjs/jest/pull/15161))
1615
- `[jest-config]` Show `rootDir` in error message when a `preset` fails to load ([#15194](https://github.com/jestjs/jest/pull/15194))
1716
- `[jest-config]` Support loading TS config files using `esbuild-register` via docblock loader ([#15190](https://github.com/jestjs/jest/pull/15190))
17+
- `[jest-config]` allow passing TS config loader options via docblock comment ([#15234](https://github.com/jestjs/jest/pull/15234))
1818
- `[@jest/core]` Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14789](https://github.com/jestjs/jest/pull/14789))
1919
- `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622))
2020
- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array<string> }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319))

docs/Configuration.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,20 @@ const config: Config = {
7373
export default config;
7474
```
7575

76-
If you are using `ts-node`, you can set `JEST_CONFIG_TRANSPILE_ONLY` environment variable to `true` (case insensitive) to read configuration files without typechecking.
76+
You can also pass options to the loader, for instance to enable `transpileOnly`.
77+
78+
```ts title="jest.config.ts"
79+
/** @jest-config-loader ts-node */
80+
/** @jest-config-loader-options {"transpileOnly": true} */
81+
82+
import type {Config} from 'jest';
83+
84+
const config: Config = {
85+
verbose: true,
86+
};
87+
88+
export default config;
89+
```
7790

7891
:::
7992

e2e/__tests__/jest.config.ts.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const jestTypesExists = fs.existsSync(jestTypesPath);
8484
writeFiles(DIR, {
8585
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
8686
'jest.config.ts': `
87+
/**@jest-config-loader-options {"transpileOnly":${!!skipTypeCheck}}*/
8788
import {Config} from 'jest';
8889
const config: Config = { testTimeout: "10000" };
8990
export default config;
@@ -95,11 +96,7 @@ const jestTypesExists = fs.existsSync(jestTypesPath);
9596
"TS2322: Type 'string' is not assignable to type 'number'.";
9697
const runtimeErrorString = 'Option "testTimeout" must be of type:';
9798

98-
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
99-
env: {
100-
JEST_CONFIG_TRANSPILE_ONLY: skipTypeCheck ? 'true' : undefined,
101-
},
102-
});
99+
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);
103100

104101
if (skipTypeCheck) {
105102
expect(stderr).not.toMatch(typeErrorString);

packages/jest-config/src/readConfigFileAndSetRootDir.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,19 @@ export default async function readConfigFileAndSetRootDir(
8989
}
9090

9191
// Load the TypeScript configuration
92+
let extraTSLoaderOptions: Record<string, unknown>;
93+
9294
const loadTSConfigFile = async (
9395
configPath: string,
9496
): Promise<Config.InitialOptions> => {
9597
// Get registered TypeScript compiler instance
9698
const docblockPragmas = parse(extract(fs.readFileSync(configPath, 'utf8')));
9799
const tsLoader = docblockPragmas['jest-config-loader'] || 'ts-node';
100+
const docblockTSLoaderOptions = docblockPragmas['jest-config-loader-options'];
98101

102+
if (typeof docblockTSLoaderOptions === 'string') {
103+
extraTSLoaderOptions = JSON.parse(docblockTSLoaderOptions);
104+
}
99105
if (Array.isArray(tsLoader)) {
100106
throw new TypeError(
101107
`Jest: You can only define a single loader through docblocks, got "${tsLoader.join(
@@ -143,8 +149,7 @@ async function registerTsLoader(loader: TsLoaderModule): Promise<TsLoader> {
143149
moduleTypes: {
144150
'**': 'cjs',
145151
},
146-
transpileOnly:
147-
process.env.JEST_CONFIG_TRANSPILE_ONLY?.toLowerCase() === 'true',
152+
...extraTSLoaderOptions,
148153
});
149154
} else if (loader === 'esbuild-register') {
150155
const tsLoader = await import(
@@ -158,6 +163,7 @@ async function registerTsLoader(loader: TsLoaderModule): Promise<TsLoader> {
158163
if (bool) {
159164
instance = tsLoader.register({
160165
target: `node${process.version.slice(1)}`,
166+
...extraTSLoaderOptions,
161167
});
162168
} else {
163169
instance?.unregister();

0 commit comments

Comments
 (0)