jest
is the primary testing framework, testing-library
as react-testing framework
and msw
for mocking the network requests/responses.
msw is used to mock the network layer
For mocking a request globally:
- Create a fixture object for your mocked data -
mockedData.fixtures.js
- Add an handler request in
/mocks/handler
import mockedData from './mockedData.fixtures.js';
export const handlers = [
// ...other handlers
rest.get(provisioningUrl('<YOUR_ENDPOINT>'), (req, res, ctx) => {
return res(ctx.status(200), ctx.json(mockedData));
}),
];
When tests try to fetch /<YOUR_ENDPOINT>
it will return the mocked data
Sometimes we'd like to override a response handler in a specific test, for example testing errors or network delays:
test('Loading state', async () => {
const { server, rest } = window.msw;
// this override the the request only in this test and simulates delay network
server.use(
rest.get(provisioningUrl('<YOUR_ENDPOINT>'), (req, res, ctx) => {
return res(ctx.delay(10000), ctx.status(200));
})
);
}
In order to access WizardContext
and react-query
provider, the render
function of testing-library
is altered with a wrapper.
You can import all testing-library
related functions from mocks/utils
import { render, screen } from '../../mocks/utils';
test('Loading state', async () => {
// the component will be rended with wizard context and react-query provider
render(<Component />)
}
If needed, import render
directly from @testing-library/react