test: Workaround for Jest ESM issues

To prevent issues with Jest and ESM
we transform all ESM dependencies to CJS while testing.
This commit is contained in:
Joachim Van Herwegen 2023-06-16 14:48:33 +02:00
parent b3ef4ed017
commit bfa70a40aa
3 changed files with 671 additions and 445 deletions

View File

@ -25,6 +25,24 @@ function jestGithubRunnerSpecs() {
};
}
// ESM libraries that need to be transformed so Jest can handle them
const esModules = [
'oidc-provider',
'nanoid',
'got',
'quick-lru',
'@sindresorhus/is',
'p-cancelable',
'@szmarczak/http-timer',
'cacheable-request',
'normalize-url',
'responselike',
'lowercase-keys',
'mimic-response',
'form-data-encoder',
'cacheable-lookup',
];
module.exports = {
transform: {
'^.+\\.ts$': [ 'ts-jest', {
@ -32,7 +50,11 @@ module.exports = {
diagnostics: false,
isolatedModules: true,
}],
// This transformer converts ESM packages to CJS
'^.+node_modules.+\\.js$': 'jest-esm-transformer-2',
},
// By default, node_modules are not transformed, but we want to transform the ESM packages
transformIgnorePatterns: [`/node_modules/(?!(${esModules.join('|')})/)`],
testRegex: '/test/(unit|integration)/.*\\.test\\.ts$',
moduleFileExtensions: [
'ts',

1093
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -185,6 +185,7 @@
"eslint-plugin-unused-imports": "^2.0.0",
"husky": "^4.3.8",
"jest": "^29.5.0",
"jest-esm-transformer-2": "^1.0.0",
"jest-rdf": "^1.8.0",
"markdownlint-cli2": "^0.7.1",
"node-mocks-http": "^1.12.2",