@@ -3,8 +3,11 @@ import path from 'path'
3
3
4
4
import { LogLevels } from 'bs-logger'
5
5
import { removeSync } from 'fs-extra'
6
+ import ts from 'typescript'
6
7
8
+ import { dedent , omitLeadingWhitespace } from '../__helpers__/dedent-string'
7
9
import { logTargetMock } from '../__helpers__/mocks'
10
+ import type { TsJestTransformOptions } from '../types'
8
11
import { importer } from '../utils/importer'
9
12
10
13
import { TsJestCompiler } from './compiler'
@@ -14,7 +17,7 @@ const SOURCE_MAPPING_PREFIX = 'sourceMappingURL='
14
17
15
18
const logTarget = logTargetMock ( )
16
19
const cacheDir = path . join ( process . cwd ( ) , 'tmp' )
17
- const baseTransformOptions = {
20
+ const baseTransformOptions : TsJestTransformOptions = {
18
21
config : {
19
22
testMatch : [ ] ,
20
23
testRegex : [ ] ,
@@ -298,34 +301,6 @@ describe('TsJestTransformer', () => {
298
301
} )
299
302
} )
300
303
301
- test ( 'should process js file with allowJs false and show warning log' , ( ) => {
302
- const fileContent = 'const foo = 1'
303
- const filePath = 'foo.js'
304
- const transformOptions = {
305
- ...baseTransformOptions ,
306
- config : {
307
- ...baseTransformOptions . config ,
308
- globals : {
309
- 'ts-jest' : { tsconfig : { allowJs : false } } ,
310
- } ,
311
- } ,
312
- }
313
- tr . getCacheKey ( fileContent , filePath , transformOptions )
314
- logTarget . clear ( )
315
-
316
- const result = tr . process ( fileContent , filePath , transformOptions )
317
-
318
- expect ( result ) . toEqual ( {
319
- code : fileContent ,
320
- } )
321
- expect ( logTarget . lines [ 1 ] . substring ( 0 ) ) . toMatchInlineSnapshot ( `
322
- "[level:40] Got a \`.js\` file to compile while \`allowJs\` option is not set to \`true\` (file: foo.js). To fix this:
323
- - if you want TypeScript to process JS files, set \`allowJs\` to \`true\` in your TypeScript config (usually tsconfig.json)
324
- - if you do not want TypeScript to process your \`.js\` files, in your Jest config change the \`transform\` key which value is \`ts-jest\` so that it does not match \`.js\` files anymore
325
- "
326
- ` )
327
- } )
328
-
329
304
test ( 'should allow detection of ts-jest' , ( ) => {
330
305
expect ( process . env . TS_JEST ) . toBe ( '1' )
331
306
} )
@@ -432,6 +407,81 @@ describe('TsJestTransformer', () => {
432
407
433
408
delete process . env . TS_JEST_HOOKS
434
409
} )
410
+
411
+ it . each ( [
412
+ {
413
+ filePath : 'my-project/node_modules/foo.js' ,
414
+ fileContent : `
415
+ function foo() {
416
+ return 1
417
+ }
418
+
419
+ export default foo;
420
+ ` ,
421
+ } ,
422
+ {
423
+ filePath : 'my-project/node_modules/foo.mjs' ,
424
+ fileContent : `
425
+ function foo() {
426
+ return 1
427
+ }
428
+
429
+ export default foo;
430
+ ` ,
431
+ } ,
432
+ ] ) ( 'should transpile js file from node_modules for CJS' , ( { filePath, fileContent } ) => {
433
+ const result = tr . process ( fileContent , filePath , baseTransformOptions )
434
+
435
+ expect ( omitLeadingWhitespace ( result . code ) ) . toContain ( dedent `
436
+ exports.default = foo;
437
+ ` )
438
+ } )
439
+
440
+ it ( 'should transpile js file from node_modules for ESM' , ( ) => {
441
+ const result = tr . process (
442
+ `
443
+ function foo() {
444
+ return 1
445
+ }
446
+
447
+ module.exports = foo;
448
+ ` ,
449
+ 'my-project/node_modules/foo.js' ,
450
+ {
451
+ ...baseTransformOptions ,
452
+ supportsStaticESM : true ,
453
+ transformerConfig : {
454
+ useESM : true ,
455
+ tsconfig : {
456
+ module : 'ESNext' ,
457
+ target : 'ESNext' ,
458
+ } ,
459
+ } ,
460
+ } ,
461
+ )
462
+
463
+ const transpileResult = ts . transpileModule (
464
+ `
465
+ function foo() {
466
+ return 1
467
+ }
468
+
469
+ module.exports = foo;
470
+ ` ,
471
+ {
472
+ compilerOptions : {
473
+ module : ts . ModuleKind . ESNext , // Transpile to ESM
474
+ target : ts . ScriptTarget . ESNext ,
475
+ } ,
476
+ } ,
477
+ )
478
+
479
+ console . log ( transpileResult . outputText )
480
+
481
+ expect ( omitLeadingWhitespace ( result . code ) ) . toContain ( dedent `
482
+ module.exports = foo;
483
+ ` )
484
+ } )
435
485
} )
436
486
437
487
describe ( 'processAsync' , ( ) => {
0 commit comments