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

Missing documentation using jest with repos that are "type":"module" ("Cannot use import statement outside a module") #10380

Closed
Pomax opened this issue Aug 7, 2020 · 6 comments

Comments

@Pomax
Copy link

Pomax commented Aug 7, 2020

Running jest in a repo with a package.json that clearly indicates "type": "module", with test code that also follows esmodule format, causes Jest to error out:

// saved as ./test/main.test.js
import { Thing } from "../lib/thing.js";

test('trivial truth', () => {
    expect(true).toBeTrue();
});

results in:

    [...]

    /yourproject/test/main.test.js:1
    import { Thing } from "../lib/thing.js";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

Which doesn't make a lot of sense: the project is clearly marked as being modern module-based code, Node 14 has zero problems with running any of the code, but Jest does not seem to "see" that we're working in a modern codebase rather than a CJS codebase.

I can't find anything in the docs that explains how to tell Jest that everything is module code, and that it should run as such. I don't see anything that explains which runtime flag is required (e.g. npx jest --esmodule?) or which explicit config setting is necessary to make this work (also of course I hope this doesn't require writing an explicit conf file, because zero conf is the best conf).

If this is in the docs, then it might be undiscoverable: searching for "module" or "esmodule" on https://jestjs.io/docs/en/ only finds sections relating to mocks.

If it's not in the docs, but Jest should be able to deal with this, then that should probably get added to the docs.

If it's not in the docs, and Jest can't deal with this, then that should definitely be added to the docs, as new JS projects in 2020 don't need ES5 conversion anymore, except for legacy support, so you'd expect Jest to be deal with test files that are modules as fine as Node itself =)

@urugator
Copy link

urugator commented Aug 8, 2020

You have to run node --experimental-vm-modules node_modules/jest/bin/jest.js as explained in #9430
I agree the docs could at least mention it's WIP and provide the link to the issue.

@Pomax
Copy link
Author

Pomax commented Aug 9, 2020

Thanks, that's a useful link, but indeed: the issue is mostly about getting that documented so that people searching the Jest docs can actually get an answer to "can I used this and if so, how?".

@SimenB
Copy link
Member

SimenB commented Aug 10, 2020

It's briefly mentioned in the blog posts (https://jestjs.io/blog/2020/05/05/jest-26#native-esm-support), but I don't think that shows up in search. PR welcome adding a note that's it WIP and experimental 🙂

@Pomax Pomax changed the title Missing documentaiton using jest with repos that are "type":"module" ("Cannot use import statement outside a module") Missing documentation using jest with repos that are "type":"module" ("Cannot use import statement outside a module") Aug 10, 2020
@Pomax
Copy link
Author

Pomax commented Aug 10, 2020

I'd rather someone who actually knows the docs writes that PR so it's in the right place in a way that's easily discoverable. I have way too many things to do to first learn a documentation codebase I'm afraid.

@SimenB
Copy link
Member

SimenB commented Oct 11, 2020

Somewhat fixed via #10611, although it's very much incomplete

@SimenB SimenB closed this as completed Oct 11, 2020
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants