Skip to content

Commit a8842b0

Browse files
authored
chore: Upgrade msw to 2.7.0 (and Node.js to 22.13.1) in /ui (#4995)
The main purpose was to upgrade msw from 0.36.8 to 2.7.0, to get rid of the security alerts of the old version. The upgrade turned out to be quite difficult, and in the end I only got the tests to pass with this combination of other dependencies: - test running environment: @happy-dom/jest-environment - jsdom (the default) or jest-fixed-jsdom (suggested in many places around the internet) neither worked due to various different errors. - Node.js version: 22 - I don't have an explanation, but with Node 20 the tests often fail to navigate to other pages, without visible errors, though sometimes the tests do pass. I saw the same failing behavior in other running environments than @happy-dom/jest-environment too, but in those there were also some visible errors. With Node 22, the tests seem to run notably faster, which might be related. - Version 22 has been in long-term support for quite a while now, so it's good to move to it anyway. Signed-off-by: Harri Lehtola <peruukki@hotmail.com>
1 parent 565ea2c commit a8842b0

File tree

6 files changed

+234
-383
lines changed

6 files changed

+234
-383
lines changed

ui/.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v20.18.0
1+
v22.13.1

ui/jest.config.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ module.exports = {
99
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
1010
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}",
1111
],
12-
testEnvironment: "jsdom",
12+
// Couldn't get tests working with msw 2 in jsdom or jest-fixed-jsdom,
13+
// happy-dom finally worked with added globals
14+
testEnvironment: "@happy-dom/jest-environment",
15+
// https://mswjs.io/docs/migrations/1.x-to-2.x#cannot-find-module-mswnode-jsdom
16+
testEnvironmentOptions: {
17+
customExportConditions: [''],
18+
},
1319
transform: {
1420
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js",
1521
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",

ui/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
5757
"@babel/preset-env": "^7.25.8",
5858
"@babel/preset-react": "^7.25.7",
59+
"@happy-dom/jest-environment": "^16.7.3",
5960
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.3",
6061
"@rollup/plugin-babel": "^5.3.1",
6162
"@rollup/plugin-commonjs": "^21.0.2",
@@ -68,7 +69,7 @@
6869
"@testing-library/react": "^16.0.1",
6970
"@testing-library/user-event": "^14.5.2",
7071
"@types/jest": "^27.0.1",
71-
"@types/node": "^20.16.13",
72+
"@types/node": "^22.12.0",
7273
"@types/react": "^18.3.11",
7374
"@types/react-dom": "^18.3.0",
7475
"babel-jest": "^27.4.2",
@@ -95,7 +96,7 @@
9596
"jest-resolve": "^29.7.0",
9697
"jest-watch-typeahead": "^2.2.2",
9798
"mini-css-extract-plugin": "^2.4.5",
98-
"msw": "^0.36.8",
99+
"msw": "^2.7.0",
99100
"postcss": "^8.4.4",
100101
"postcss-flexbugs-fixes": "^5.0.2",
101102
"postcss-loader": "^6.2.1",

ui/src/mocks/handlers.ts

+17-27
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
import { rest } from "msw";
2-
import {readFileSync} from 'fs';
1+
import { http, HttpResponse } from "msw";
2+
import { readFileSync } from 'fs';
33
import path from "path";
44

55
const registry = readFileSync(path.resolve(__dirname, "../../public/registry.db"));
66

7-
const projectsListWithDefaultProject = rest.get(
8-
"/projects-list.json",
9-
(req, res, ctx) => {
10-
return res(
11-
ctx.status(200),
12-
ctx.json({
13-
default: "credit_score_project",
14-
projects: [
15-
{
16-
name: "Credit Score Project",
17-
description:
18-
"Project for credit scoring team and associated models.",
19-
id: "credit_score_project",
20-
registryPath: "/registry.pb",
21-
},
22-
],
23-
})
24-
);
25-
}
7+
const projectsListWithDefaultProject = http.get("/projects-list.json", () =>
8+
HttpResponse.json({
9+
default: "credit_score_project",
10+
projects: [
11+
{
12+
name: "Credit Score Project",
13+
description: "Project for credit scoring team and associated models.",
14+
id: "credit_score_project",
15+
registryPath: "/registry.pb",
16+
},
17+
],
18+
})
2619
);
2720

28-
const creditHistoryRegistry = rest.get("/registry.pb", (req, res, ctx) => {
29-
return res(
30-
ctx.status(200),
31-
ctx.set('Content-Type', 'application/octet-stream'),
32-
ctx.body(registry));
33-
});
21+
const creditHistoryRegistry = http.get("/registry.pb", () =>
22+
HttpResponse.arrayBuffer(registry.buffer)
23+
);
3424

3525
export { projectsListWithDefaultProject, creditHistoryRegistry };

ui/src/setupTests.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
// expect(element).toHaveTextContent(/react/i)
44
// learn more: https://github.com/testing-library/jest-dom
55
import '@testing-library/jest-dom';
6+
import { BroadcastChannel } from 'worker_threads';
7+
8+
// BroadcastChannel is missing from @happy-dom/jest-environment globals
9+
Object.assign(global, { BroadcastChannel });

0 commit comments

Comments
 (0)