在为我的代码库编写单元测试时,我注意到我正在复制执行 HTTP 请求并添加对响应的期望的代码。所以我最终得到了一些辅助函数,其中一个称为:
expectedResponseForGET
,我在 TestUtils
类中将其静态化,在一个名为:utils.ts
的文件中。
在某些测试中,我多次调用该方法以在 POST 后检查多个内容。但是,当该期望失败时(响应不是我期望的),测试失败,并且调用堆栈显示它未达到
expectedResponseForGET
函数的期望,而没有调用该辅助函数的测试的调用堆栈。
我收到的失败期望的调用堆栈如下所示:
src/tests/sites/add.test.ts
Add site route
✓ User4 - Add site, not company manager (238 ms)
✕ User1 - Add site, company manager (269 ms)
● Add site route › User1 - Add site, company manager
expect(received).toEqual(expected) // deep equality
- Expected - 1
+ Received + 1
@@ -1,63 +1,10 @@
- "location": "Location2",
+ "location": "Location",
92 | .set('Authorization', user.token);
93 | expect(res.status).toBe(200);
> 94 | expect(res.body).toEqual(response);
| ^
95 | return res;
96 | };
97 |
at src/tests/utils.ts:94:22
at fulfilled (src/tests/utils.ts:5:58)
这部分:
at src/tests/utils.ts:94:22 -> This is for the line containing the expectation
at fulfilled (src/tests/utils.ts:5:58) -> This is for the import of request from supertest
其中不包含
src/tests/sites/add.test.ts
测试文件中名为 expectedResponseForGET
的行。
这是测试:
test(' User1 - Add site, company manager', async () => {
await TestUtils.expectedResponseForPOST(
server,
C1.users.U1,
route,
C1.sites.S5.requests.addSite,
C1.sites.S5.responses.fullSite,
);
await TestUtils.expectedResponseForGET(
server,
C1.users.U1,
routeSites,
C1.sites.responses.allAfterS5,
);
await TestUtils.expectedResponseForGET(
server,
C1.users.U1,
routeWork,
C1.work.responses.allAfterS5,
);
});
以下是辅助函数:
import request from 'supertest';
class TestUtils{
static expectedResponseForGET = async (
server: Express,
user: UserTest,
route: string,
response: GenericResponse,
) => {
const res = await request(server)
.get(route)
.set('Authorization', user.token);
expect(res.status).toBe(200);
expect(res.body).toEqual(response);
return res;
};
static expectedResponseForPOST = async (
server: Express,
user: UserTest,
route: string,
// eslint-disable-next-line @typescript-eslint/ban-types
req: Object,
response: GenericResponse,
) => {
const res = await request(server)
.post(route)
.send(req)
.set('Authorization', user.token);
expect(res.status).toBe(200);
expect(res.body).toEqual(response);
return res;
};
}
有人遇到过这个问题并且知道解决这个问题的方法或者解决方法来显示测试中调用辅助方法的行吗?
我设法找到了解决方案,将堆栈保存在该 utils 方法中并将其附加到测试失败时引发的错误:
static expectedResponseForPOST = async (
server: Express,
user: UserTest,
route: string,
// eslint-disable-next-line @typescript-eslint/ban-types
req: Object,
response: GenericResponse,
) => {
const originalStack = new Error().stack;
try {
const res = await request(server)
.post(route)
.send(req)
.set('Authorization', user.token);
expect(res.status).toBe(200);
expect(res.body).toEqual(response);
return res;
}catch (err) {
if (err instanceof Error) {
err.stack = `${err.stack}\nComplete Stack:\n${originalStack}`;
}
throw err;
}
};
这将导致像这样的日志记录:
at src/tests/utils.ts:140:26
at fulfilled (src/tests/utils.ts:5:58)
Complete Stack:
at src/tests/utils.ts:131:27
at src/tests/utils.ts:8:71
at Object.<anonymous>.__awaiter (src/tests/utils.ts:4:12)
at Function.Object.<anonymous>.TestUtils.expectedResponseForPOST (src/tests/utils.ts:118:7)
at Function.<anonymous> (src/tests/data/models/WarehouseEventTest.ts:39:21)
at src/tests/data/models/WarehouseEventTest.ts:8:71
at Object.<anonymous>.__awaiter (src/tests/data/models/WarehouseEventTest.ts:4:12)
at Function.checkIncrementalEventsAfterAction (src/tests/data/models/WarehouseEventTest.ts:24:16)
at Function.<anonymous> (src/tests/data/models/WarehouseEventTest.ts:20:30)
at src/tests/data/models/WarehouseEventTest.ts:8:71
at Object.<anonymous>.__awaiter (src/tests/data/models/WarehouseEventTest.ts:4:12)
at Function.checkIncrementalEventAfterAction (src/tests/data/models/WarehouseEventTest.ts:18:16)
at src/tests/warehouse/warehouseItem.test.ts:166:30
at fulfilled (src/tests/warehouse/warehouseItem.test.ts:5:58)
哪个是完整的堆栈