jest-preset-angular icon indicating copy to clipboard operation
jest-preset-angular copied to clipboard

[Bug]: It is not work After config useESM: true

Open zhouyali opened this issue 1 year ago • 2 comments

Version

12.2.3

Steps to reproduce

`transform: { '^.+\.(ts|js|mjs|html|svg)$': [ 'jest-preset-angular', { useESM: true, }, ], },

transformIgnorePatterns: [
	'<rootDir>/node_modules/(?!(.*\\.mjs$|tslib|@elf|lit-elemen|lit-html|tr-grid-util|tr-grid-titlewrap|d3-color|uuid))',
],`

 another try
 

jest.mock('./miscellaneous/uuid', () => { return { v4: jest.fn(() => 1) }; });

1 2 3 4

Expected behavior

I expect the uuid on transformIgnorePatterns will be compile successfully

Actual behavior

Test suite failed to run

RangeError: Maximum call stack size exceeded.

Additional context

it is a internal project, so it's not allowed to post repo here, the transform and transformIgnorePatterns config work for other es module package, but not work for all esm.

Environment

System :
Windows64
Env:
"jest-preset-angular": "^12.2.3", 
"jest": "^28.1.3",
"@angular/core": "^14.2.12"

zhouyali avatar Jan 23 '24 05:01 zhouyali

You can try to use https://github.com/thymikee/jest-preset-angular/tree/main/examples to create a small reproduce case, that would help a lot

ahnpnl avatar Jan 23 '24 08:01 ahnpnl

try moduleMapper '^uuid$': require.resolve('uuid'), instead of adding to transformignore

Delagen avatar Jan 26 '24 21:01 Delagen

thanks @Delagen , @zhouyali besides, you can also put the configuration like this in your Jest config

{
   //....
   moduleNameMapper: {
       uuid: '<rootDir>/node_modules/uuid/dist/index.js',
   }
}

The error you got which was caused by default Jest module resolution which looks for the default field inside exports of package.json image

However, because Jest runs with JSDOM, which is entirely in NodeJs environment therefore the file under esm-browser can't be used. We need to tell Jest to load the file from node export instead.

ahnpnl avatar May 10 '24 03:05 ahnpnl