preact-testing-library icon indicating copy to clipboard operation
preact-testing-library copied to clipboard

"SyntaxError: Cannot use import statement outside a module" running jest

Open wuifdesign opened this issue 2 years ago • 3 comments

  • @testing-library/preact version: 3.2.3
  • preact version: 10.13.2
  • node version: 18.14.0
  • npm version: 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']
  }
}

wuifdesign avatar May 09 '23 11:05 wuifdesign

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.

akselinurmio avatar May 23 '23 08:05 akselinurmio

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.

rmannibucau avatar Nov 17 '23 14:11 rmannibucau

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?

brianecook avatar May 13 '24 19:05 brianecook