@@ -3,8 +3,11 @@ import path from 'path'
33
44import { LogLevels } from 'bs-logger'
55import { removeSync } from 'fs-extra'
6+ import ts from 'typescript'
67
8+ import { dedent , omitLeadingWhitespace } from '../__helpers__/dedent-string'
79import { logTargetMock } from '../__helpers__/mocks'
10+ import type { TsJestTransformOptions } from '../types'
811import { importer } from '../utils/importer'
912
1013import { TsJestCompiler } from './compiler'
@@ -14,7 +17,7 @@ const SOURCE_MAPPING_PREFIX = 'sourceMappingURL='
1417
1518const logTarget = logTargetMock ( )
1619const cacheDir = path . join ( process . cwd ( ) , 'tmp' )
17- const baseTransformOptions = {
20+ const baseTransformOptions : TsJestTransformOptions = {
1821 config : {
1922 testMatch : [ ] ,
2023 testRegex : [ ] ,
@@ -298,34 +301,6 @@ describe('TsJestTransformer', () => {
298301 } )
299302 } )
300303
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-
329304 test ( 'should allow detection of ts-jest' , ( ) => {
330305 expect ( process . env . TS_JEST ) . toBe ( '1' )
331306 } )
@@ -432,6 +407,81 @@ describe('TsJestTransformer', () => {
432407
433408 delete process . env . TS_JEST_HOOKS
434409 } )
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+ } )
435485 } )
436486
437487 describe ( 'processAsync' , ( ) => {
0 commit comments