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

[DO NOT MERGE] feeding ESM to Jest #880

Closed
wants to merge 2 commits into from
Closed

[DO NOT MERGE] feeding ESM to Jest #880

wants to merge 2 commits into from

Conversation

olaf-k
Copy link
Contributor

@olaf-k olaf-k commented Dec 1, 2022

The PR to allow custom prefix for the Vivid elements relies on meta.import.url to propagate said prefix. As this is an ESM feature and Jest works with CJS modules, the PR mocks meta.import using ts-jest-mock-import-meta.

This PR is an attempt at feeding Jest ESM directly. This has been done by updating the config files, using Jest native ESM support and updating a few things in the tests.

Config files

  • Removed module: commonjs from tsconfig.spec.json to output ES modules.
  • Updated jest.config.cjs to use the ts-jest/presets/default-esm preset.
  • Fine-tuning: ignore CSS styles files imported in components with jest-transform-stub, make sure @vivid-nx/shared is found using moduleNameMapper.

Jest ESM

As explained here, the support is currently experimental and only works when the proper --experimental-vm-modules node flag is used. Do not forget it or nothing will work.

Updating the tests

  • For tests that use them, Jest globals need to be explicitly imported with import {jest} from '@jest/globals'; (could also have used the jest object in import.meta).
  • The icon test uses require('promise') and several tests use jest-axe which is only available as a CJS module. These tests were patched using the method documented in Jest's site (i.e., by importing node's require).
  • The rest are just tiny irrelevant TS fixes to make ts-jest stop complaining.

Remaining issues

  • The dialog test has been excluded from the suite because Jest insists on treating the CSS from the polyfill it uses as a module. I didn't investigate further. Should be fixable given more time? (And why doesn't it fall into jest-transform-stub?)
  • The popup test imports the floating-ui ES module and uses Jest to mock part of it, but since ESM are immutable this raises an error. This should be fixable using a dynamic import apparently but I haven't tested.
  • More annoyingly, the import.meta.url does not seem to carry over query parameters? See the breadcrumb test which logs the url without params when called with import './index?prefix=xxx'; which is quite ironic since that would be a major reason to switch to ESM. Jest limitation or I may have missed something here.

Additional reading

@olaf-k
Copy link
Contributor Author

olaf-k commented Dec 22, 2022

Keeping for the future, closing for now.

@olaf-k olaf-k closed this Dec 22, 2022
@rinaok rinaok deleted the jest-esm branch July 10, 2023 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant