Repository navigation
test(frontend): comprehensive snapshot tests for all screens - #64
Conversation
The frontend had behavioral tests but no snapshot coverage. Add Jest + React Testing Library snapshot tests for every screen (17): LandingPage, Login, Register, ResetPassword, VerifyUsername, Dashboard, EmployeeList, EmployeeForm, DepartmentList, DepartmentForm, NewDepartmentForm, Profile, Passkeys, NotFoundPage, QuickActions, Navbar, and Footer. Each test renders the screen inside a MemoryRouter with its services mocked and asserts the rendered markup with toMatchSnapshot(), so unintended UI changes surface as a diff. Harness (jest.setup.js): add jsdom polyfills (matchMedia, scrollTo, scrollIntoView, Resize/IntersectionObserver) and neutralize autoFocus so focus-sensitive markup is stable across environments. Determinism (passes identically on local + CI regardless of when/where): - LandingPage: stub the WebGL 3D hero background. - Dashboard: stub react-chartjs-2 (charts need a canvas/GPU). - Footer: freeze Date (renders the current year). - Data screens: services mocked as pending so they render their deterministic loading state offline (no async act races). Verified: full suite 25 suites / 37 tests / 17 snapshots green in CI mode (the existing 8 behavioral suites still pass), and the snapshot suites pass against the committed baselines under UTC, Pacific/Kiritimati (UTC+14), and Pacific/Pago_Pago (UTC-11). No new dependencies. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
✅ Deploy Preview for employee-management-fullstack-system ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
There was a problem hiding this comment.
Code Review
This pull request introduces comprehensive frontend snapshot testing using Jest and React Testing Library, adding snapshot suites and baseline files for all major screens, updating README files with instructions, and configuring global jsdom polyfills in 'jest.setup.js'. The review feedback recommends three key improvements: conditionally mocking 'HTMLElement.prototype.focus' in 'jest.setup.js' to prevent polluting behavioral tests, using Jest's native fake timers and system time APIs in the Footer snapshot test instead of manually mutating the global 'Date' class, and mocking 'authService' in the Employee and Department list snapshot tests to ensure determinism.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| // autoFocus toggles MUI focus classes, and jsdom applies it inconsistently | ||
| // across environments (focuses locally but not on CI), which would make | ||
| // focus-sensitive snapshots flaky. Neutralize focus so the rendered markup | ||
| // is the unfocused state everywhere. | ||
| window.HTMLElement.prototype.focus = () => {}; |
There was a problem hiding this comment.
Neutralizing HTMLElement.prototype.focus globally for all tests can cause silent failures or unexpected behaviors in behavioral tests that rely on focus state or user interactions (e.g., checking expect(element).toHaveFocus() or using userEvent which triggers focus events).
To prevent test pollution and keep behavioral tests working correctly, we can conditionally apply this mock only for snapshot tests by checking the current test file path using expect.getState().testPath.
// autoFocus toggles MUI focus classes, and jsdom applies it inconsistently
// across environments (focuses locally but not on CI), which would make
// focus-sensitive snapshots flaky. Neutralize focus so the rendered markup
// is the unfocused state everywhere.
const originalFocus = window.HTMLElement.prototype.focus;
beforeEach(() => {
const testPath = expect.getState().testPath;
if (testPath && testPath.includes('.snapshot.test.')) {
window.HTMLElement.prototype.focus = () => {};
} else {
window.HTMLElement.prototype.focus = originalFocus;
}
});| const RealDate = Date; | ||
| const FIXED_ISO = '2024-06-15T12:00:00.000Z'; | ||
|
|
||
| beforeAll(() => { | ||
| global.Date = class extends RealDate { | ||
| constructor(...args) { | ||
| super(...(args.length ? args : [FIXED_ISO])); | ||
| } | ||
| static now() { | ||
| return new RealDate(FIXED_ISO).getTime(); | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| global.Date = RealDate; | ||
| }); |
There was a problem hiding this comment.
Manually overriding the global Date class can be risky and may lead to subtle bugs or prototype pollution if not cleaned up perfectly.
Since you are using Jest 27, you can use the built-in, standard, and safe jest.useFakeTimers() and jest.setSystemTime() APIs to freeze time deterministically without mutating the global Date constructor.
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date('2024-06-15T12:00:00.000Z'));
});
afterAll(() => {
jest.useRealTimers();
});| jest.mock('../../src/services/departmentService', () => ({ | ||
| getAllDepartments: () => new Promise(() => {}), | ||
| getDepartmentById: () => new Promise(() => {}), | ||
| addDepartment: jest.fn(), | ||
| updateDepartment: jest.fn(), | ||
| deleteDepartment: jest.fn(), | ||
| })); |
There was a problem hiding this comment.
Unlike Dashboard.snapshot.test.jsx and Profile.snapshot.test.jsx, this test suite does not mock authService.
To ensure test determinism, consistency, and to prevent potential flakiness or dependency on the real authService implementation (which might access localStorage or make network calls), please mock authService here as well. The same applies to DepartmentList.snapshot.test.jsx.
jest.mock('../../src/services/departmentService', () => ({
getAllDepartments: () => new Promise(() => {}),
getDepartmentById: () => new Promise(() => {}),
addDepartment: jest.fn(),
updateDepartment: jest.fn(),
deleteDepartment: jest.fn(),
}));
jest.mock('../../src/services/authService', () => ({
getToken: () => null,
getUsername: () => '',
isAuthenticated: () => false,
setSession: jest.fn(),
clearSession: jest.fn(),
subscribeAuth: () => () => {},
}));
What
The frontend had behavioral tests but no snapshot coverage. This adds Jest + React Testing Library snapshot tests for all 17 screens:
LandingPage, Login, Register, ResetPassword, VerifyUsername, Dashboard, EmployeeList, EmployeeForm, DepartmentList, DepartmentForm, NewDepartmentForm, Profile, Passkeys, NotFoundPage, QuickActions, Navbar, Footer.
Each test renders the screen inside a
MemoryRouterwith its services mocked and asserts the rendered markup withtoMatchSnapshot()— so unintended UI changes surface as a diff. Tests live infrontend/__tests__/snapshots/.Harness
jest.setup.jsgains jsdom polyfills (matchMedia,scrollTo,scrollIntoView,Resize/IntersectionObserver) and a globalautoFocusneutralization (jsdom applies focus inconsistently across environments). No config or dependency changes otherwise.Determinism — passes identically on local + CI regardless of when/where
react-chartjs-2stubbed (charts need a canvas/GPU).Datefrozen (renders the current year).Verification
Snapshot suites verified against the committed baselines under UTC, Pacific/Kiritimati (UTC+14), and Pacific/Pago_Pago (UTC-11) — all pass. The 8 existing behavioral suites still pass with the updated setup.
Docs updated: root
README.mdandfrontend/README.mdtesting sections now document the snapshot suites and how to run/update them. No new dependencies.🤖 Generated with Claude Code