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

Jest encountered an unexpected token export with the adapter #179

Open
tleunen opened this issue Apr 26, 2022 · 9 comments
Open

Jest encountered an unexpected token export with the adapter #179

tleunen opened this issue Apr 26, 2022 · 9 comments

Comments

@tleunen
Copy link

tleunen commented Apr 26, 2022

I'm trying to set up a new project, and simply adding these give me an error:

import { configure } from "enzyme";
import Adapter from "enzyme-adapter-preact-pure";

configure({ adapter: new Adapter() });
SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1773:14)
      at Object.<anonymous> (node_modules/enzyme-adapter-preact-pure/build-cjs/src/Adapter.js:8:18)

In the details of the error, it looks like it might be trying to parse the preact.module.js file?

Details:

    /projects/x/node_modules/preact/dist/preact.module.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){var n,l,u,i,t,o,r,f={}....

Not sure exactly what's going on. Am I missing an obvious option?

@robertknight
Copy link
Member

SyntaxError: Unexpected token 'export'

This error suggests that Jest is trying to parse an ES module as if it were a CommonJS module. It is loading the CommonJS build of enzyme-adapter-preact-pure (see build-cjs/ in the path) but the ES module build of Preact (preact.module.js).

What I would normally expect to happen is that a bundler / environment either uses the ES module build of both, if it supports that, or the CommonJS build of both.

Can you provide the details needed to recreate your project setup?

@tleunen
Copy link
Author

tleunen commented Apr 27, 2022

Hmm... I tried a couple things, I initially used @swc/jest and I tried with the jest-preset-preact to see if I had something wrong with my config, but both give me the same error on some of preact files.

Any reason why jest would load the es module version of preact instead of cjs? (Using Node 16, my swc/jest config has module: { type: "commonjs" }. My tsconfig has "module": "esnext",, but even setting it to commonjs doesn't make any changes)

@robertknight
Copy link
Member

Any reason why jest would load the es module version of preact instead of cjs?

Can you import functions from the preact package directly, as opposed to the indirect imports that happen when you import this package? What happens if you simplify the system (eg. to take swc or TypeScript out of the picture temporarily)?

@tleunen
Copy link
Author

tleunen commented May 2, 2022

I believe there's a misunderstanding. I'm simply trying to do the initial setup to add the preact adapter for enzyme. Tests are importing preact directly.

After a bit more try and error, starting from scratch again, and removing all complexity (all tests and typescript) , it seems the error arises as soon as I set jest to run in a jsdom environment. testEnvironment: "jsdom"

@tleunen
Copy link
Author

tleunen commented May 2, 2022

After rollback to jest 27, it seems the error is gone. So might be a problem with Jest 28, or somewhere in between Jest and this adapter?
I don't have any problem between Jest 28 and the React adapter.

@shinxi
Copy link

shinxi commented May 17, 2022

Same here, met exactly the same issue after upgrading to jest 28, and have to downgrade back to 27 to make the original tests work...

@robertknight
Copy link
Member

If someone could create a gist with the minimal set of files needed to reproduce the problem in a project, that would be helpful. I'm not currently using Jest together with Preact in any projects, so this would save some debugging time.

@rschristian
Copy link
Member

Might be related to jestjs/jest#11924

Sounds like Jest is consuming the browser build of Preact while applying Node extension semantics to it.

@TimvdLippe
Copy link

After a lot of fiddling, I was able to fix it based on ai/nanoid#363 (comment)

Add the following to moduleNameMapper in your jest.config.js:

"^preact(/(.*)|$)": "preact$1"

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

5 participants