Heroes App ships with a singleDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ludwiigdev/Heroes_App/llms.txt
Use this file to discover all available pages before exploring further.
npm test command that starts Jest in interactive watch mode, making it quick to iterate while developing. This page covers every way to invoke the test suite — from a full watched run to targeted single-file execution — and then walks through writing both a pure helper test and a component test that requires router and context setup.
Running the Test Suite
Available commands
| Command | What it does |
|---|---|
npm test | Starts Jest in --watchAll mode (re-runs on every file save) |
npx jest --watchAll=false | Runs the full suite once and exits — ideal for CI pipelines |
npx jest --coverage | Runs the full suite and generates a coverage report in coverage/ |
npx jest tests/auth/context/authReducer.test.js | Runs a single test file by path |
The
"test" script in package.json is defined as "jest --watchAll", so npm test will never exit on its own. Use npx jest --watchAll=false in CI environments or pre-commit hooks.Watch Mode Keyboard Shortcuts
When Jest is running in watch mode it displays an interactive menu. These keyboard shortcuts let you narrow the run without restarting:| Key | Action |
|---|---|
a | Run all tests |
f | Run only failing tests |
p | Filter by filename pattern |
t | Filter by test name pattern |
u | Update failing snapshots |
q | Quit watch mode |
Writing a New Unit Test (Helper Function)
The following walkthrough creates a test file forgetHeroesByName — a helper that filters the hero list by a partial name match. It returns an empty array when given an empty string, a filtered array on a partial match, and an empty array when nothing matches.
Create the test file
Mirror the source path under
tests/. The helper lives at src/Heroes/helpers/getHeroesByName.js, so the test file goes at tests/Heroes/helpers/getHeroesByName.test.js.Import the function under test
Open the new file and add the import. Use the same module path style as the rest of the project.
Open a describe block
Group all related cases under a single
describe so the output is easy to read in the terminal.Test the empty-string case
When called with an empty string the function should return an empty array, preventing an accidental full-list dump.
Test a partial match
A query of
"bat" should match at least Batman. Check that the returned array is non-empty and that every entry’s superhero field contains the search term (case-insensitive).Test the no-match case
A query that matches nothing should return an empty array, not
undefined or null.Writing a Component Test (with AuthContext)
Components that read fromAuthContext and use React Router hooks need a bit more setup than pure helpers. The pattern below is extracted directly from Navbar.test.jsx and works for any component with the same dependencies.
The setup
Two things must be in place before rendering:- Mock
useNavigateat the module level so the function returned by the hook is a trackablejest.fn()instead of the real router implementation. - Wrap the component in both
AuthContext.Provider(to supply the context value) andMemoryRouter(to satisfy any internal<Link>oruseNavigatecalls).
Common Gotchas
Must mock useNavigate for components that call it
Must mock useNavigate for components that call it
Must wrap routing-dependent components in MemoryRouter
Must wrap routing-dependent components in MemoryRouter
Any component that renders a Use
<Link>, <NavLink>, <Navigate>, or calls a router hook (useParams, useLocation, useNavigate) will throw “You should not use <Link> outside a <Router>” if rendered without a router context. Always wrap with <MemoryRouter>.initialEntries to simulate a specific URL when the component reads from useSearchParams or useLocation.CSS imports and identity-obj-proxy
CSS imports and identity-obj-proxy
Vite handles CSS Modules at build time, but Jest doesn’t run through Vite. Without a mock,
import styles from './Hero.module.css' would throw a syntax error. identity-obj-proxy is installed as a project dependency and can be wired up to intercept those imports — but it is not currently configured in jest.config.cjs (no moduleNameMapper key is present). If you add components that import CSS Modules, add the mapper manually: