Supertest
Node.js library ที่ Provide High-level Fluent API ให้เราสามารถทำ API Testing ได้อย่างง่ายๆ มี syntax ในการเขียนเทสที่ทำให้เราอ่านเข้าใจได้ง่าย และยังสามารถทำงานร่วมกันได้ดีกับ Node.js Server Side App.
Jest + Supertest ทำงานร่วมแล้วจะเป็นไง?
install
npm i --dev-save supertest @types/supertest#client.ts
import supertest from 'supertest';
import { addMsg } from 'jest-html-reporters/helper';
import { toCurl } from 'request-to-curl';
const request = supertest.agent('https://reqres.in/api');
request.on('response', async (response) => {
const { request, body, statusCode, headers } = response;
const data = {
request: {
header: request._header,
url: request.url,
body: request._data,
method: request.method,
curl: request.req.toCurl()
},
response: {
header: headers,
status: statusCode,
body,
}
}
await addMsg(JSON.stringify(data, null, 2));
});
export default request;Last updated