在我使用Supertest和MSW进行的测试中,我注意到,尽管它们仍然成功通过,但MSW已经开始显示对Supertest正在发出的请求的警告。例如(请参阅文章末尾要复制的文件):
$ npm t
> msw-example@1.0.0 test
> jest
PASS ./app.test.js
password API
✓ exposes a number of words (76 ms)
console.warn
[MSW] Warning: captured a request without a matching request handler:
• GET http://127.0.0.1:55984/api
If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/getting-started/mocks
at onUnhandledRequest (node_modules/msw/node/lib/index.js:7599:21)
at node_modules/msw/node/lib/index.js:7630:13
at fulfilled (node_modules/msw/node/lib/index.js:50:58)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.005 s
Ran all test suites.
要求GET http://127.0.0.1:55984/api
是一个超级测试对应用程序,这是整个测试的重点,而不是一个垃圾需要处理。这些警告在我第一次编写测试时也没有显示出来。
链接页面显示了如何创建处理程序,但我不希望MSW处理这些请求。为什么会发生这种情况,我如何才能停止显示"/api"
调用的警告?
package.json
{
"name": "msw-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"express": "^4.17.1"
},
"devDependencies": {
"jest": "^27.0.4",
"msw": "^0.29.0",
"supertest": "^6.1.3"
}
}
app.js
const axios = require("axios");
const express = require("express");
const app = express();
app.get("/api", (_, res) => {
axios.get("https://api.pwnedpasswords.com/range/ABC12")
.then(() => res.json({ words: 3 }))
.catch((err) => res.sendStatus(500));
});
module.exports = app;
app.test.js
const { rest } = require("msw");
const { setupServer } = require("msw/node");
const request = require("supertest");
const app = require("./app");
const server = setupServer(
rest.get("https://api.pwnedpasswords.com/range/:range", (req, res, ctx) => {
return res(ctx.status(200), ctx.text(""));
}),
);
describe("password API", () => {
beforeAll(() => server.listen());
beforeEach(() => server.resetHandlers());
afterAll(() => server.close());
it("exposes a number of words", () => {
return request(app).get("/api").expect(200).then((res) => {
expect(res.body.words).toBe(3);
});
});
});
发布于 2021-06-17 18:42:45
这一特性是在MSW v0.20.0中引入的,但在v0.29.0中,未处理请求的默认设置从"bypass"
更改为"warn"
,因此控制台中突然出现警告。您可以将其重置为"bypass"
,如setupWorker#start
或setupServer#listen
的docs所示,在我的例子中:
beforeAll(() => server.listen({ onUnhandledRequest: "bypass" }));
但是,这可能意味着您应该处理的请求缺少警告,因此另一个选项是传递一个接收请求对象的函数。这可以例如记录警告或抛出错误(这将导致测试失败)。在我的例子中,由于我所有的超级测试请求都是指向/api
端点的,这看起来如下所示:
beforeAll(() => server.listen({
onUnhandledRequest: ({ method, url }) => {
if (!url.pathname.startsWith("/api")) {
throw new Error(`Unhandled ${method} request to ${url}`);
}
},
}));
正如凯塔纳托在评论中所建议的那样,我研究了是否可以通过它们的头来识别超级测试调用。不幸的是,超级测试不再似乎设置了一个默认的User-Agent
,因此您必须执行这个逐个测试:
describe("password API", () => {
beforeAll(() => server.listen({
onUnhandledRequest: ({ headers, method, url }) => {
if (headers.get("User-Agent") !== "supertest") {
throw new Error(`Unhandled ${method} request to ${url}`);
}
},
}));
beforeEach(() => server.resetHandlers());
afterAll(() => server.close());
it("exposes a number of words", () => {
return request(app)
.get("/api")
.set("User-Agent", "supertest")
.expect(200)
.then((res) => {
expect(res.body.words).toBe(3);
});
});
});
在v0.38.0中,您可以使用onUnhandledRequest
的第二个参数(通常命名为print
),在您不想处理的情况下,将控制返回给垃圾,例如:
beforeAll(() => server.listen({
onUnhandledRequest: ({ headers }, print) => {
if (headers.get("User-Agent") === "supertest") {
return;
}
print.error();
},
}));
发布于 2022-08-28 16:29:22
对于任何使用工作者而不是服务器的人,您可以将“onUnhandledRequest”字段设置为“旁路”的对象传递到工人的“start”方法中,如下所示:
worker.start({
onUnhandledRequest: 'bypass',
});
其他选项包括“警告”和“错误”https://mswjs.io/docs/api/setup-worker/start#onunhandledrequest
https://stackoverflow.com/questions/68024935
复制相似问题