Skip to content

Commit c627a68

Browse files
authored
chore: migrate some tests from jest to vitest (coder#20568)
1 parent 7ae3fdc commit c627a68

File tree

77 files changed

+629
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+629
-375
lines changed

site/.knip.jsonc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
"@types/react-virtualized-auto-sizer",
99
"jest_workaround",
1010
"ts-proto"
11-
]
11+
],
12+
"jest": {
13+
"entry": "./src/**/*.jest.{ts,tsx}"
14+
}
1215
}

site/jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
testEnvironmentOptions: {
3232
customExportConditions: [""],
3333
},
34-
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
34+
testRegex: "(/__tests__/.*|(\\.|/)(jest))\\.tsx?$",
3535
testPathIgnorePatterns: [
3636
"/node_modules/",
3737
"/e2e/",

site/package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
"storybook": "STORYBOOK=true storybook dev -p 6006",
2828
"storybook:build": "storybook build",
2929
"storybook:ci": "storybook build --test",
30-
"test": "jest",
31-
"test:ci": "jest --selectProjects test --silent",
32-
"test:coverage": "jest --selectProjects test --collectCoverage",
33-
"test:watch": "jest --selectProjects test --watch",
30+
"test": "vitest run && jest",
31+
"test:ci": "vitest run && jest --silent",
32+
"test:watch": "vitest",
33+
"test:watch-jest": "jest --watch",
3434
"stats": "STATS=true pnpm build && npx http-server ./stats -p 8081 -c-1",
3535
"update-emojis": "cp -rf ./node_modules/emoji-datasource-apple/img/apple/64/* ./static/emojis"
3636
},
@@ -133,7 +133,7 @@
133133
"@swc/core": "1.3.38",
134134
"@swc/jest": "0.2.37",
135135
"@tailwindcss/typography": "0.5.16",
136-
"@testing-library/jest-dom": "6.6.3",
136+
"@testing-library/jest-dom": "6.9.1",
137137
"@testing-library/react": "14.3.1",
138138
"@testing-library/user-event": "14.6.1",
139139
"@types/chroma-js": "2.4.0",
@@ -167,6 +167,7 @@
167167
"jest-location-mock": "2.0.0",
168168
"jest-websocket-mock": "2.5.0",
169169
"jest_workaround": "0.1.14",
170+
"jsdom": "27.0.1",
170171
"knip": "5.64.1",
171172
"msw": "2.4.8",
172173
"postcss": "8.5.6",
@@ -180,7 +181,8 @@
180181
"ts-proto": "1.181.2",
181182
"typescript": "5.6.3",
182183
"vite": "7.1.11",
183-
"vite-plugin-checker": "0.11.0"
184+
"vite-plugin-checker": "0.11.0",
185+
"vitest": "4.0.5"
184186
},
185187
"browserslist": [
186188
"chrome 110",

site/pnpm-lock.yaml

Lines changed: 543 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/api/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ function getConfiguredAxiosInstance(): AxiosInstance {
27592759
}
27602760
} else {
27612761
// Do not write error logs if we are in a FE unit test.
2762-
if (process.env.JEST_WORKER_ID === undefined) {
2762+
if (!process.env.JEST_WORKER_ID && !process.env.VITEST) {
27632763
console.error("CSRF token not found");
27642764
}
27652765
}

site/src/components/Dialogs/ConfirmDialog/ConfirmDialog.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ConfirmDialog } from "./ConfirmDialog";
55
describe("ConfirmDialog", () => {
66
it("onClose is called when cancelled", () => {
77
// Given
8-
const onCloseMock = jest.fn();
8+
const onCloseMock = vi.fn();
99
const props = {
1010
cancelText: "CANCEL",
1111
hideCancel: false,
@@ -24,8 +24,8 @@ describe("ConfirmDialog", () => {
2424

2525
it("onConfirm is called when confirmed", () => {
2626
// Given
27-
const onCloseMock = jest.fn();
28-
const onConfirmMock = jest.fn();
27+
const onCloseMock = vi.fn();
28+
const onConfirmMock = vi.fn();
2929
const props = {
3030
cancelText: "CANCEL",
3131
confirmText: "CONFIRM",

site/src/components/Dialogs/DeleteDialog/DeleteDialog.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ describe("DeleteDialog", () => {
2323
renderComponent(
2424
<DeleteDialog
2525
isOpen
26-
onConfirm={jest.fn()}
27-
onCancel={jest.fn()}
26+
onConfirm={vi.fn()}
27+
onCancel={vi.fn()}
2828
entity="template"
2929
name="MyTemplate"
3030
/>,
@@ -38,8 +38,8 @@ describe("DeleteDialog", () => {
3838
renderComponent(
3939
<DeleteDialog
4040
isOpen
41-
onConfirm={jest.fn()}
42-
onCancel={jest.fn()}
41+
onConfirm={vi.fn()}
42+
onCancel={vi.fn()}
4343
entity="template"
4444
name="MyTemplate"
4545
/>,
@@ -56,8 +56,8 @@ describe("DeleteDialog", () => {
5656
renderComponent(
5757
<DeleteDialog
5858
isOpen
59-
onConfirm={jest.fn()}
60-
onCancel={jest.fn()}
59+
onConfirm={vi.fn()}
60+
onCancel={vi.fn()}
6161
entity="template"
6262
name="MyTemplate"
6363
/>,

site/src/components/FileUpload/FileUpload.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fireEvent, screen } from "@testing-library/react";
33
import { FileUpload } from "./FileUpload";
44

55
test("accepts files with the correct extension", async () => {
6-
const onUpload = jest.fn();
6+
const onUpload = vi.fn();
77

88
renderComponent(
99
<FileUpload
@@ -21,14 +21,14 @@ test("accepts files with the correct extension", async () => {
2121
fireEvent.drop(dropZone, {
2222
dataTransfer: { files: [tarFile] },
2323
});
24-
expect(onUpload).toBeCalledWith(tarFile);
24+
expect(onUpload).toHaveBeenCalledWith(tarFile);
2525
onUpload.mockClear();
2626

2727
const zipFile = new File([""], "file.zip");
2828
fireEvent.drop(dropZone, {
2929
dataTransfer: { files: [zipFile] },
3030
});
31-
expect(onUpload).toBeCalledWith(zipFile);
31+
expect(onUpload).toHaveBeenCalledWith(zipFile);
3232
onUpload.mockClear();
3333

3434
const unsupportedFile = new File([""], "file.mp4");

site/src/components/GlobalSnackbar/utils.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Mock } from "vitest";
12
import {
23
displayError,
34
displaySuccess,
@@ -48,7 +49,7 @@ describe("Snackbar", () => {
4849

4950
describe("displaySuccess", () => {
5051
const originalWindowDispatchEvent = window.dispatchEvent;
51-
type TDispatchEventMock = jest.MockedFunction<
52+
type TDispatchEventMock = Mock<
5253
(msg: CustomEvent<NotificationMsg>) => boolean
5354
>;
5455
let dispatchEventMock: TDispatchEventMock;
@@ -67,7 +68,7 @@ describe("Snackbar", () => {
6768
};
6869

6970
beforeEach(() => {
70-
dispatchEventMock = jest.fn();
71+
dispatchEventMock = vi.fn();
7172
window.dispatchEvent =
7273
dispatchEventMock as unknown as typeof window.dispatchEvent;
7374
});
@@ -114,16 +115,18 @@ describe("Snackbar", () => {
114115
});
115116

116117
describe("displayError", () => {
117-
it("shows the title and the message", (done) => {
118+
it("shows the title and the message", () => {
118119
const message = "Some error happened";
119120

120-
window.addEventListener(SnackbarEventType, (event) => {
121-
const notificationEvent = event as CustomEvent<NotificationMsg>;
122-
expect(notificationEvent.detail.msg).toEqual(message);
123-
done();
124-
});
121+
return new Promise<void>((resolve) => {
122+
window.addEventListener(SnackbarEventType, (event) => {
123+
const notificationEvent = event as CustomEvent<NotificationMsg>;
124+
expect(notificationEvent.detail.msg).toEqual(message);
125+
resolve();
126+
});
125127

126-
displayError(message);
128+
displayError(message);
129+
});
127130
});
128131
});
129132
});

0 commit comments

Comments
 (0)