javascript - Simple Unit Testing NodeJS/Express -
hi pretty new nodejs , trying write first tests.
i kind of stuck setup, hoping help.
i wrote these 2 functions:
app.js: var express = require('express') , cors = require('cors') , app = express(); app.get('/a_nice_jsonp',cors(corsoptions), function(req, res, next){ var result = parsecookies(req); res.jsonp(result); }); app.get('',function(req,res,next){ res.statuscode = 200; res.end() });
i not export module file.
i assume it's pretty easy write tests that. started this:
app-test.js: var expect = require('expect.js'); var express = require('express'); var expressapp = express(); describe('app js test', function() { describe('get /', function() { it('should respond empty path', function () { expressapp.get('', function(req, res, body){ expect(res.status).to.equal(200); }); }) }); });
i suppose reads simple task, seem fail on setup of test , how it.
can me out here?
edit: above test runs fine. however, have difficulties test e.g. .end()
result
in jsonp request
. simple not know how it?!
when do
expressapp.get('', function(req, res, body){ expect(res.status).to.equal(200); });
you mapping route.
to test rest api, have use library supertest (there example of testing using express + mocha in link)
it works way
var request = require('supertest'); var express = require('express'); var app = express(); app.get('/a_nice_jsonp',cors(corsoptions), function(req, res, next){ var result = parsecookies(req); res.jsonp(result); }); app.get('',function(req,res,next){ res.statuscode = 200; res.end() }); describe('app js test', function() { describe('get /', function() { it('should respond empty path', function (done) { request(app) .get('') .expect(200) .end(done) }); }); });
edited separated files
app.js
var express = require('express') , cors = require('cors') , app = express(); app.get('/a_nice_jsonp',cors(corsoptions), function(req, res, next){ var result = parsecookies(req); res.jsonp(result); }); app.get('',function(req,res,next){ res.statuscode = 200; res.end() }); module.exports = app;
app-test.js
var request = require('supertest'); var app = require('app.js'); describe('app js test', function() { describe('get /', function() { it('should respond empty path', function (done) { request(app) .get('') .expect(200) .end(done) }); }); });
Comments
Post a Comment