| Name | Version |
| ---- | ------- |
| msw | 0.19.5 |
| node | 14.x |
| OS | ubuntu |
[email protected]
setupServer(
rest.post(process.env.ai_api_endpoint + '/datasync/job-profile/', (req, res, ctx) => {
/**
* here need to check actual response of ai api.
* but i think its just empty
*/
return res(ctx.status(200), ctx.json({}))
}),
)
await request(app)
.post("/user/login")
.set("Accept", "application/json")
.send({
"email": user.email,
"password": "123"
})
using msw with supertest causes empty post i.e data sent in "/user/login" is always empty.
if i comment out the msw server, post json is not empty anymore
same problem as this https://github.com/mswjs/msw/issues/209#issuecomment-654427646
Hey, @manishiitg. Thanks for putting this into a separate issue.
I'm confused: you handle a process.env.ai_api_endpoint + '/datasync/job-profile/' endpoint, but during supertest you call .post("/user/login"). Is there any internal logic you are not sharing (i.e. nested API call, redirects)? Without that info it looks like you're mocking request A, while performing request B. That, surely, won't result into a mocked response.
Hi @kettanaito
process.env.ai_api_endpoint point to https://ai.server its a totally separate url.
there is no connection between the two.
request B is an express app.
request B internally calls A which i am trying to mock.
problem is as soon as i have included mocking of request A, the data in POST gets empty i.e /user/login doesn't get email, password
I've found out an issue in node-request-interceptor where performing an original POST request with a body (issued by supertest does not get the request body included. It appears that supertest is not calling ClientRequest.prototype.write() to set the request's body. Investigating this.
The cause of this issue is the current implementation of ClientRequestOverride.end() method. It does not accept any arguments, while per NodeJS specification it should have the following overload function signatures:
function end(cb: () => void) {}
function end(chunk: string | Buffer, cb: () => void) {}
function end(chunk: string | Buffer, encoding: string, cb: () => void) {}
The current implementation assumes that q request's body will always be written via:
req.write(chunk)
req.end()
However, it's also possible (and this is what supertest is doing) to attach a body as the first argument to the .end() call:
req.end(chunk)
I was looking the same thing. This is the problem also for #209. The request intercepted from node-request-interceptor doesn't have a body.
I'm working on the implementation of ClientRequestOverride.end() method. Looks good. Expect a pull request soon.
Issued a pull request with a fix to node-request-interceptor library: https://github.com/mswjs/node-request-interceptor/pull/33. You can follow the progress there. I don't have time capacity to continue on this right now, will do so later next week.
The issue has been fixed in node-request-interceptor 0.3.2. The update on the msw side will follow and be released in 0.20.0.
i can confirm the issues is fixed. it works for me now. thanks @kettanaito