@@ -54,6 +54,45 @@ describe('when `Config` type is imported from "@jest/types"', () => {
54
54
expect ( globalConfig . verbose ) . toBe ( true ) ;
55
55
} ) ;
56
56
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
+
57
96
test ( 'throws if type errors are encountered' , ( ) => {
58
97
writeFiles ( DIR , {
59
98
'__tests__/dummy.test.js' : "test('dummy', () => expect(123).toBe(123));" ,
@@ -92,6 +131,44 @@ describe('when `Config` type is imported from "@jest/types"', () => {
92
131
expect ( exitCode ) . toBe ( 1 ) ;
93
132
} ) ;
94
133
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
+
95
172
test ( 'works with object config exported from TS file when package.json#type=module' , ( ) => {
96
173
writeFiles ( DIR , {
97
174
'__tests__/dummy.test.js' : "test('dummy', () => expect(12).toBe(12));" ,
@@ -114,6 +191,45 @@ describe('when `Config` type is imported from "@jest/types"', () => {
114
191
writeFiles ( DIR , {
115
192
'__tests__/dummy.test.js' : "test('dummy', () => expect(12).toBe(12));" ,
116
193
'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' : `
117
233
import type {Config} from '@jest/types';
118
234
async function getVerbose() {return true;}
119
235
export default async (): Promise<Config.InitialOptions> => {
@@ -168,6 +284,44 @@ describe('when `Config` type is imported from "@jest/types"', () => {
168
284
) ;
169
285
expect ( exitCode ) . toBe ( 1 ) ;
170
286
} ) ;
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
+ } ) ;
171
325
} ) ;
172
326
173
327
describe ( 'when `Config` type is imported from "jest"' , ( ) => {
@@ -210,6 +364,45 @@ describe('when `Config` type is imported from "jest"', () => {
210
364
expect ( globalConfig . verbose ) . toBe ( true ) ;
211
365
} ) ;
212
366
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
+
213
406
test ( 'throws if type errors are encountered' , ( ) => {
214
407
writeFiles ( DIR , {
215
408
'__tests__/dummy.test.js' : "test('dummy', () => expect(123).toBe(123));" ,
@@ -248,6 +441,44 @@ describe('when `Config` type is imported from "jest"', () => {
248
441
expect ( exitCode ) . toBe ( 1 ) ;
249
442
} ) ;
250
443
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
+
251
482
test ( 'works with object config exported from TS file when package.json#type=module' , ( ) => {
252
483
writeFiles ( DIR , {
253
484
'__tests__/dummy.test.js' : "test('dummy', () => expect(12).toBe(12));" ,
@@ -287,6 +518,45 @@ describe('when `Config` type is imported from "jest"', () => {
287
518
expect ( globalConfig . verbose ) . toBe ( true ) ;
288
519
} ) ;
289
520
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
+
290
560
test ( 'throws if type errors are encountered when package.json#type=module' , ( ) => {
291
561
writeFiles ( DIR , {
292
562
'__tests__/dummy.test.js' : "test('dummy', () => expect(12).toBe(12));" ,
@@ -324,4 +594,42 @@ describe('when `Config` type is imported from "jest"', () => {
324
594
) ;
325
595
expect ( exitCode ) . toBe ( 1 ) ;
326
596
} ) ;
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
+ } ) ;
327
635
} ) ;
0 commit comments