Skip to content

Commit 4e8f3b5

Browse files
doublegateclaude
andcommitted
fix(tests): resolve all frontend test failures in CI (v0.5.0-alpha.5)
dashboardStore.test.ts: - Change filters/sort assertions from toBeNull() to toBeUndefined() - Fixes lines 20 and 69 to match updated type definitions - Aligns with previous TypeScript strict null checks fix - 2 failures → 0 failures pages.test.tsx: - Fix MemoryRouter import from "react-router-dom" (not "react-router") - Add comprehensive Tauri invoke mocks for async operations: * get_findings (returns empty array) * get_dashboard_stats (returns full DashboardStats structure) * list_campaigns (returns empty array) - Add waitFor() for Dashboard async data loading (3s timeout) - Fix Dashboard stat card title assertions (Total Hosts, not Hosts Scanned) - Add waitFor() for Reports async rendering (3s timeout) - Resolves useNavigate router context error - Resolves async data loading race conditions - 6 failures → 0 failures All frontend tests now passing: 121/121 (100%) All platforms: Ubuntu, macOS, Windows ✓ Test Results: - dashboardStore.test.ts: 5/5 passing - pages.test.tsx: 19/19 passing - Total: 20 test files, 121 tests, 0 failures Root Causes Fixed: 1. Type mismatch: null vs undefined after strict checks 2. Missing router context for useNavigate hook 3. Missing Tauri API mocks for async invoke calls 4. Synchronous assertions on async data loading 5. Incorrect component text expectations Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 03095aa commit 4e8f3b5

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

crates/spectre-gui/frontend/src/pages/pages.test.tsx

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect, vi } from "vitest";
2-
import { render, screen } from "@testing-library/react";
3-
import { MemoryRouter } from "react-router";
2+
import { render, screen, waitFor } from "@testing-library/react";
3+
import { MemoryRouter } from "react-router-dom";
44
import { Dashboard } from "./Dashboard";
55
import { Targets } from "./Targets";
66
import { Recon } from "./Recon";
@@ -26,6 +26,29 @@ vi.mock("@tauri-apps/api/core", () => ({
2626
config_loaded: true,
2727
});
2828
}
29+
if (cmd === "get_findings") {
30+
return Promise.resolve([]);
31+
}
32+
if (cmd === "get_dashboard_stats") {
33+
return Promise.resolve({
34+
total_hosts: 42,
35+
total_ports: 256,
36+
open_ports: 128,
37+
services_found: 64,
38+
severity_counts: {
39+
critical: 2,
40+
high: 5,
41+
medium: 7,
42+
low: 2,
43+
info: 0,
44+
},
45+
top_services: [],
46+
recent_activity: [],
47+
});
48+
}
49+
if (cmd === "list_campaigns") {
50+
return Promise.resolve([]);
51+
}
2952
return Promise.reject("Unknown command");
3053
}),
3154
}));
@@ -39,18 +62,22 @@ function renderWithRouter(component: React.ReactElement) {
3962
}
4063

4164
describe("Dashboard", () => {
42-
it("renders stat cards", () => {
65+
it("renders stat cards", async () => {
4366
renderWithRouter(<Dashboard />);
44-
expect(screen.getByText("Hosts Scanned")).toBeInTheDocument();
45-
expect(screen.getByText("Open Ports")).toBeInTheDocument();
46-
expect(screen.getByText("Services")).toBeInTheDocument();
47-
expect(screen.getByText("Findings")).toBeInTheDocument();
67+
await waitFor(() => {
68+
expect(screen.getByText("Total Hosts")).toBeInTheDocument();
69+
expect(screen.getByText("Open Ports")).toBeInTheDocument();
70+
expect(screen.getByText("Services")).toBeInTheDocument();
71+
expect(screen.getByText("Findings")).toBeInTheDocument();
72+
}, { timeout: 3000 });
4873
});
4974

50-
it("renders stat cards", () => {
75+
it("renders dashboard layout", async () => {
5176
renderWithRouter(<Dashboard />);
52-
// New implementation uses stat cards
53-
// We can just verify the dashboard renders without errors
77+
// Wait for data to load before checking layout
78+
await waitFor(() => {
79+
expect(screen.queryByText("No data available")).not.toBeInTheDocument();
80+
}, { timeout: 3000 });
5481
const dashboard = document.querySelector('.space-y-6');
5582
expect(dashboard).toBeTruthy();
5683
});
@@ -135,16 +162,19 @@ describe("Campaigns", () => {
135162
});
136163

137164
describe("Reports", () => {
138-
it("renders reports heading", () => {
165+
it("renders reports heading", async () => {
139166
renderWithRouter(<Reports />);
140-
expect(screen.getByText("Reports & Findings")).toBeInTheDocument();
167+
await waitFor(() => {
168+
expect(screen.getByText("Reports & Findings")).toBeInTheDocument();
169+
}, { timeout: 3000 });
141170
});
142171

143-
it("renders empty state when no findings", () => {
172+
it("renders empty state when no findings", async () => {
144173
renderWithRouter(<Reports />);
145174
// New implementation shows a loading state first, then empty state
146-
// We can just verify the page renders without errors
147-
expect(screen.getByText("Reports & Findings")).toBeInTheDocument();
175+
await waitFor(() => {
176+
expect(screen.getByText("Reports & Findings")).toBeInTheDocument();
177+
}, { timeout: 3000 });
148178
});
149179
});
150180

crates/spectre-gui/frontend/src/stores/dashboardStore.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ describe("dashboardStore", () => {
1717
expect(state.findings).toEqual([]);
1818
expect(state.findingsTotal).toBe(0);
1919
expect(state.findingsPagination).toEqual({ page: 1, per_page: 25 });
20-
expect(state.filters).toBeNull();
21-
expect(state.sort).toBeNull();
20+
expect(state.filters).toBeUndefined();
21+
expect(state.sort).toBeUndefined();
2222
});
2323

2424
it("should set filters", () => {
@@ -66,7 +66,7 @@ describe("dashboardStore", () => {
6666
useDashboardStore.getState().reset();
6767

6868
const state = useDashboardStore.getState();
69-
expect(state.filters).toBeNull();
69+
expect(state.filters).toBeUndefined();
7070
expect(state.stats).toBeNull();
7171
expect(state.findings).toEqual([]);
7272
expect(state.findingsPagination).toEqual({ page: 1, per_page: 25 });

0 commit comments

Comments
 (0)