[DO NOT MERGE] feeding ESM to Jest #880
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 mocksmeta.import
usingts-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
module: commonjs
fromtsconfig.spec.json
to output ES modules.jest.config.cjs
to use thets-jest/presets/default-esm
preset.jest-transform-stub
, make sure@vivid-nx/shared
is found usingmoduleNameMapper
.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
import {jest} from '@jest/globals';
(could also have used the jest object inimport.meta
).require('promise')
and several tests usejest-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).Remaining issues
jest-transform-stub
?)import.meta.url
does not seem to carry over query parameters? See the breadcrumb test which logs the url without params when called withimport './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