javascript - Closing server after mocha test, but data is still there -
i'm trying test data through rest api. data stored array in project.
i want data "restored" every time test suites runs, know amount of data in array.
but when im testing post. element inserted there in next test suite, though start , stop server before each test.
app.js requires user array, stopping , starting server, thought clean before every test, doesn't
thansk .
global.test = true; // test server setup var app = require('../app'); var testport = 9999; var testserver; // dependencies var should = require('should'); var request = require('request'); var baseurl = 'http://localhost:9999/api'; describe('restapi : methods', function () { //start server before tests beforeeach(function (done) { testserver = app.listen(testport, function () { console.log("server listening on: " + testport); done(); }) .on('error', function (err) { console.log(err); }); }); aftereach(function () { //stop server after test testserver.close(function () { console.log("closing server"); }); }); test here }); describe('restapi : post methods', function () { //start server before tests before(function (done) { testserver = app.listen(testport, function () { console.log("server listening on: " + testport); done(); }) .on('error', function (err) { console.log(err); }); }); after(function () { //stop server after test testserver.close(function () { console.log("closing server"); }); }); test here });
------ app.js ----- know rest api should in own folder. :)
var path = require('path'); var bodyparser = require('body-parser'); //for testing delete cached user array delete require.cache[require.resolve('./users')]; var users = require('./users'); var express = require('express'); function finduser(users, email) { var result = users.filter(function(u) { return u.email === email; }); if(result.length >= 1) { return result[0]; } return undefined; } var app = express(); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); app.get("/api/users", function(req, res) { res.send(users); }); app.get("/api/users/:email", function(req, res) { var user = finduser(users, req.params.email); if(!user) { return res.status(404).send({error: "user not found"}); } res.send(user); }); app.post("/api/users", function(req, res) { var user = req.body; if(finduser(users, user.email)) { return res.status(400).send({error: "user exist"}); } users.push(user); res.send(user); }); app.put("/api/users/:email", function(req, res) { var user = req.body; if(!finduser(users, req.params.email)) { return res.status(404).send({error: "user not found"}); } if(!user) { return res.status(404).send({error: "no user provided"}); } for(var = 0; < users.length; += 1) { if(users[i].email === user.email) { users[i].password = user.password; break; } } res.send(user); }); app.delete("/api/users/:email", function(req, res) { var user = finduser(users, req.params.email); if(!user) { return res.status(404).send({error: "user not found"}); } for(var = 0; < users.length; i++) { if(users[i].email === req.params.email) { users.splice(i, 1); break; } } res.send(user); }); app.all('*', function(req, res) { res.status(500).send(req.url + " not valid"); }); if(global.activetest === undefined){ app.listen(3000, function() { console.log("rest api started, listen on port 3000"); }); } module.exports = app;
my solution (as mentioned in comment above) refactor code initializes users
var separate function, can called before each test.
var path = require('path'); var bodyparser = require('body-parser'); var express = require('express'); var clone = require('clone'); // <-- add `clone` dependency var users; function initusers() { users = clone(require('./users')); } // ... rest if code, initializing express/adding route handlers app.listen(3000, function() { console.log("rest api started, listen on port 3000"); }); module.exports = exports = app; exports.initusers = initusers;
using sample data in users.js
:
module.exports = [ { email: 'email0', password: 'password0' }, { email: 'email1', password: 'password1' }, { email: 'email2', password: 'password2' }, { email: 'email3', password: 'password3' }, { email: 'email4', password: 'password4' }, { email: 'email5', password: 'password5' }, { email: 'email6', password: 'password6' }, { email: 'email7', password: 'password7' }, { email: 'email8', password: 'password8' }, { email: 'email9', password: 'password9' } ];
i able test calling initusers()
before each test, follows:
var app = require('../app'); var initusers = require('../app').initusers; var should = require('should'); var request = require('request'); var baseurl = 'http://localhost:3000/api'; describe('restapi', function() { describe('get methods', function () { beforeeach(function() { initusers(); }); it('should users initially', function(done) { request(baseurl + '/users', function(err, res) { var data = json.parse(res.body); data.length.should.equal(10); done(); }); }); it('should remove user', function(done) { request.del(baseurl + '/users/email1', function(err, res) { var data = json.parse(res.body); data.email.should.equal('email1'); data.password.should.equal('password1'); request(baseurl + '/users', function(err, res) { var data = json.parse(res.body); data.length.should.equal(9); done(); }); }); }); it('should users again', function(done) { request(baseurl + '/users', function(err, res) { var data = json.parse(res.body); data.length.should.equal(10); done(); }); }); }); });
as can see, wrote 3 tests.
- get users (verifying initial count 10)
- removing user (verifying count 9)
- get users (verifying count 10 again)
these are, no means, exhaustive tests, verify users
being initialized before every test.
Comments
Post a Comment