0

Recently I got introduced to node.js and packages like express, mongodb and ejs. I have few questions :

As a learning purpose, I have created a github repo of user management which uses express, mongodb and ejs. I have all my functions in routes/users.js file. I need to write test cases all these functions. How to create a test driven programming with this example?

In my routes in app.js file.

app.get('/login', user.login);
app.post('/login', user.loginSubmit);

I need to write different routes to login page renders and submit etc. If there are some ajax request also, then we have lots of routes in app.js file when considering to routes of a single page. Is it like that or need to change my structure?

Justin John
  • 9,223
  • 14
  • 70
  • 129
  • isnt express bundled with tests ? you could just look at the repo and see if there are some test utilities you can use or recycle. – mpm Mar 21 '13 at 06:48
  • @mpm How to run tests in express? In express test some functions `describe`, `test` etc are there. From where we can find docs and usage of these functions? – Justin John Mar 21 '13 at 06:58
  • look for either mocha or jasmine but i guess express creator used mocha ,since he created it too. – mpm Mar 21 '13 at 07:13
  • may be you try [vows](http://vowsjs.org/) – Mithun Satheesh Mar 21 '13 at 08:46

1 Answers1

1

I recommend you Mocha, it's from the same guy of expressjs. It supports test coverage for you code, hooks before, after, each and of course it supports async code.

I use it in combination with should.js or even chai.js

A test in mocha looks like, the code is from my own test where I'm using superagent, in order to make requests.

it('requests a permission with valid ticket', function (done){
        agent
            .post(route.server + '/preq')
            .set('Content-Type', 'application/json')
            .set('Authorization', 'Bearer ' + ACCESSTOKEN)
            .send({ticket: TICKET})
            .end(function (req,res) {

                res.should.have.property('statusCode').that.equals(201);
                var location = .....
                res.headers.should.have.property('location').that.is.equal(location);
                done();
            });
    })
Maldo
  • 106
  • 2
  • 8
  • Can you please show an example of mocha integration with my [registerUser](https://github.com/justin-john/node-user-management/blob/master/routes/users.js#L107-L146) function? – Justin John Mar 21 '13 at 09:27
  • I have install `mocha` by `npm install mocha` and created a file `test/test.js`. I haved copied the all code from gist to `test.js` and tried with command `make test` in root, but receives a message in shell as `make: Nothing to be done for \`test'.` and when entering same command with `test` folder as root gives another message `make: *** No rule to make target \`test'. Stop.` How could we run the test file? – Justin John Mar 21 '13 at 11:37
  • you could execute from a Makefile or from npm scripts variable, but the easiest way is `mocha test/ -R spec` – Maldo Mar 21 '13 at 11:45
  • I have changed **post(route.signup)** in gist example to **post(route.registerUser)** . The **var route = require('../routes/users')** is already defined. Every time I am getting an error as `⤠â 1 of 3 tests failed: 1) testing user "before all" hook: Error: timeout of 2000ms exceeded at null. (/home/justin/www/NodeUserManagement/node_modules/mocha/lib/runnable.js:167:14) at Timer.list.ontimeout (timers.js:100:19) at process._makeCallback (node.js:248:20) make: *** [test] Error 1` How to correct this error? – Justin John Apr 04 '13 at 07:54