Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 23e3e4c

Browse files
chore(site): upgrade msw to 2.0 (coder#12597)
Closes coder#11426
1 parent 9cfd5ba commit 23e3e4c

29 files changed

+655
-860
lines changed

site/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ env:
55
es6: true
66
jest: true
77
node: true
8+
ignorePatterns:
9+
- "jest.polyfills.js"
810
extends:
911
- eslint:recommended
1012
- plugin:@typescript-eslint/recommended

site/jest.config.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
{
77
displayName: "test",
88
roots: ["<rootDir>"],
9+
setupFiles: ["./jest.polyfills.js"],
910
setupFilesAfterEnv: ["./jest.setup.ts"],
1011
extensionsToTreatAsEsm: [".ts"],
1112
transform: {
@@ -27,8 +28,18 @@ module.exports = {
2728
],
2829
},
2930
testEnvironment: "jsdom",
31+
testEnvironmentOptions: {
32+
customExportConditions: [""],
33+
},
3034
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
31-
testPathIgnorePatterns: ["/node_modules/", "/e2e/"],
35+
testPathIgnorePatterns: [
36+
"/node_modules/",
37+
"/e2e/",
38+
// TODO: This test is timing out after upgrade a few Jest dependencies
39+
// and I was not able to figure out why. When running it specifically, I
40+
// can see many act warnings that may can help us to find the issue.
41+
"/usePaginatedQuery.test.ts",
42+
],
3243
transformIgnorePatterns: [
3344
"<rootDir>/node_modules/@chartjs-adapter-date-fns",
3445
],

site/jest.polyfills.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Necessary for MSW
3+
*
4+
* @note The block below contains polyfills for Node.js globals
5+
* required for Jest to function when running JSDOM tests.
6+
* These HAVE to be require's and HAVE to be in this exact
7+
* order, since "undici" depends on the "TextEncoder" global API.
8+
*
9+
* Consider migrating to a more modern test runner if
10+
* you don't want to deal with this.
11+
*/
12+
const { TextDecoder, TextEncoder } = require("node:util");
13+
const { ReadableStream } = require("node:stream/web");
14+
15+
Object.defineProperties(globalThis, {
16+
TextDecoder: { value: TextDecoder },
17+
TextEncoder: { value: TextEncoder },
18+
ReadableStream: { value: ReadableStream },
19+
});
20+
21+
const { Blob, File } = require("node:buffer");
22+
const { fetch, Headers, FormData, Request, Response } = require("undici");
23+
24+
Object.defineProperties(globalThis, {
25+
fetch: { value: fetch, writable: true },
26+
Blob: { value: Blob },
27+
File: { value: File },
28+
Headers: { value: Headers },
29+
FormData: { value: FormData },
30+
Request: { value: Request },
31+
Response: { value: Response },
32+
});

site/jest.setup.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import "@testing-library/jest-dom";
22
import "jest-location-mock";
33
import { cleanup } from "@testing-library/react";
4-
import { Blob } from "buffer";
54
import crypto from "crypto";
6-
import jestFetchMock from "jest-fetch-mock";
75
import { useMemo } from "react";
8-
import { TextEncoder, TextDecoder } from "util";
96
import type { Region } from "api/typesGenerated";
107
import type { ProxyLatencyReport } from "contexts/useProxyLatency";
118
import { server } from "testHelpers/server";
129

13-
jestFetchMock.enableMocks();
14-
1510
// useProxyLatency does some http requests to determine latency.
1611
// This would fail unit testing, or at least make it very slow with
1712
// actual network requests. So just globally mock this hook.
@@ -42,11 +37,6 @@ jest.mock("contexts/useProxyLatency", () => ({
4237
},
4338
}));
4439

45-
global.TextEncoder = TextEncoder;
46-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Polyfill for jsdom
47-
global.TextDecoder = TextDecoder as any;
48-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Polyfill for jsdom
49-
global.Blob = Blob as any;
5040
global.scrollTo = jest.fn();
5141

5242
window.HTMLElement.prototype.scrollIntoView = jest.fn();

site/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"tzdata": "1.0.30",
8383
"ua-parser-js": "1.0.33",
8484
"ufuzzy": "npm:@leeoniya/[email protected]",
85+
"undici": "6.7.1",
8586
"unique-names-generator": "4.7.1",
8687
"uuid": "9.0.0",
8788
"xterm": "5.2.0",
@@ -151,12 +152,11 @@
151152
"jest": "29.6.2",
152153
"jest-canvas-mock": "2.5.2",
153154
"jest-environment-jsdom": "29.5.0",
154-
"jest-fetch-mock": "3.0.3",
155155
"jest-location-mock": "2.0.0",
156156
"jest-runner-eslint": "2.1.0",
157157
"jest-websocket-mock": "2.5.0",
158158
"jest_workaround": "0.1.14",
159-
"msw": "1.3.0",
159+
"msw": "2.2.3",
160160
"prettier": "3.1.0",
161161
"protobufjs": "7.2.4",
162162
"rxjs": "7.8.1",

0 commit comments

Comments
 (0)