如何在超级测试失败时打印请求日志(如请求url、请求正文、请求queryParam)



我想打印或控制台下面关于Supertest失败的详细信息对于以下关于成功的请求,无需在成功上打印任何东西

const result = await request(dummy_url).get("repos/Microsoft/TypeScript/pulls")
.set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0")
.expect(200)
.then(response => {
console.log("success");
console.log(response);
return response;
}).catch(error => {
console.log("error");
// console.log(error);
return 2;
})
console.log(result)
done();

如果失败,假设我将url修改为CCD_ 1而不是CCD_,我需要知道请求的url,路径parma和请求体(如果有的话(,

目前,试图以上述方式添加它,它只会给出以下错误

Error: expected 200 "OK", got 404 "Not Found"
at Object.<anonymous> (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/__tests__/github-routes/jest.test.ts:40:8)
at /Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/jest-jasmine2/build/queueRunner.js:45:12
at new Promise (<anonymous>)
at mapper (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/jest-jasmine2/build/queueRunner.js:28:19)
at /Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/jest-jasmine2/build/queueRunner.js:75:41
at processTicksAndRejections (node:internal/process/task_queues:96:5)
----
at Test.Object.<anonymous>.Test._assertStatus (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:296:12)
at /Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:80:15
at Test.Object.<anonymous>.Test._assertFunction (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:311:11)
at Test.Object.<anonymous>.Test.assert (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:201:21)
at localAssert (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:159:12)
at fn (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:156:5)
at Test.callback (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/superagent/src/node/index.js:902:3)
at fn (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/superagent/src/node/index.js:1130:18)
at IncomingMessage.<anonymous> (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/superagent/src/node/parsers/json.js:19:7)
at Stream.emit (node:events:365:28)

需要注意的事项,我在Typescript中使用了这个,在异步函数中,我认为它现在不是一个阻塞器。

经过多次尝试,我能够想出一个记录请求详细信息的函数,我将supertest的响应与预期的状态代码一起传递。失败时,该功能将记录请求和响应的详细信息,如路径parm、查询parm、请求主体

import SuperTest from "supertest";
export const checkStatusCode = (res: any, expectedStatus: any = 200): SuperTest.Response => {
if (res.status === expectedStatus) {
return res
};
const error = res.error;
const reqData = JSON.parse(JSON.stringify(res)).req;
throw new Error(` 
request-method  : ${JSON.stringify(reqData.method)} 
request-url     : ${JSON.stringify(reqData.url)}
request-data    : ${JSON.stringify(reqData.data)}
request-headers : ${JSON.stringify(reqData.headers)}
reponse-status  : ${JSON.stringify(res.status)}
reponse-body    : ${JSON.stringify(res.body)}
`
);
};

在笑话测试文件中的使用


describe("Jest - Api - user", () => {
it("Verify POST ", async () => {
const res = await request(url.someurl)
.post("/dummy")
.set("Authorization", authToken)
.send(updateThirdParty)
checkStatusCode(res, 200)
})
})

该解决方案的灵感来源于超级测试github问题中的一个建议。感谢萨科维亚。注意:这是记录数据的函数,我们仍然可以将其作为预期本身的包装器,我将其作为单独的线程发布。

相关内容

  • 没有找到相关文章

最新更新