Repository navigation
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already include this feature request, without success.
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
Related issues:
- Changes to extension ordering in moduleFileExtensions breaks jest tests #2608
- SyntaxError: Cannot use import statement outside a module #2178
I'm willing to work on a PR for this - looks like it shouldn't be too hard.
Activity
rwaskiewicz commentedon Feb 2, 2022
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!
johncrim commentedon Feb 3, 2022
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).
johncrim commentedon Mar 6, 2022
Hi @rwaskiewicz - I have a working PR that works great for our projects. Lmk when you have time to review.
rwaskiewicz commentedon Mar 10, 2022
Hey @johncrim - I haven't forgotten about this, I'll circle back as soon as I get a chance
subasically commentedon Jun 24, 2022
any updates on this?
johncrim commentedon Sep 1, 2022
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.
rwaskiewicz commentedon Sep 6, 2022
@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
ryuran commentedon Sep 29, 2022
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.
Current workaround, add a transform option in jest config to transform concerned modules from ESM to CommonJS using
babel-jestcam-narzt commentedon Apr 23, 2023
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.
EduardoFelipeLogcomex commentedon Jun 26, 2023
this adjustment would be very important for the project I've been developing!
3 remaining items
TheKonka commentedon Jan 25, 2024
@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?
pfteter commentedon May 10, 2024
Any updates on this, should be quite important ?
@rwaskiewicz @christian-bromann
christian-bromann commentedon May 10, 2024
@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.
christian-bromann commentedon Jun 26, 2024
@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.dellfort commentedon Dec 3, 2024
christian-bromann commentedon Dec 6, 2024
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.
johncrim commentedon Jun 13, 2025
@christian-bromann - Jest 30 is out: https://jestjs.io/blog/2025/06/04/jest-30
Do you expect this will be worked on?
christian-bromann commentedon Jun 13, 2025
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.
johncrim commentedon Jun 14, 2025
@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.
christian-bromann commentedon Jun 17, 2025
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.
pfteter commentedon Jun 18, 2025
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
christian-bromann commentedon Jun 18, 2025
@pfteter thanks for your feedback!
Yes, definitely prefer Vitest over Jest these days.
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.
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.
christian-bromann commentedon Jun 24, 2025
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.