Skip to content

Support Testing with imports of JS files in ESM #187

@daraclare

Description

@daraclare

Current Behavior

When running test files with that contain ES6 module imports with Jest, I get the following error:

Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

Is there an existing recommendation for handling ES6/ESNext module imports, if so, could we add documentation around this? If it doesn't exist, could we add recommendation on how to handle it? I'm happy to help if I can. Could this be a TSDX feature, to switch on and off?

Desired Behavior

Either handled by default by TSDX, or there is clear recommendations on how best to handle this.

Suggested Solution

Happy to add docs with the recommendation to resolve, although I'm not sure what the recommendations are as yet.

Who does this impact? Who is this for?

Users importing other packages that use ES6.

Describe alternatives you've considered

I considered using babel-jest and configuring it myself, but this seems to make TSDX a little redundant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions