SIn*_*Sim 3 javascript node.js typescript supertest jestjs
我是后端开发的新手,我面临一个我不明白的问题。
我设置了 API 的第一个路由,名为“health”,它只返回一条简单的消息来了解我的服务器是否已启动。
这条路线看起来按预期工作。
然而,
当我尝试使用 jest API 中的“toMatchSnapshot”方法测试此路由时,测试未通过,因为 url 中的内容不断变化。
我的测试文件“index.test.ts”:
const request = supertest.agent(app);
describe("app", () => {
it("should return a successful response for GET /health", async () => {
const res = await request.get("/health");
res.header = omit(res.header, ["date"]);
expect(res).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
服务器“index.ts”的索引:
const app = express();
expressService(app);
if (require.main === module) {
app.listen(PORT, () => {
console.log("server started at http://localhost:" + PORT);
});
}
export default app;
Run Code Online (Sandbox Code Playgroud)
我的功能“expressService”:
const expressService = (app: Application) => {
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(api);
};
export default expressService;
Run Code Online (Sandbox Code Playgroud)
我的端口变量:PORT = 3000;
- "url": "http://127.0.0.1:49694/health",
+ "url": "http://127.0.0.1:52568/health",
Run Code Online (Sandbox Code Playgroud)
这就是测试失败的地方。
谢谢您的回答。
的文档supertest说:
\n\n\n您可以将 http.Server 或 Function 传递给 request() - 如果服务器尚未侦听连接,那么它会为您绑定到临时端口,因此无需跟踪端口。
\n
您需要将 Node.jshttp.Server对象传递给supertest.agent(),然后您可以使用特定的PORT进行测试。
这是解决方案:
\n\nindex.ts:
import express from \'express\';\nimport expressService from \'./expressService\';\nimport http from \'http\';\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\nexpressService(app);\n\nfunction createHttpServer() {\n const httpServer: http.Server = app.listen(PORT, () => {\n console.log(\'server started at http://localhost:\' + PORT);\n });\n\n return httpServer;\n}\n\nif (require.main === module) {\n createHttpServer();\n}\n\nexport default createHttpServer;\nRun Code Online (Sandbox Code Playgroud)\n\nexpressService.ts:
import { Application } from \'express-serve-static-core\';\nimport express, { Router } from \'express\';\nimport cors from \'cors\';\n\nconst expressService = (app: Application) => {\n app.use(cors());\n app.use(express.urlencoded({ extended: true }));\n app.use(express.json());\n\n const api = Router();\n\n api.get(\'/health\', (req, res) => {\n res.sendStatus(200);\n });\n\n app.use(api);\n};\n\nexport default expressService;\nRun Code Online (Sandbox Code Playgroud)\n\nindex.spec.ts:
import createHttpServer from \'./\';\nimport { agent } from \'supertest\';\nimport { omit } from \'lodash\';\n\nconst httpServer = createHttpServer();\nconst request = agent(httpServer);\n\nafterAll(done => {\n httpServer.close(done);\n});\n\ndescribe(\'app\', () => {\n it(\'should return a successful response for GET /health\', async () => {\n const res = await request.get(\'/health\');\n res.header = omit(res.header, [\'date\']);\n expect(res).toMatchSnapshot();\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n单元测试结果:
\n\n PASS src/stackoverflow/57409561/index.spec.ts (7.853s)\n app\n \xe2\x9c\x93 should return a successful response for GET /health (61ms)\n\n console.log src/stackoverflow/57409561/index.ts:12\n server started at http://localhost:3000\n\nTest Suites: 1 passed, 1 total\nTests: 1 passed, 1 total\nSnapshots: 1 passed, 1 total\nTime: 8.66s\nRun Code Online (Sandbox Code Playgroud)\n\n快照:
\n\n// Jest Snapshot v1\n\nexports[`app should return a successful response for GET /health 1`] = `\nObject {\n "header": Object {\n "access-control-allow-origin": "*",\n "connection": "close",\n "content-length": "2",\n "content-type": "text/plain; charset=utf-8",\n "etag": "W/\\\\"2-nOO9QiTIwXgNtWtBJezz8kv3SLc\\\\"",\n "x-powered-by": "Express",\n },\n "req": Object {\n "data": undefined,\n "headers": Object {\n "user-agent": "node-superagent/3.8.3",\n },\n "method": "GET",\n "url": "http://127.0.0.1:3000/health",\n },\n "status": 200,\n "text": "OK",\n}\n`;\nRun Code Online (Sandbox Code Playgroud)\n\n这是完成的演示: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57409561
\n| 归档时间: |
|
| 查看次数: |
7144 次 |
| 最近记录: |