This directory contains the setup for testing react-router apps. It includes the setup.unit.tsx file which sets up the testing environment for unit tests.
The setup.unit.tsx file is used to set up the testing environment for unit tests in the following ways:
- It sets up the i18next instance with the
i18nconfiguration. - It extends your test context with the
renderStubfunction which is used to render the react-router routes. - It extends your test context with the
debugfunction which is used to debug your unit tests withjest-preview.
The renderStub function is used to render the react-router routes and return the container. It takes the following arguments:
props: Optional props to be passed into the react-router stub.entries: Optional entries to be passed into the react-router stub.i18n: Optional i18n configuration to be passed into the react-router stub.
The debug function is used to debug your unit tests with jest-preview. To use it, run your tests by running:
pnpm test:liveThis will run your tests in watch mode and you can use the debug function to debug your tests.
This will do two things:
- Start the
jest-previewserver. - Open the
jest-previewserver in your browser. - Start
vitestin UI mode.
You can use the debug function to debug your tests by adding the following code to your test:
it("should do something", ({ debug }) => {
// Your test code here
debug()
})This will render the tests current state into the jest-preview server and you can use the debug function to debug your tests.
- It sets up the i18next instance with the
i18nconfiguration. - It extends your test context with the
renderStubfunction which is used to render the react-router routes. - It extends your test context with the
debugfunction which is used to debug your unit tests withjest-preview. - It sets up the testing environment for unit tests
- Extract the
renderStubfunction from the vitest test context.
test("should do something", async ({ renderStub }) => {
// Your test code here
})- Use the
renderStubfunction to render the react-router routes.
test("should do something", async ({ renderStub }) => {
const { container } = await renderStub({
props: {
initialEntries: ["/"],
},
entries: [
{
id: "home",
path: "/",
Component: Home,
...Home,
},
],
})
})- Test your routes by using the
containerobject.
test("should do something", async ({ renderStub }) => {
const { container } = await renderStub({
props: {
initialEntries: ["/"],
},
entries: [
{
id: "home",
path: "/",
Component: Home,
...Home,
},
],
})
expect(container.queryByText("Home", { exact: false })).not.toBeNull()
})