"SyntaxError: Cannot use import statement outside a module" running jest
-
@testing-library/preactversion:3.2.3 -
preactversion:10.13.2 -
nodeversion:18.14.0 -
npmversion:9.3.1
What happened:
Starting with v3.1.0 i have problems running my tests with jest.
When running jest i get following error:
.../node_modules/@testing-library/preact/dist/esm/index.mjs:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { cleanup } from "./pure.mjs";
^^^^^^
SyntaxError: Cannot use import statement outside a module
// jest.config.js
const config = {
testEnvironment: 'jsdom',
transform: {
'\\.[jt]sx?$': ['@swc/jest']
}
}
We’re getting this same error while running our tests. Tests pass when using version 2.0.1 of this package, but fail in 3.0.0.
Hi, seems it comes from the package.json which now contains:
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"browser": "./dist/esm/index.mjs",
it should be "browser": "./dist/cjs/index.js",
not sure the change was intended.
workaround: '^@testing-library/preact$': '<rootDir>/../../node_modules/@testing-library/preact/dist/cjs/index.js', in jest moduleNameMapper (i'm using lerna so ../.. to use root node_modules, you can need to adjust it.
Any update here? This seems to completely block me from using the preact testing-library. Since this ticket is relatively old, I'm wondering if this library is not well-supported? If so, can someone point me to a better tool to use for testing preact components?