node.js - How to Make a Call to Koa.js App Instance for Unit Tests -


i don't know how i'd term maybe 'static call koa router'? seem right wordage here i'm trying accomplish if talk technically?

anyway, i'm using koa-router , i'm coding unit tests (not integration tests). not want invoke .listen() on koa app because of reason...it create http server makes test integration tests.

instead in test want make straight call app object instance , call route , able return no results , check returned no results in response.

how can that? can't find example , i've tried sorts of pseudo code attemps agains koa app object.

if want test function koa-router routes perform unit test on function , leave routing out of it.

to me sounds you've got file such app.js , contains code. can create router.js file put route bindings , services.js file can put application logic.

so example app.js might like:

var koa = require("koa"); var app = module.exports = koa(); var router = require('./router.js');  app.use(router.unsecured.middleware());  app.listen(3000); 

and router.js might like:

var router = require("koa-router"); var service = require("./services.js");  var unsecured = module.exports.unsecured = new router();  unsecured.post('/account/signin', service.signinuser); unsecured.post('/account/register', service.registeruser); 

and services.js might like:

module.exports.signinuser  = function*(signindetails) {   // contains application signin logic };  module.exports.registeruser  = function*(registerdetails) {   // contains application register logic }; 

so in manner can individually test services.js. don't see value in individually testing router.js since trivial. @dan pantry shows can test routing part of integration test using supertest.

edit:

so little experimental test playing around test routing correct. i'm using mocha test runner , code example posted in original code.

// standard library var assert = require("assert");  // in app objects var router = require('./router.js'); var service = require('./service.js');  describe("routing tests", function() {    it("test register routing, post", function*(done) {     // arrange     var unsecured = router.unsecured;     var path = '/account/register';     var httpmethod = 'post';     var expected = service.register.tostring();     var actual;      // act     (var = 0; < unsecured.stack.length; i++)     {       var pathmatch = unsecured.stack[i].path === path;       var methodmatch = unsecured.stack[i].methods.indexof(httpmethod) >= 0;        if (pathmatch && methodmatch)       {         actual = unsecured.stack[i].middleware.tostring();         break;       }     }      // assert     try {       assert.equal(expected, actual);       done();     } catch(err) {       done(err);     }   });     }); 

there neater way of doing (and more modular way testing multiple paths) said basic example verify routing calling correct service. i'm doing delving koa-router object verify path bound service code depending on http method (e.g. post, get, etc).

if have routing , services in modules test avoids dealing main koa app. although technically test spans multiple units (the routing , service code) technically integration test mean don't go near app.listen() didn't want call in tests.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -