Javascript 我怎样才能澄清一个被嘲笑的剧作家的反应?
根据,您可以模拟API调用 这对我来说很好,但我有两个测试,在同一个文件中有不同的模拟响应Javascript 我怎样才能澄清一个被嘲笑的剧作家的反应?,javascript,jestjs,playwright,Javascript,Jestjs,Playwright,根据,您可以模拟API调用 这对我来说很好,但我有两个测试,在同一个文件中有不同的模拟响应 submitMock = (response) => page.route(/submit/, (route) => route.fulfill({ status: status || 200, body: JSON.stringify(response), }), ); /// my test code describe('fill form'
submitMock = (response) => page.route(/submit/, (route) =>
route.fulfill({
status: status || 200,
body: JSON.stringify(response),
}),
);
/// my test code
describe('fill form', () => {
beforeEach(async () => {
jest.resetAllMocks(); // doesn't clear mock requests
await setupAppMocks(); // some basic mocks
await page.goto(
`${localhost}/?foo=bar`,
);
});
afterAll(async () => {
await browser.close();
});
it('should test scenario 1', async () => {
expect.hasAssertions();
const responseMock = { test: 'test' };
await submitMock(responseMock); // first mock created here
await fillForm();
await clickSubmit();
await page.waitForSelector('[class*="CongratulationsText"]');
const sectionText = await page.$eval(
'[class*="CongratulationsText"]',
(e) => e.textContent,
);
expect(sectionText).toBe(
`Congratulations, expecting message for scenario 1`,
);
});
it('should test scenario 2', async () => {
expect.hasAssertions();
const responseMock = { test: 'test 2' };
await submitMock(responseMock); // second mock created here
await fillForm();
await clickSubmit();
await page.waitForSelector('[class*="CongratulationsText"]');
const sectionText = await page.$eval(
'[class*="CongratulationsText"]',
(e) => e.textContent,
);
// fails because first mock is still active
expect(sectionText).toBe(
`Congratulations, expecting message for scenario 2`,
);
});
});
有没有办法清除以前的模拟
到目前为止,我尝试的基本上是添加另一个mock,但它似乎没有覆盖上一个。我假设问题在于无法覆盖同一模式的模拟,但我没有简单的方法来解决问题。取消API调用的方法是使用
page.unroute
“你可以解开你的路线。”哈德科德,你能解释一下怎么走吗?找到了这个我猜谢谢@hardkode的建议,它成功了
page.unroute(/submit/);