Skip to content

Commit 20d254a

Browse files
committed
feature #1202 Migrate from Jest to Vitest (ChqThomas, weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- Migrate from Jest to Vitest | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Tickets | #1018 #610 | License | MIT This PR is a draft to test the feasibility of migrating from [jest ](https://jestjs.io/) to [vitest](https://vitest.dev/) Jest doesn't support ESM out of the box, which makes it difficult to test some libraries like Chart.js and Svelte v4. Vitest supports typescript and ESM by default and is compatible with Jest API, it'll help us test those packages and maybe others in the future #### Infos: There is a global config file `vitest.config.js` which contains the default settings for every package, when needed it can be extended with a file in the package root (ex: `src\React\assets\vitest.config.js`) I replaced the `yarn test` script with `vitest src --run` so it's run globally and the CI doesn't stop at the first error from a package, it still can be run for every/individual packages with `yarn workspaces run vitest --run` / `yarn workspace "`@symfony`/ux-live-component" run vitest --run` Libraries like fetch-mock-jest and jest-canvas-mock need to be replaced The `require_context_poylfill.ts` file used by ux-react, ux-vue and ux-svelte needs to be modified as it uses a dynamic `require`, replacing it with an `import` doesn't work as it is asynchronous #### Resources: - https://vitest.dev/guide/migration.html - https://srivastavaankita080.medium.com/jest-vitest-migration-79f4735dd5d0 ### TODO: - Packages: - [x] `@symfony`/ux-autocomplete `0/14` - [x] replace fetch-mock-jest ? - [x] `@symfony`/ux-chartjs `0/4` - [x] fix failing tests - [x] replace jest-canvas-mock ? - [x] `@symfony`/ux-cropperjs `1/1` - [x] `@symfony`/ux-dropzone `3/3` - [x] `@symfony`/ux-lazy-image `1/1` - [x] `@symfony`/ux-live-component `217/217` 💯 - [x] `Error: Uncaught [ReferenceError: Node is not defined]` Just a warning but the test suite pass - [x] `@symfony`/ux-notify `1/1` - [x] `@symfony`/ux-react `0/4` - [x] install `@vitejs`/plugin-react - [x] migrate `require_context_poylfill.ts` - [x] `@symfony`/stimulus-bundle `1/1` - [x] `Error: Uncaught [ReferenceError: Node is not defined]` Just a warning but the test suite pass - [x] `@symfony`/ux-svelte `0/4` - [x] replace svelte-jester with `@sveltejs`/vite-plugin-svelte - [x] migrate `require_context_poylfill.ts` - [x] fix failing tests - [x] `@symfony`/ux-swup `0/5` - [x] `require() of ES Module node_modules/delegate-it/index.js from node_modules/swup/lib/index.js not supported.` - [x] `@symfony`/ux-toggle-password `1/1` - [x] `@symfony`/ux-translator `91/91` - [x] `@symfony`/ux-turbo `0/2` - [x] `TypeError: Cannot read properties of undefined (reading 'prototype')` - [x] `@symfony`/ux-typed `1/1` - [x] `@symfony`/ux-vue `0/5` - [x] replace `@vue`/vue3-jest with `@vitejs`/plugin-vue - [x] migrate `require_context_poylfill.ts` - [x] Remove jest - [x] Cleanup - [ ] Benchmark ? I'll need help to resolve these issues 😁 Commits ------- 76d82de7a5 Removing need for jq 7f37e599ba vitest.config.js format f953d52f6c Remove jest config and dependencies de32106b84 Import vitest when needed instead of adding global types in tsconfig 9322bc7d47 format 2be40886de re-adding module a33fc1dc84 Fixing final tests f59f4f696d Working around Turbo issue 13c7431525 dropping support for very old versions of swup ecba87b55c Adding script to run all tests, but keep going if a previous one fails 238d25efa8 Stopping Stimulus to avoid side effects after the test cf29587c4a Replacing require.context polyfill with a simpler implementation ef340312da Swapping in vitest-canvas-mock 22704067e0 Switching to vitest-fetch-mock 9043da3db3 Removing no-longer-needed setup file c1db340e39 Fixing problem where Stimulus sometimes continued after the test 10c5591acb Add forgotten package.json from ux-react package e9e16ebfa5 Wip trying to migrate from jest to vitest
2 parents 01910ee + 52e16d4 commit 20d254a

File tree

4 files changed

+7
-19
lines changed

4 files changed

+7
-19
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/.symfony.bundle.yaml export-ignore
44
/phpunit.xml.dist export-ignore
55
/assets/.gitignore export-ignore
6-
/assets/jest.config.js export-ignore
76
/assets/src/**/*.ts export-ignore
87
/assets/test export-ignore
98
/phpunit.xml.dist export-ignore

assets/jest.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

assets/test/setup.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

assets/test/tools.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ let activeTests: FunctionalTest[] = [];
1313
let application: Application;
1414

1515
export function shutdownTests() {
16+
if (application) {
17+
application.stop();
18+
}
19+
1620
if (activeTests.length === 0) {
1721
// no test was run, apparently
1822
return;
@@ -366,7 +370,9 @@ export function createTestForExistingComponent(component: Component): Functional
366370

367371
export async function startStimulus(element: string|HTMLElement) {
368372
// start the Stimulus app just once per test suite
369-
if (!application) {
373+
if (application) {
374+
await application.start();
375+
} else {
370376
application = Application.start();
371377
application.register('live', LiveController);
372378
}

0 commit comments

Comments
 (0)