Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb6f27ab

Browse files
authored
feat: allowjest.config.cts (#14070)
1 parentc9962ab commitb6f27ab

File tree

9 files changed

+336
-4
lines changed

9 files changed

+336
-4
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
-`[jest-config]`[**BREAKING**] Add`mts` and`cts` to default`moduleFileExtensions` config ([#14369](https://github.com/facebook/jest/pull/14369))
99
-`[jest-config]`[**BREAKING**] Update`testMatch` and`testRegex` default option for supporting`mjs`,`cjs`,`mts`, and`cts` ([#14584](https://github.com/jestjs/jest/pull/14584))
1010
-`[jest-config]` Loads config file from provided path in`package.json` ([#14044](https://github.com/facebook/jest/pull/14044))
11+
-`[jest-config]` Allow loading`jest.config.cts` files ([#14070](https://github.com/facebook/jest/pull/14070))
1112
-`[@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))
1213
-`[@jest/core]` Add`perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622))
1314
-`[@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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Configuring Jest
55

66
The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
77

8-
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named`jest.config.js|ts|mjs|cjs|json`. You can use[`--config`](CLI.md#--configpath) flag to pass an explicit path to the file.
8+
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named`jest.config.js|ts|mjs|cjs|cts|json`. You can use[`--config`](CLI.md#--configpath) flag to pass an explicit path to the file.
99

1010
:::note
1111

‎e2e/__tests__/tsIntegration.test.ts‎

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,45 @@ describe('when `Config` type is imported from "@jest/types"', () => {
5454
expect(globalConfig.verbose).toBe(true);
5555
});
5656

57+
test('with object config exported from CTS file',()=>{
58+
writeFiles(DIR,{
59+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
60+
'jest.config.cts':`
61+
import type {Config} from '@jest/types';
62+
const config: Config.InitialOptions = {displayName: 'ts-object-config', verbose: true};
63+
export default config;
64+
`,
65+
'package.json':'{}',
66+
});
67+
68+
const{configs, globalConfig}=getConfig(path.join(DIR));
69+
70+
expect(configs).toHaveLength(1);
71+
expect(configs[0].displayName?.name).toBe('ts-object-config');
72+
expect(globalConfig.verbose).toBe(true);
73+
});
74+
75+
test('with function config exported from CTS file',()=>{
76+
writeFiles(DIR,{
77+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
78+
'jest.config.cts':`
79+
import type {Config} from '@jest/types';
80+
async function getVerbose() {return true;}
81+
export default async (): Promise<Config.InitialOptions> => {
82+
const verbose: Config.InitialOptions['verbose'] = await getVerbose();
83+
return {displayName: 'ts-async-function-config', verbose};
84+
};
85+
`,
86+
'package.json':'{}',
87+
});
88+
89+
const{configs, globalConfig}=getConfig(path.join(DIR));
90+
91+
expect(configs).toHaveLength(1);
92+
expect(configs[0].displayName?.name).toBe('ts-async-function-config');
93+
expect(globalConfig.verbose).toBe(true);
94+
});
95+
5796
test('throws if type errors are encountered',()=>{
5897
writeFiles(DIR,{
5998
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
@@ -92,6 +131,44 @@ describe('when `Config` type is imported from "@jest/types"', () => {
92131
expect(exitCode).toBe(1);
93132
});
94133

134+
test('throws if type errors are encountered with CTS config',()=>{
135+
writeFiles(DIR,{
136+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
137+
'jest.config.cts':`
138+
import type {Config} from '@jest/types';
139+
const config: Config.InitialOptions = {testTimeout: '10000'};
140+
export default config;
141+
`,
142+
'package.json':'{}',
143+
});
144+
145+
const{stderr, exitCode}=runJest(DIR);
146+
147+
expect(stderr).toMatch(
148+
"jest.config.cts(2,40): error TS2322: Type 'string' is not assignable to type 'number'.",
149+
);
150+
expect(exitCode).toBe(1);
151+
});
152+
153+
test('throws if syntax errors are encountered with CTS config',()=>{
154+
writeFiles(DIR,{
155+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
156+
'jest.config.cts':`
157+
import type {Config} from '@jest/types';
158+
const config: Config.InitialOptions = {verbose: true};
159+
export default get config;
160+
`,
161+
'package.json':'{}',
162+
});
163+
164+
const{stderr, exitCode}=runJest(DIR);
165+
166+
expect(stderr).toMatch(
167+
"jest.config.cts(3,16): error TS2304: Cannot find name 'get'.",
168+
);
169+
expect(exitCode).toBe(1);
170+
});
171+
95172
test('works with object config exported from TS file when package.json#type=module',()=>{
96173
writeFiles(DIR,{
97174
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
@@ -114,6 +191,45 @@ describe('when `Config` type is imported from "@jest/types"', () => {
114191
writeFiles(DIR,{
115192
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
116193
'jest.config.ts':`
194+
import type {Config} from '@jest/types';
195+
async function getVerbose() {return true;}
196+
export default async (): Promise<Config.InitialOptions> => {
197+
const verbose: Config.InitialOptions['verbose'] = await getVerbose();
198+
return {displayName: 'ts-esm-async-function-config', verbose};
199+
};
200+
`,
201+
'package.json':'{"type": "module"}',
202+
});
203+
204+
const{configs, globalConfig}=getConfig(path.join(DIR));
205+
206+
expect(configs).toHaveLength(1);
207+
expect(configs[0].displayName?.name).toBe('ts-esm-async-function-config');
208+
expect(globalConfig.verbose).toBe(true);
209+
});
210+
211+
test('works with object config exported from CTS file when package.json#type=module',()=>{
212+
writeFiles(DIR,{
213+
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
214+
'jest.config.cts':`
215+
import type {Config} from '@jest/types';
216+
const config: Config.InitialOptions = {displayName: 'ts-esm-object-config', verbose: true};
217+
export default config;
218+
`,
219+
'package.json':'{"type": "module"}',
220+
});
221+
222+
const{configs, globalConfig}=getConfig(path.join(DIR));
223+
224+
expect(configs).toHaveLength(1);
225+
expect(configs[0].displayName?.name).toBe('ts-esm-object-config');
226+
expect(globalConfig.verbose).toBe(true);
227+
});
228+
229+
test('works with function config exported from CTS file when package.json#type=module',()=>{
230+
writeFiles(DIR,{
231+
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
232+
'jest.config.cts':`
117233
import type {Config} from '@jest/types';
118234
async function getVerbose() {return true;}
119235
export default async (): Promise<Config.InitialOptions> => {
@@ -168,6 +284,44 @@ describe('when `Config` type is imported from "@jest/types"', () => {
168284
);
169285
expect(exitCode).toBe(1);
170286
});
287+
288+
test('throws if type errors are encountered when package.json#type=module with CTS config',()=>{
289+
writeFiles(DIR,{
290+
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
291+
'jest.config.cts':`
292+
import type {Config} from '@jest/types';
293+
const config: Config.InitialOptions = {testTimeout: '10000'};
294+
export default config;
295+
`,
296+
'package.json':'{"type": "module"}',
297+
});
298+
299+
const{stderr, exitCode}=runJest(DIR);
300+
301+
expect(stderr).toMatch(
302+
"jest.config.cts(2,40): error TS2322: Type 'string' is not assignable to type 'number'.",
303+
);
304+
expect(exitCode).toBe(1);
305+
});
306+
307+
test('throws if syntax errors are encountered when package.json#type=module with CTS config',()=>{
308+
writeFiles(DIR,{
309+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
310+
'jest.config.cts':`
311+
import type {Config} from '@jest/types';
312+
const config: Config.InitialOptions = {verbose: true};
313+
export default get config;
314+
`,
315+
'package.json':'{"type": "module"}',
316+
});
317+
318+
const{stderr, exitCode}=runJest(DIR);
319+
320+
expect(stderr).toMatch(
321+
"jest.config.cts(3,16): error TS2304: Cannot find name 'get'.",
322+
);
323+
expect(exitCode).toBe(1);
324+
});
171325
});
172326

173327
describe('when `Config` type is imported from "jest"',()=>{
@@ -210,6 +364,45 @@ describe('when `Config` type is imported from "jest"', () => {
210364
expect(globalConfig.verbose).toBe(true);
211365
});
212366

367+
test('with object config exported from CTS file',()=>{
368+
writeFiles(DIR,{
369+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
370+
'jest.config.cts':`
371+
import type {Config} from 'jest';
372+
const config: Config = {displayName: 'ts-object-config', verbose: true};
373+
export default config;
374+
`,
375+
'package.json':'{}',
376+
});
377+
378+
const{configs, globalConfig}=getConfig(path.join(DIR));
379+
380+
expect(configs).toHaveLength(1);
381+
expect(configs[0].displayName?.name).toBe('ts-object-config');
382+
expect(globalConfig.verbose).toBe(true);
383+
});
384+
385+
test('with function config exported from CTS file',()=>{
386+
writeFiles(DIR,{
387+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
388+
'jest.config.cts':`
389+
import type {Config} from 'jest';
390+
async function getVerbose() {return true;}
391+
export default async (): Promise<Config> => {
392+
const verbose: Config['verbose'] = await getVerbose();
393+
return {displayName: 'ts-async-function-config', verbose};
394+
};
395+
`,
396+
'package.json':'{}',
397+
});
398+
399+
const{configs, globalConfig}=getConfig(path.join(DIR));
400+
401+
expect(configs).toHaveLength(1);
402+
expect(configs[0].displayName?.name).toBe('ts-async-function-config');
403+
expect(globalConfig.verbose).toBe(true);
404+
});
405+
213406
test('throws if type errors are encountered',()=>{
214407
writeFiles(DIR,{
215408
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
@@ -248,6 +441,44 @@ describe('when `Config` type is imported from "jest"', () => {
248441
expect(exitCode).toBe(1);
249442
});
250443

444+
test('throws if type errors are encountered with CTS config',()=>{
445+
writeFiles(DIR,{
446+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
447+
'jest.config.cts':`
448+
import type {Config} from 'jest';
449+
const config: Config = {testTimeout: '10000'};
450+
export default config;
451+
`,
452+
'package.json':'{}',
453+
});
454+
455+
const{stderr, exitCode}=runJest(DIR);
456+
457+
expect(stderr).toMatch(
458+
"jest.config.cts(2,25): error TS2322: Type 'string' is not assignable to type 'number'.",
459+
);
460+
expect(exitCode).toBe(1);
461+
});
462+
463+
test('throws if syntax errors are encountered with CTS config',()=>{
464+
writeFiles(DIR,{
465+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
466+
'jest.config.cts':`
467+
import type {Config} from 'jest';
468+
const config: Config = {verbose: true};
469+
export default get config;
470+
`,
471+
'package.json':'{}',
472+
});
473+
474+
const{stderr, exitCode}=runJest(DIR);
475+
476+
expect(stderr).toMatch(
477+
"jest.config.cts(3,16): error TS2304: Cannot find name 'get'.",
478+
);
479+
expect(exitCode).toBe(1);
480+
});
481+
251482
test('works with object config exported from TS file when package.json#type=module',()=>{
252483
writeFiles(DIR,{
253484
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
@@ -287,6 +518,45 @@ describe('when `Config` type is imported from "jest"', () => {
287518
expect(globalConfig.verbose).toBe(true);
288519
});
289520

521+
test('works with object config exported from CTS file when package.json#type=module',()=>{
522+
writeFiles(DIR,{
523+
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
524+
'jest.config.cts':`
525+
import type {Config} from 'jest';
526+
const config: Config = {displayName: 'ts-esm-object-config', verbose: true};
527+
export default config;
528+
`,
529+
'package.json':'{"type": "module"}',
530+
});
531+
532+
const{configs, globalConfig}=getConfig(path.join(DIR));
533+
534+
expect(configs).toHaveLength(1);
535+
expect(configs[0].displayName?.name).toBe('ts-esm-object-config');
536+
expect(globalConfig.verbose).toBe(true);
537+
});
538+
539+
test('works with function config exported from CTS file when package.json#type=module',()=>{
540+
writeFiles(DIR,{
541+
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
542+
'jest.config.cts':`
543+
import type {Config} from 'jest';
544+
async function getVerbose() {return true;}
545+
export default async (): Promise<Config> => {
546+
const verbose: Config['verbose'] = await getVerbose();
547+
return {displayName: 'ts-esm-async-function-config', verbose};
548+
};
549+
`,
550+
'package.json':'{"type": "module"}',
551+
});
552+
553+
const{configs, globalConfig}=getConfig(path.join(DIR));
554+
555+
expect(configs).toHaveLength(1);
556+
expect(configs[0].displayName?.name).toBe('ts-esm-async-function-config');
557+
expect(globalConfig.verbose).toBe(true);
558+
});
559+
290560
test('throws if type errors are encountered when package.json#type=module',()=>{
291561
writeFiles(DIR,{
292562
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
@@ -324,4 +594,42 @@ describe('when `Config` type is imported from "jest"', () => {
324594
);
325595
expect(exitCode).toBe(1);
326596
});
597+
598+
test('throws if type errors are encountered when package.json#type=module with CTS config',()=>{
599+
writeFiles(DIR,{
600+
'__tests__/dummy.test.js':"test('dummy', () => expect(12).toBe(12));",
601+
'jest.config.cts':`
602+
import type {Config} from 'jest';
603+
const config: Config = {testTimeout: '10000'};
604+
export default config;
605+
`,
606+
'package.json':'{"type": "module"}',
607+
});
608+
609+
const{stderr, exitCode}=runJest(DIR);
610+
611+
expect(stderr).toMatch(
612+
"jest.config.cts(2,25): error TS2322: Type 'string' is not assignable to type 'number'.",
613+
);
614+
expect(exitCode).toBe(1);
615+
});
616+
617+
test('throws if syntax errors are encountered when package.json#type=module with CTS config',()=>{
618+
writeFiles(DIR,{
619+
'__tests__/dummy.test.js':"test('dummy', () => expect(123).toBe(123));",
620+
'jest.config.cts':`
621+
import type {Config} from 'jest';
622+
const config: Config = {verbose: true};
623+
export default get config;
624+
`,
625+
'package.json':'{"type": "module"}',
626+
});
627+
628+
const{stderr, exitCode}=runJest(DIR);
629+
630+
expect(stderr).toMatch(
631+
"jest.config.cts(3,16): error TS2304: Cannot find name 'get'.",
632+
);
633+
expect(exitCode).toBe(1);
634+
});
327635
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
exportdefault{};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp