Skip to content

Commit

Permalink
Merge pull request #113 from gfa-cc-after/fix/react-test-imports-and-…
Browse files Browse the repository at this point in the history
…globals

fix: fix test imports and mock types
  • Loading branch information
AnnaUgrai authored Sep 20, 2024
2 parents 178c97f + ff5bdf5 commit aaefc91
Show file tree
Hide file tree
Showing 13 changed files with 1,761 additions and 1,594 deletions.
10 changes: 10 additions & 0 deletions frontend/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
}
}
}
},
{
"include": ["*.test.tsx"],
"linter": {
"rules": {
"style": {
"noNamespaceImport": "off"
}
}
}
}
]
}
3,177 changes: 1,671 additions & 1,506 deletions frontend/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"axios": "^1.7.4",
"framer-motion": "^11.3.30",
"jwt-decode": "^4.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons-kit": "^2.0.0",
"validator": "^13.12.0",
"zustand": "^4.5.4"
Expand All @@ -41,11 +39,13 @@
"jsdom": "^24.1.0",
"lint-staged": "^15.2.8",
"msw": "^2.3.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.23.1",
"ts-node": "^10.9.2",
"typescript": "^5.2.2",
"typescript": "^5.6.2",
"vite": "^5.4.6",
"vitest": "^2.0.2",
"vitest": "^2.1.1",
"vitest-dom": "^0.1.1"
}
}
7 changes: 6 additions & 1 deletion frontend/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/// <reference types="vitest-dom/extend-expect" />

import "vitest-dom/extend-expect";

import { afterAll, afterEach, beforeAll } from "vitest";
import { server } from "./src/mocks/server";

// Establish API mocking before all tests.
beforeAll(() => server.listen());
beforeAll(() => {
server.listen();
});

// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./App.css";
import { RouterProvider, createBrowserRouter } from "react-router-dom";
import { Login } from "./pages/Login";
import { Main } from "./pages/Main";
import PetList from "./pages/PetList";
import { PetList } from "./pages/PetList";
import { ProfileDeletion } from "./pages/ProfileDeletion";
import { ProfileDetail } from "./pages/ProfileDetail";
import { ProfileUpdate } from "./pages/ProfileUpdate";
Expand Down
17 changes: 12 additions & 5 deletions frontend/src/pages/PetList.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { render, screen, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { petList } from "../httpClient";
import PetList from "./PetList";
import type { AxiosResponse } from "axios";
import { type Mocked, describe, expect, it, vi } from "vitest";
import * as httpClient from "../httpClient";
import { PetList } from "./PetList";

// Mock the petList function
vi.mock("../httpClient", () => ({
petList: vi.fn(),
}));

const mockHttpClient = httpClient as Mocked<typeof httpClient>;

const mockPets = [
{
name: "Buddy",
Expand All @@ -29,7 +32,9 @@ const mockPets = [

describe("PetList Component", () => {
it("displays pets in a table when data is available", async () => {
(petList as vi.mock).mockResolvedValueOnce({ data: { pets: mockPets } });
mockHttpClient.petList.mockResolvedValue({
data: { pets: mockPets },
} as AxiosResponse);

render(<PetList />);

Expand All @@ -42,7 +47,9 @@ describe("PetList Component", () => {
});

it("displays a message when no pets are registered", async () => {
(petList as vi.mock).mockResolvedValueOnce({ data: { pets: [] } });
mockHttpClient.petList.mockResolvedValue({
data: { pets: [] },
} as AxiosResponse);

render(<PetList />);

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/PetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ const PetList = () => {
);
};

export default PetList;
export { PetList };
1 change: 0 additions & 1 deletion frontend/src/pages/ProfileDeletion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ vi.mock("../httpClient", () => ({
deleteProfile: vi.fn(),
}));

// biome-ignore lint: used it for mocking purposes
import * as httpClient from "../httpClient";

describe("ProfileDeletion", () => {
Expand Down
16 changes: 13 additions & 3 deletions frontend/src/pages/ProfileUpdate.test.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { MockedFunction } from "vitest";
import { useProfileUpdateState } from "../hooks/useProfileUpdate";
import { ProfileUpdate } from "./ProfileUpdate.tsx";

vi.mock("../hooks/useProfileUpdate");

const mockUseProfileUpdateState = useProfileUpdateState as vi.MockedFunction<
const mockUseProfileUpdateState = useProfileUpdateState as MockedFunction<
typeof useProfileUpdateState
>;

describe("ProfileUpdate", () => {
beforeEach(() => {
mockUseProfileUpdateState.mockReturnValue({
state: { user: { email: "", firstName: "", lastName: "", password: "" } },
updateUserField: vi.fn(),
updateUserProfile: vi.fn(),
navigate: vi.fn(),
state: {
user: {
email: "",
firstName: "",
lastName: "",
password: "",
},
errorMessage: null,
message: null,
},
});
});

Expand Down
8 changes: 1 addition & 7 deletions frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
/* Needed for the Vitest */
"types": ["vitest/globals"]
},
"include": ["src", "src/tests/setup.ts"],
"exclude": [
"node_modules",
"src/tests/setup.ts",
"src/**/*.test.ts",
"src/**/*.test.tsx"
],
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
30 changes: 0 additions & 30 deletions frontend/tsconfig.test.json

This file was deleted.

35 changes: 0 additions & 35 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,8 @@
import react from "@vitejs/plugin-react-swc";
import { defineConfig } from "vite";

const threshold: number = 70 as const;

// https://vitejs.dev/config/
// biome-ignore lint: viteconfig signature
export default defineConfig({
plugins: [react()],
test: {
globals: true,
include: ["**/*.test.ts", "**/*.test.tsx"],
environment: "jsdom",
setupFiles: ["./setup-tests.ts"],
css: true,
typecheck: {
tsconfig: "./tsconfig.test.json",
},
coverage: {
exclude: [
"src/**/*.test.tsx",
"src/**/*.test.ts",
"src/main.tsx",
"vite.config.ts",
"dist/**/*",
"node_modules/**/*",
"coverage/**/*",
".eslintrc.cjs",
"src/**/*.d.ts",
],
provider: "v8",
reporter: ["text", "json", "html"],
thresholds: {
"src/**/*.{ts,tsx}": {
functions: threshold,
branches: threshold,
statements: threshold,
lines: threshold,
},
},
},
},
});
42 changes: 42 additions & 0 deletions frontend/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import react from "@vitejs/plugin-react-swc";
import { defineConfig } from "vitest/config";

const threshold: number = 70 as const;

// biome-ignore lint: define config sigrature
export default defineConfig({
plugins: [react()],
test: {
globals: true,
// include: ["**/*.test.ts", "**/*.test.tsx"],
environment: "jsdom",
setupFiles: ["./setup-tests.ts"],
css: true,
typecheck: {
tsconfig: "./tsconfig.test.json",
},
coverage: {
exclude: [
"src/**/*.test.tsx",
"src/**/*.test.ts",
"src/main.tsx",
"vite.config.ts",
"dist/**/*",
"node_modules/**/*",
"coverage/**/*",
".eslintrc.cjs",
"src/**/*.d.ts",
],
provider: "v8",
reporter: ["text", "json", "html"],
thresholds: {
"src/**/*.{ts,tsx}": {
functions: threshold,
branches: threshold,
statements: threshold,
lines: threshold,
},
},
},
},
});

0 comments on commit aaefc91

Please sign in to comment.