Skip to content

feat: Support ES modules in jest tests #3230

Closed

Description

@johncrim

Prerequisites

Describe the Feature Request

Stencil tests should be able to run using Jest ES Module support, instead of transpiling all ESM modules to CJS. This needs to be done to improve test perf, and because ESM is preferred in all modern projects that are used in browsers.

Right now, stencil jest support hard-codes CJS output: https://github.com/ionic-team/stencil/blob/main/src/testing/test-transpile.ts

Describe the Use Case

When I run stencil tests, I should have the option to run them in Jest ESM mode.

The lack of this feature causes bugs and wasted dev effort, eg: #2178

Performance should be improved b/c .mjs files (most dependencies) will no longer need to be transpiled. Dev cycle speed is important!

In addition, there are behavioral differences between CJS and ESM, and it's valuable for the test environment to be as similar as possible to the runtime environment.

Describe Preferred Solution

A config option exists for running tests in Jest ESM mode.

Describe Alternatives

No response

Related Code

No response

Additional Information

https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support/

Related issues:

I'm willing to work on a PR for this - looks like it shouldn't be too hard.

Activity

  1. rwaskiewicz commented on Feb 2, 2022

    @rwaskiewicz
    Member

    Hey @johncrim 👋

    I agree that Stencil should support ES Modules when using Jest. To be honest, I had not been considering this support until Jest's ESM support came out of it's 'experimental' phase. But if you're interested, I say go for it!

  2. added
    Resolution: Needs Investigation
    on Feb 2, 2022
  3. johncrim commented on Feb 3, 2022

    @johncrim
    Author

    Thanks @rwaskiewicz . I certainly understand waiting until the experimental phase ends - I can't fault you for that, though I don't know how long it will remain in experimental.

    It's working acceptably with ts-jest and jest + angular (though still a little bumpy). I just migrated our angular jest tests to Jest ESM and would like to tackle this as long as it's not too involved (from a code review it looks pretty straightforward, but I could certainly be wrong about that).

  4. johncrim commented on Mar 6, 2022

    @johncrim
    Author

    Hi @rwaskiewicz - I have a working PR that works great for our projects. Lmk when you have time to review.

  5. rwaskiewicz commented on Mar 10, 2022

    @rwaskiewicz
    Member

    Hey @johncrim - I haven't forgotten about this, I'll circle back as soon as I get a chance

  6. subasically commented on Jun 24, 2022

    @subasically

    any updates on this?

  7. johncrim commented on Sep 1, 2022

    @johncrim
    Author

    Hi @rwaskiewicz - the PR may be outdated (I don't know what has changed since I created it), but would be great to get either my PR merged, or this fixed some other way.

  8. rwaskiewicz commented on Sep 6, 2022

    @rwaskiewicz
    Member

    @johncrim

    Agreed - I've done a little work internally to make Jest easier to upgrade (and easier to change things like ESM support). I'm hoping to move to the prototyping stage later this week, from there I hope to be able to be in a better place to determine how we can work with this PR

  9. ryuran commented on Sep 29, 2022

    @ryuran

    I'm blocked by this situation because one of my component use swiper available only as esm in last version and it make fail the related spec test.

       FAIL  src/components/carousel/carousel.spec.ts
        ● Test suite failed to run
      
          Jest encountered an unexpected token
      
          Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
      
          Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
      
          By default "node_modules" folder is ignored by transformers.
      
          Here's what you can do:
           • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
           • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
           • 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/configuration
          For information about custom transformations, see:
          https://jestjs.io/docs/code-transformation
      
          Details:
      
          /root/actions-runner/_work/----/----/node_modules/swiper/swiper.esm.js:13
          export { default as Swiper, default } from './core/core.js';
          ^^^^^^
      
          SyntaxError: Unexpected token 'export'
      
            12 |   forceUpdate,
            13 | } from '@stencil/core';
          > 14 | import { Swiper, SwiperOptions } from 'swiper';
               | ^
            15 |
            16 | import { defaultSwiperOptions } from './carousel.option.defaults';
            17 |
      
            at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1728:14)
            at Object.<anonymous> (src/components/carousel/carousel.tsx:14:1)
    

    Current workaround, add a transform option in jest config to transform concerned modules from ESM to CommonJS using babel-jest

  10. cam-narzt commented on Apr 23, 2023

    @cam-narzt
    Contributor

    The old workaround of editing the jest config doesn't work since stencil 2.14.0 when jest's config file sterted to be ignored (per: #3251) so fixing this is pretty important since there's no workaround anymore.

  11. EduardoFelipeLogcomex commented on Jun 26, 2023

    @EduardoFelipeLogcomex

    this adjustment would be very important for the project I've been developing!

  12. 3 remaining items

  13. TheKonka commented on Jan 25, 2024

    @TheKonka

    @ryuran Hello! I have the same problem using [email protected] and [email protected]. I've tried all the solutions I've found, but none of them work, can you share your config code for this?

  14. pfteter commented on May 10, 2024

    @pfteter
    Contributor

    Any updates on this, should be quite important ?

    @rwaskiewicz @christian-bromann

  15. christian-bromann commented on May 10, 2024

    @christian-bromann
    Member

    @pfteter we have been de-prioritizing Stencils Jest integration and worked on better alternatives, e.g. via WebdriverIO or Playwright. At this point I can't tell when this will be picked up. Maintaining this Jest integration adds a lot of work to the team with little value in return. In an ideal world we would want to have users use Jest directly and ESM should work out of the box. I may look into this in one of our next Jedi days.

  16. christian-bromann commented on Jun 26, 2024

    @christian-bromann
    Member

    Any updates on this, should be quite important ?

    @pfteter with the release of Jest v30 we will be working on a Jest preset plugin (e.g. @stencil/jest) that you can use moving forward. Keep an eye on this issue for updates.

  17. dellfort commented on Dec 3, 2024

    @dellfort
  18. christian-bromann commented on Dec 6, 2024

    @christian-bromann
    Member

    Please don't comment with "any updates on this", especially not until Jest v30 actually has been released that would allow to make progress on this. In general I would recommend to use a better test runner for testing Stencil components. There are two great alternatives in Playwright and WebdriverIO that allow you to load ESM modules.

  19. johncrim commented on Jun 13, 2025

    @johncrim
    Author
  20. christian-bromann commented on Jun 13, 2025

    @christian-bromann
    Member

    Do you expect this will be worked on?

    Contributions are much appreciated 😉 To set expectations clear: Stencil is openly governed at this point, see our latest blog post. This means that the community is invited to propose bug fixes or send enhancements where needed.

  21. johncrim commented on Jun 14, 2025

    @johncrim
    Author

    @christian-bromann - thanks for pointing me to the blog post, I hadn't seen it. I think that's a good call.

    I provided a fix for this in 2022 that worked well:
    #3256

    A lot has changed since then, so I'll consider whether it's a good use of my time to update it. Also, stencil testing isn't working with jest 30, I'll create a separate bug for that.

  22. christian-bromann commented on Jun 17, 2025

    @christian-bromann
    Member

    We will likely remove Stencil testing from core in v5 in exchange for creating a Jest environment that is based on JSDOM and supports rendering Stencil components.

  23. pfteter commented on Jun 18, 2025

    @pfteter
    Contributor

    Hi @christian-bromann,

    Have you considered using Vite/Vitest? It supports JSDOM, is jest compatible and also allows running tests in a real browser. Plus, it integrates seamlessly with Vite for code instrumentation. Even Playwright can be used this way—eliminating the need for a separate Playwright project.
    In the last few months I got the opinion that JEST is outdated / deprecated and shouldn't be used anymore since vitest solves a lot of problems that JEST has (hmr, esm, integration, pluggability, scalability / memory issues , ... )

    In my opinion, one of the biggest architectural challenges in Stencil is the custom builder. I know you've been involved in that area.

    The great thing about Vitest is that it's pluggable and works well with Jest. Since it integrates with Vite, you don’t need to handle code instrumentation manually (though it’s also possible via Rollup with the Stencil custom builder).

    That said, I understand this would be a significant change for v5. Just my two cents—but I believe moving to a pluggable builder would make Stencil much more maintainable. The technology is already there; the challenge is making it stable.

    Having said that I don't see a single reason to proceed with JEST except maturity and support for jest codebases

  24. christian-bromann commented on Jun 18, 2025

    @christian-bromann
    Member

    @pfteter thanks for your feedback!

    Have you considered using Vite/Vitest?

    Yes, definitely prefer Vitest over Jest these days.

    In my opinion, one of the biggest architectural challenges in Stencil is the custom builder. I know you've been involved in that area.

    If I could re-design Stencil I would definitely build it as simple Rolldown plugin. Unfortunately this is a larger undertaking and requires resources that I don't have at the moment.

    Having said that I don't see a single reason to proceed with JEST except maturity and support for jest codebases

    The only reason I see is to allow users with large Jest test suites to seamlessly migrate to v5. It should be not a big effort to enable Jest support for Stencil as a plugin.

  25. christian-bromann commented on Jun 24, 2025

    @christian-bromann
    Member

    Support for Jest v30+ is now available via the jest-stencil-runner. Please give it a try and let me know if it works for you. Feel free to raise issue or submit PRs with improvements.

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

    Help WantedResolution: Needs Investigation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions