Cześć, piszę testy do aplikacji w nextjs:
import ContactsFirstPageResponse from '@mocks/contacts/firstPage.json';
import ContactsSecondPageResponse from '@mocks/contacts/secondPage.json';
import ContactsEmptyResponse from '@mocks/contacts/empty.json';
const spy = jest.spyOn(axios, 'get');
describe('Contacts', () => {
afterEach(() => {
// tutaj sprawdzałem różne możliwości resetowania mocków, ale żadna nie działa
spy.mockReset();
spy.mockClear();
spy.mockRestore();
jest.clearAllMocks();
jest.restoreAllMocks();
jest.resetAllMocks();
});
it('show empty list component', async () => {
spy.mockResolvedValue(ContactsEmptyResponse);
// ...
});
it('render 10 contacts and fetch 10 more on click', async () => {
spy.mockResolvedValue((url: string) => {
if (url.includes('?page=1')) {
return ContactsFirstPageResponse;
} else {
return ContactsSecondPageResponse;
}
});
// ...
});
});
Jest config:
const nextJest = require('next/jest');
const createJestConfig = nextJest({
dir: './',
});
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
clearMocks: true,
};
module.exports = createJestConfig(customJestConfig);
Problem polega na tym, że pierwszy test dostaje response z pliku ContactsEmptyResponse (i test przechodzi, czyli wszystko ok), ale już drugi test również dostaje ContactsEmptyResponse zamiast ContactsFirstPageResponse lub ContactsSecondPageResponse