Skip to content

chore(site): upgrade msw to 2.0 #12597

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

Merged
merged 16 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions site/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ env:
es6: true
jest: true
node: true
ignorePatterns:
- "jest.polyfills.js"
extends:
- eslint:recommended
- plugin:@typescript-eslint/recommended
Expand Down
13 changes: 12 additions & 1 deletion site/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
{
displayName: "test",
roots: ["<rootDir>"],
setupFiles: ["./jest.polyfills.js"],
setupFilesAfterEnv: ["./jest.setup.ts"],
extensionsToTreatAsEsm: [".ts"],
transform: {
Expand All @@ -27,8 +28,18 @@ module.exports = {
],
},
testEnvironment: "jsdom",
testEnvironmentOptions: {
customExportConditions: [""],
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
testPathIgnorePatterns: ["/node_modules/", "/e2e/"],
testPathIgnorePatterns: [
"/node_modules/",
"/e2e/",
// TODO: This test is timing out after upgrade a few Jest dependencies
// and I was not able to figure out why. When running it specifically, I
// can see many act warnings that may can help us to find the issue.
"/usePaginatedQuery.test.ts",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Parkreiner I may need your help with this 🙏 I created a separated issue to not block this one #12598

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! Happy to take a look tomorrow after Backstage launch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than temporarily ignore the file, can we use test.failing on the tests in the file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realize that was an option. Definitely think we should go with that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, describe.skip or test.failure are not working 😢 it just hangs forever

Screen.Recording.2024-03-14.at.14.54.43.mov

Ps: I tried to post a longer video but GH didn't allow me because of the size.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's annoying... what happens if you add an empty non-skipped test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it just hangs... I think there is some weird thing happening on this test 🤔

],
transformIgnorePatterns: [
"<rootDir>/node_modules/@chartjs-adapter-date-fns",
],
Expand Down
32 changes: 32 additions & 0 deletions site/jest.polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Necessary for MSW
*
* @note The block below contains polyfills for Node.js globals
* required for Jest to function when running JSDOM tests.
* These HAVE to be require's and HAVE to be in this exact
* order, since "undici" depends on the "TextEncoder" global API.
*
* Consider migrating to a more modern test runner if
* you don't want to deal with this.
*/
const { TextDecoder, TextEncoder } = require("node:util");
const { ReadableStream } = require("node:stream/web");

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
});

const { Blob, File } = require("node:buffer");
const { fetch, Headers, FormData, Request, Response } = require("undici");

Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
});
10 changes: 0 additions & 10 deletions site/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import "@testing-library/jest-dom";
import "jest-location-mock";
import { cleanup } from "@testing-library/react";
import { Blob } from "buffer";
import crypto from "crypto";
import jestFetchMock from "jest-fetch-mock";
import { useMemo } from "react";
import { TextEncoder, TextDecoder } from "util";
import type { Region } from "api/typesGenerated";
import type { ProxyLatencyReport } from "contexts/useProxyLatency";
import { server } from "testHelpers/server";

jestFetchMock.enableMocks();

// useProxyLatency does some http requests to determine latency.
// This would fail unit testing, or at least make it very slow with
// actual network requests. So just globally mock this hook.
Expand Down Expand Up @@ -42,11 +37,6 @@ jest.mock("contexts/useProxyLatency", () => ({
},
}));

global.TextEncoder = TextEncoder;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Polyfill for jsdom
global.TextDecoder = TextDecoder as any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Polyfill for jsdom
global.Blob = Blob as any;
global.scrollTo = jest.fn();

window.HTMLElement.prototype.scrollIntoView = jest.fn();
Expand Down
4 changes: 2 additions & 2 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"tzdata": "1.0.30",
"ua-parser-js": "1.0.33",
"ufuzzy": "npm:@leeoniya/ufuzzy@1.0.10",
"undici": "6.7.1",
"unique-names-generator": "4.7.1",
"uuid": "9.0.0",
"xterm": "5.2.0",
Expand Down Expand Up @@ -151,12 +152,11 @@
"jest": "29.6.2",
"jest-canvas-mock": "2.5.2",
"jest-environment-jsdom": "29.5.0",
"jest-fetch-mock": "3.0.3",
"jest-location-mock": "2.0.0",
"jest-runner-eslint": "2.1.0",
"jest-websocket-mock": "2.5.0",
"jest_workaround": "0.1.14",
"msw": "1.3.0",
"msw": "2.2.3",
"prettier": "3.1.0",
"protobufjs": "7.2.4",
"rxjs": "7.8.1",
Expand Down
Loading