Skip to content

Commit cdd4d0b

Browse files
test: add vitest-fail-on-console and improve test stability (#170)
* test: add vitest-fail-on-console and improve test stability - Integrate `vitest-fail-on-console` plugin to ensure console logs cause test failures - Use `act` to wrap asynchronous updates in test interactions - Add `happy-dom` for improved test environment compatibility - Update test descriptions to remove async usage where unnecessary * feat: enhance button accessibility and update tsconfig with testing types - Add 'type="button"' to improve button element accessibility in test files - Include 'vitest/globals' and '@testing-library/jest-dom' types in tsconfig for better testing support * Create grumpy-ways-look.md --------- Co-authored-by: Hayden Bleasel <[email protected]>
1 parent 0949dd9 commit cdd4d0b

File tree

12 files changed

+150
-35
lines changed

12 files changed

+150
-35
lines changed

.changeset/grumpy-ways-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ai-elements": patch
3+
---
4+
5+
test: add vitest-fail-on-console and improve test stability

packages/elements/__tests__/actions.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe("Actions", () => {
66
it("renders children", () => {
77
render(
88
<Actions>
9-
<button>Test Action</button>
9+
<button type="button">Test Action</button>
1010
</Actions>
1111
);
1212
expect(screen.getByText("Test Action")).toBeInTheDocument();
@@ -15,7 +15,7 @@ describe("Actions", () => {
1515
it("applies custom className", () => {
1616
const { container } = render(
1717
<Actions className="custom-class">
18-
<button>Test</button>
18+
<button type="button">Test</button>
1919
</Actions>
2020
);
2121
expect(container.firstChild).toHaveClass("custom-class");

packages/elements/__tests__/artifact.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe("ArtifactActions", () => {
6666
it("renders action buttons", () => {
6767
render(
6868
<ArtifactActions>
69-
<button>Action 1</button>
69+
<button type="button">Action 1</button>
7070
</ArtifactActions>
7171
);
7272
expect(screen.getByText("Action 1")).toBeInTheDocument();

packages/elements/__tests__/chain-of-thought.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe("ChainOfThoughtImage", () => {
186186
render(
187187
<ChainOfThought>
188188
<ChainOfThoughtImage>
189-
<img alt="test" src="test.jpg" />
189+
<img alt="test" height={100} src="test.jpg" width={100} />
190190
</ChainOfThoughtImage>
191191
</ChainOfThought>
192192
);
@@ -198,7 +198,7 @@ describe("ChainOfThoughtImage", () => {
198198
render(
199199
<ChainOfThought>
200200
<ChainOfThoughtImage caption="Image caption">
201-
<img alt="test" src="test.jpg" />
201+
<img alt="test" height={100} src="test.jpg" width={100} />
202202
</ChainOfThoughtImage>
203203
</ChainOfThought>
204204
);

packages/elements/__tests__/code-block.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("CodeBlock", () => {
3636
it("renders children actions", () => {
3737
render(
3838
<CodeBlock code="code" language="javascript">
39-
<button>Action</button>
39+
<button type="button">Action</button>
4040
</CodeBlock>
4141
);
4242
expect(screen.getByText("Action")).toBeInTheDocument();

packages/elements/__tests__/plan.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ describe("Plan", () => {
406406
});
407407

408408
it("applies custom className", () => {
409-
const { container } = render(
409+
render(
410410
<Plan>
411411
<PlanHeader>
412412
<PlanTrigger className="custom-trigger" />

packages/elements/__tests__/prompt-input.test.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ describe("PromptInputTextarea", () => {
501501
expect(onSubmit).not.toHaveBeenCalled();
502502
});
503503

504-
it("does not submit on Enter during IME composition - #21", async () => {
504+
it("does not submit on Enter during IME composition - #21", () => {
505505
const onSubmit = vi.fn();
506506

507507
render(
@@ -1021,7 +1021,7 @@ describe("Paste functionality", () => {
10211021
});
10221022
});
10231023

1024-
it("handles paste with no files", async () => {
1024+
it("handles paste with no files", () => {
10251025
const onSubmit = vi.fn();
10261026

10271027
render(
@@ -1126,7 +1126,9 @@ describe("PromptInputSpeechButton", () => {
11261126
if (button) {
11271127
// Start listening
11281128
await user.click(button);
1129-
mockRecognition.onstart?.();
1129+
act(() => {
1130+
mockRecognition.onstart?.();
1131+
});
11301132

11311133
// Stop listening
11321134
await user.click(button);
@@ -1698,9 +1700,8 @@ describe("PromptInputTab components", () => {
16981700
});
16991701

17001702
describe("PromptInputModelSelect components", () => {
1701-
it("renders model select with all subcomponents", async () => {
1703+
it("renders model select with all subcomponents", () => {
17021704
const onSubmit = vi.fn();
1703-
const user = userEvent.setup();
17041705

17051706
render(
17061707
<PromptInput onSubmit={onSubmit}>

packages/elements/__tests__/reasoning.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen } from "@testing-library/react";
1+
import { act, render, screen } from "@testing-library/react";
22
import userEvent from "@testing-library/user-event";
33
import { describe, expect, it, vi } from "vitest";
44
import {
@@ -85,7 +85,9 @@ describe("Reasoning", () => {
8585
expect(screen.getByText("Reasoning content")).toBeVisible();
8686

8787
// Advance time past AUTO_CLOSE_DELAY (3000ms)
88-
vi.advanceTimersByTime(3100);
88+
act(() => {
89+
vi.advanceTimersByTime(3100);
90+
});
8991

9092
// Should auto-close
9193
await vi.waitFor(() => {

packages/elements/__tests__/setup.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "@testing-library/jest-dom/vitest";
22
import { cleanup } from "@testing-library/react";
33
import { afterEach, vi } from "vitest";
4+
import failOnConsole from "vitest-fail-on-console";
45

56
// Mock CSS imports
67
vi.mock("*.css", () => ({}));
@@ -41,6 +42,45 @@ Object.defineProperty(window, "matchMedia", {
4142
})),
4243
});
4344

45+
// Patch createElement to handle SVG elements properly in jsdom
46+
const SVG_TAGS = new Set([
47+
"svg",
48+
"path",
49+
"circle",
50+
"g",
51+
"rect",
52+
"line",
53+
"polyline",
54+
"polygon",
55+
"ellipse",
56+
"text",
57+
"tspan",
58+
"defs",
59+
"clipPath",
60+
]);
61+
62+
const originalCreateElement = document.createElement.bind(document);
63+
// @ts-expect-error - Overriding createElement signature for test environment
64+
document.createElement = (
65+
tagName: string,
66+
options?: ElementCreationOptions
67+
) => {
68+
if (SVG_TAGS.has(tagName.toLowerCase())) {
69+
return document.createElementNS("http://www.w3.org/2000/svg", tagName);
70+
}
71+
return originalCreateElement(tagName, options);
72+
};
73+
74+
// Fail the test if there are any console logs during test execution
75+
failOnConsole({
76+
shouldFailOnAssert: true,
77+
shouldFailOnDebug: true,
78+
shouldFailOnInfo: true,
79+
shouldFailOnWarn: true,
80+
shouldFailOnError: true,
81+
shouldFailOnLog: true,
82+
});
83+
4484
// Cleanup after each test
4585
afterEach(() => {
4686
cleanup();

packages/elements/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@vitest/coverage-v8": "^4.0.4",
3838
"jsdom": "^26.0.0",
3939
"typescript": "^5.9.3",
40-
"vitest": "^4.0.4"
40+
"vitest": "^4.0.4",
41+
"vitest-fail-on-console": "^0.10.1"
4142
}
4243
}

0 commit comments

Comments
 (0)