Skip to content

Commit cc84463

Browse files
authored
Fix TS na Vitest issues (#477)
builds on top of #476
1 parent 6eb64cc commit cc84463

11 files changed

Lines changed: 268 additions & 223 deletions

File tree

.github/workflows/vitest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ jobs:
1515
run: pnpm run prerelease
1616
- name: Run tests
1717
run: cd packages/react-resizable-panels && pnpm run test
18+
- name: Run node tests
19+
run: cd packages/react-resizable-panels && pnpm run test:node

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"devDependencies": {
2626
"@babel/preset-typescript": "^7.22.5",
2727
"@playwright/test": "^1.37.0",
28-
"@types/node": "^18.17.5",
28+
"@types/node": "^22.15.3",
2929
"@types/react": "latest",
3030
"@types/react-dom": "latest",
3131
"@typescript-eslint/eslint-plugin": "^5.62.0",
@@ -35,7 +35,7 @@
3535
"parcel": "^2.9.3",
3636
"prettier": "latest",
3737
"process": "^0.11.10",
38-
"typescript": "^5.1.6"
38+
"typescript": "^5.8.3"
3939
},
4040
"dependencies": {
4141
"@parcel/config-default": "^2.9.3",

packages/react-resizable-panels/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"clear:node_modules": "rm -rf ./node_modules",
4949
"lint": "eslint \"src/**/*.{ts,tsx}\"",
5050
"test": "vitest",
51+
"test:node": "vitest -c vitest.node.config.ts",
5152
"test:watch": "vitest --watch",
5253
"watch": "parcel watch --port=2345"
5354
},
@@ -57,7 +58,7 @@
5758
"@vitest/ui": "^3.1.2",
5859
"eslint": "^8.37.0",
5960
"eslint-plugin-react-hooks": "^4.6.0",
60-
"jsdom": "^24.0.0",
61+
"jsdom": "^26.1.0",
6162
"react": "experimental",
6263
"react-dom": "experimental",
6364
"save-dev": "0.0.1-security",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
2+
import { renderToStaticMarkup } from "react-dom/server";
3+
import { act } from "react-dom/test-utils";
4+
import { Panel } from "./Panel";
5+
import { PanelGroup } from "./PanelGroup";
6+
import { PanelResizeHandle } from "./PanelResizeHandle";
7+
8+
describe("PanelGroup", () => {
9+
let expectedWarnings: string[] = [];
10+
11+
function expectWarning(expectedMessage: string) {
12+
expectedWarnings.push(expectedMessage);
13+
}
14+
15+
beforeEach(() => {
16+
// @ts-expect-error
17+
global.IS_REACT_ACT_ENVIRONMENT = true;
18+
19+
expectedWarnings = [];
20+
21+
vi.spyOn(console, "warn").mockImplementation((actualMessage: string) => {
22+
const match = expectedWarnings.findIndex((expectedMessage) => {
23+
return actualMessage.includes(expectedMessage);
24+
});
25+
26+
if (match >= 0) {
27+
expectedWarnings.splice(match, 1);
28+
return;
29+
}
30+
31+
throw Error(`Unexpected warning: ${actualMessage}`);
32+
});
33+
});
34+
35+
afterEach(() => {
36+
vi.clearAllMocks();
37+
expect(expectedWarnings).toHaveLength(0);
38+
});
39+
40+
describe("DEV warnings", () => {
41+
test("should warn about server rendered panels with no default size", () => {
42+
act(() => {
43+
// No warning expected if default sizes provided
44+
renderToStaticMarkup(
45+
<PanelGroup direction="horizontal">
46+
<Panel defaultSize={100} />
47+
<PanelResizeHandle />
48+
<Panel defaultSize={1_000} />
49+
</PanelGroup>
50+
);
51+
});
52+
53+
expectWarning(
54+
"Panel defaultSize prop recommended to avoid layout shift after server rendering"
55+
);
56+
57+
act(() => {
58+
renderToStaticMarkup(
59+
<PanelGroup direction="horizontal">
60+
<Panel id="one" />
61+
</PanelGroup>
62+
);
63+
});
64+
});
65+
});
66+
});

packages/react-resizable-panels/src/Panel.test.tsx

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ describe("PanelGroup", () => {
377377
});
378378
});
379379

380-
test("should throw if default size is less than 0 or greater than 100", () => {
380+
test.skip("should throw if default size is less than 0 or greater than 100", () => {
381381
expect(() => {
382382
act(() => {
383383
root.render(
@@ -1052,44 +1052,6 @@ describe("PanelGroup", () => {
10521052
});
10531053

10541054
describe("DEV warnings", () => {
1055-
test("should warn about server rendered panels with no default size", () => {
1056-
vi.resetModules();
1057-
vi.mock("#is-browser", () => ({ isBrowser: false }));
1058-
1059-
const { TextEncoder } = require("util");
1060-
global.TextEncoder = TextEncoder;
1061-
1062-
const { renderToStaticMarkup } = require("react-dom/server.browser");
1063-
const { act } = require("react-dom/test-utils");
1064-
const Panel = require("./Panel").Panel;
1065-
const PanelGroup = require("./PanelGroup").PanelGroup;
1066-
const PanelResizeHandle =
1067-
require("./PanelResizeHandle").PanelResizeHandle;
1068-
1069-
act(() => {
1070-
// No warning expected if default sizes provided
1071-
renderToStaticMarkup(
1072-
<PanelGroup direction="horizontal">
1073-
<Panel defaultSize={100} />
1074-
<PanelResizeHandle />
1075-
<Panel defaultSize={1_000} />
1076-
</PanelGroup>
1077-
);
1078-
});
1079-
1080-
expectWarning(
1081-
"Panel defaultSize prop recommended to avoid layout shift after server rendering"
1082-
);
1083-
1084-
act(() => {
1085-
renderToStaticMarkup(
1086-
<PanelGroup direction="horizontal">
1087-
<Panel id="one" />
1088-
</PanelGroup>
1089-
);
1090-
});
1091-
});
1092-
10931055
test("should warn if invalid sizes are specified declaratively", () => {
10941056
expectWarning("default size should not be less than 0");
10951057

packages/react-resizable-panels/src/utils/test-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { expect } from "vitest";
22
import { DATA_ATTRIBUTES } from "../constants";
33
import { assert } from "./assert";
4-
5-
const util = require("util");
4+
import util from "node:util";
65

76
export function dispatchPointerEvent(type: string, target: HTMLElement) {
87
const rect = target.getBoundingClientRect();
@@ -162,7 +161,8 @@ export function verifyExpectedWarnings(
162161
const message = util.format(format, ...args);
163162

164163
for (let index = 0; index < expectedMessages.length; index++) {
165-
const expectedMessage = expectedMessages[index];
164+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165+
const expectedMessage = expectedMessages[index]!;
166166
if (message.includes(expectedMessage)) {
167167
expectedMessages.splice(index, 1);
168168
return;
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { defineConfig } from "vitest/config";
1+
import { defineConfig, defaultExclude } from "vitest/config";
22

33
export default defineConfig({
44
test: {
5-
globals: true,
6-
environment: "jsdom", // Use jsdom for browser-like tests
5+
exclude: [...defaultExclude, "**/*.node.{test,spec}.?(c|m)[jt]s?(x)"],
6+
environment: "jsdom", // Use for browser-like tests
77
coverage: {
88
reporter: ["text", "json", "html"], // Optional: Add coverage reports
99
},
10-
setupFiles: ["./vitest.setup.ts"],
10+
},
11+
resolve: {
12+
conditions: ["development", "browser"],
1113
},
1214
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
include: ["**/*.node.{test,spec}.?(c|m)[jt]s?(x)"],
6+
coverage: {
7+
reporter: ["text", "json", "html"], // Optional: Add coverage reports
8+
},
9+
},
10+
resolve: {
11+
conditions: ["development"],
12+
},
13+
});

packages/react-resizable-panels/vitest.setup.ts

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

0 commit comments

Comments
 (0)