Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wuifdesign opened this issue May 9, 2023 · 3 comments

Comments

@wuifdesign
Copy link

wuifdesign commented May 9, 2023

  • @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']
  }
}
@akselinurmio
Copy link

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.

@rmannibucau
Copy link

rmannibucau commented Nov 17, 2023

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.

@brianecook
Copy link

brianecook commented May 13, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants