How do I send a request to KOA app without starting the server
I want to avoid app.listen(port)
I am looking for something like
yield koaApp.sendRequest("POST", "/login/user", {
username : "svsf",
passwd : "XXXX"
}, {
someHeader: "header Value"
})
Why would you do that? What's the use case?
I basically have requirement for functional test in my project which hits database but it should not hit any third party services such as SMS and Email so i need to mock them and assert their call in Jasmine test suite.
if i start server using app.listen(port) it blocks and jasmine is not able to execute test cases ?
I am also using koa-router lib for my project
does that makes sense ? let me know if you need more input
Can't you test the function without router and koa? Because router is calling some function and and I am pretty sure koa-router itself is working fine, so it's ok if you don't test that part of the flow :)
That's not the requirement. I am using interceptors.
Anyway thanks for your response.
Personally I just spin up http servers via INADDR_ANY (listen(0)) and test against that, typically quick enough with Node for tests, and that way you don't have any false positives.
Koa doesn't support what you're looking for out of the box though! I'm not aware of any third-party mods that provide it.
Yeah listen(0) seems to be how a lot of testing frameworks do it under the hood. Not sure there's any downside to it.
if i start server using app.listen(port) it blocks and jasmine is not able to execute test cases ?
app.listen certainly does not block, I think the problem is elsewhere.
To answer your original question though, I think this might work:
app.callback()(req, res);
(you'll probably want to store app.callback() somewhere after you've added in the middleware, calling that calls compose on the middleware)
Does the code access the third-party services via an http API? If so you
could use https://www.npmjs.com/package/nock to intercept those external
calls. As for avoiding app.listen within the unit tests, you could use
https://www.npmjs.com/package/supertest-as-promised (a promisified version
of https://github.com/visionmedia/supertest ), which automagically fires up
an http.Server on an ephemeral port. Instead of passing in "app" like in
the examples, you'll want to pass in app.callback() if your app is a Koa
app.
app.callback() works like a charm. Many thanks.
I think this issue can be closed then?
For anyone interested in doing this, it is possible:
import createContext from "koa-create-context";
import compose from "koa-compose";
function handleRequest(ctx, fnMiddleware) {
const res = ctx.res;
res.statusCode = 404;
const onerror = err => ctx.onerror(err);
const handleResponse = () => {
return ctx
};
return fnMiddleware(ctx).then(handleResponse).catch(onerror);
}
var middleware = [func1, func2, func3, ...];
var chain = compose(middleware);
let resultCtx = await handleRequest(ctx,chain);
//Make assertions about response, like
assert( resultCtx.response.body.success == true, "Failed");
And so forth
Most helpful comment
Yeah
listen(0)seems to be how a lot of testing frameworks do it under the hood. Not sure there's any downside to it.app.listencertainly does not block, I think the problem is elsewhere.To answer your original question though, I think this might work:
app.callback()(req, res);(you'll probably want to store
app.callback()somewhere after you've added in the middleware, calling that calls compose on the middleware)