为什么这些模拟不重置?
我正在使用NextJ和Jest和React测试库进行测试。我使用 nock 用于模拟Axios响应。
我的测试:
axios.defaults.adapter = require('axios/lib/adapters/http');
describe('Contacts component', () => {
beforeEach(() => {
nock.disableNetConnect();
nock(BACKEND_URL).defaultReplyHeaders(nockReplyHeaders).options('/api/user').reply(200);
nock(BACKEND_URL).defaultReplyHeaders(nockReplyHeaders).get('/api/user').reply(200, RootUserJson);
});
afterEach(() => {
// mock resetting
nock.restore();
});
it('loads first contacts list', async () => {
// these mocks works fine
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.options(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200);
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.get(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200, ContactsFirstPageJson);
render(
<Contacts />
);
// some expects..
});
it('loads empty list and show empty component', async () => {
// these mocks not returns EmptyJson, it return json from first test
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.options(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200);
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.get(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200, EmptyJson);
render(
<Contacts />
);
const emptyComponent = await screen.findByText('No contacts, add some friends');
expect(emptyComponent).toBeInTheDocument();
});
});
嘲笑重置不起作用。来自第二个测试的模拟返回第一次测试的JSON。我想在每个 it()之后重置模拟。请帮助。
I am making app with Nextjs and Jest and React Testing Library for testing. I use nock for mock axios responses.
My tests:
axios.defaults.adapter = require('axios/lib/adapters/http');
describe('Contacts component', () => {
beforeEach(() => {
nock.disableNetConnect();
nock(BACKEND_URL).defaultReplyHeaders(nockReplyHeaders).options('/api/user').reply(200);
nock(BACKEND_URL).defaultReplyHeaders(nockReplyHeaders).get('/api/user').reply(200, RootUserJson);
});
afterEach(() => {
// mock resetting
nock.restore();
});
it('loads first contacts list', async () => {
// these mocks works fine
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.options(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200);
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.get(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200, ContactsFirstPageJson);
render(
<Contacts />
);
// some expects..
});
it('loads empty list and show empty component', async () => {
// these mocks not returns EmptyJson, it return json from first test
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.options(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200);
nock(BACKEND_URL)
.defaultReplyHeaders(nockReplyHeaders)
.get(`/api/friendship/friends/${RootUserJson.id}?page=1`)
.reply(200, EmptyJson);
render(
<Contacts />
);
const emptyComponent = await screen.findByText('No contacts, add some friends');
expect(emptyComponent).toBeInTheDocument();
});
});
Mocks resetting not working. Mock from second tests returns json from first test. I want to reset mock after each it(). Please for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论