aws-sdk-client-mock
aws-sdk-client-mock copied to clipboard
aws-sdk-client-mock-jest 4.1.0 package.json exports causes "SyntaxError: Cannot use import statement outside a module" when using es modules
Checklist
- [X] I have read Caveats documentation and didn't find a solution for this problem there.
Bug description
My jest tests were fine with [email protected] but as soon as I upgrade to 4.1.0 I am getting
Details:
/Users/s0046755/projects/adam-assumptions-model-inputs/node_modules/aws-sdk-client-mock-jest/dist/es/jest.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { __assign, __spreadArray } from "tslib";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
I am running in Node.js 20.17.0 and for jest I run it with NODE_OPTIONS=--experimental-vm-modules jest
so I can use es modules.
Problem is resolved if package.json exports is removed
I was able to diff the aws-sdk-client-mock-jest package.json of 4.0.2 and 4.1.0 and found that the newer package.json includes an exports property. It appears that the inclusion of this is not playing well with jest causing the above error. If I manually remove the exports property from your package.json in node_modules/aws-sdk-client-mock-json then things once again work fine. So there must be some incompatibility with jest and the exports property.
Apparently the exports property was added to support the vitest feature.
Reproduction
Gist with all necessary code to reproduce the problem https://gist.github.com/jeffbski-rga/97f15fbffb23aa7f91484da49aa7d21d
package.json
{
"name": "aws-sdk-client-mock-jest-4.1.0-bug",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest \"--testMatch=**/*.mjs\" index.test.mjs"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk-client-mock": "4.1.0"
},
"devDependencies": {
"aws-sdk-client-mock-jest": "4.1.0",
"jest": "^29.7.0"
}
}
index.test.mjs
import 'aws-sdk-client-mock-jest';
Reproduce in Node.js 20.17.0 by running npm t
Simply attempting to import the module will cause the error.
Environment
- Node version: 20.17.0
- Testing lib and version: jest
- Typescript version:
- AWS SDK v3 Client mock version: 4.1.0
Solution
Removing the exports property from the aws-sdk-client-mock-jest package.json fixes the problem.
The exports in 4.1.0 package.json (remove this to fix, but it may affect vitest feature since it was added for that)
"exports": {
".": {
"require": {
"types": "./dist/types/jest.d.ts",
"default": "./dist/cjs/jest.js"
},
"import": {
"types": "./dist/types/jest.d.ts",
"default": "./dist/es/jest.js"
}
},
"./vitest": {
"require": {
"types": "./dist/types/vitest.d.ts",
"default": "./dist/cjs/vitest.js"
},
"import": {
"types": "./dist/types/vitest.d.ts",
"default": "./dist/es/vitest.js"
}
}
},
4.0.2 did not have exports and it works properly.