Skip to content

Changes to extension ordering in moduleFileExtensions breaks jest tests #2608

Closed

Description

@glenveegee

Stencil version:

I'm submitting a:

[x ] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior:

I have a stencil component that imports a npm package that has mixed ESM and UMD files in the library. My stencil components builds and runs fine but Jest testing fails because the .mjs file extensions are preferred over the .js file extensions without transforming them. This results in errors like:

Test suite failed to run

    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. 

    Details:

    ......./node_modules/graphql/language/parser.mjs:1
    import inspect from '../jsutils/inspect';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

Expected behavior:

Jest tests should pass

Steps to reproduce:

Test any component that imports from graphql package or any package that imports from a npm package with mixed ESM and UMD files

Related code:

This started appearing following the commits in
This commit 7 days ago..
781180d

reordered the moduleFileExtensions from

['ts', 'tsx', 'js', 'mjs', 'jsx', 'json', 'd.ts']

to

['ts', 'tsx', 'mjs', 'js', 'jsx']

NOTE mjs now precedes js

Other information:

WORKAROUND

To work around this issue you can override the ordering by setting the moduleFileExtensions in stencil.config.ts to...

testing: {
    ...
    moduleFileExtensions: [
      'ts',
      'tsx',
      'js',
      'mjs',
      'jsx',
      'json',
      'd.ts'
    ]
  },

Activity

  1. glenveegee commented on Jul 31, 2020

    @glenveegee
    Author

    I should add that there's nothing wrong preferring mjs over js but then the appropriate and necessary changes also have to be made to jest config such that jest can natively support es modules which it is only now starting to address. See jestjs/jest#9430

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions