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?

4
  • 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. Commented Mar 21, 2013 at 6: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? Commented Mar 21, 2013 at 6:58
  • look for either mocha or jasmine but i guess express creator used mocha ,since he created it too. Commented Mar 21, 2013 at 7:13
  • may be you try vows Commented Mar 21, 2013 at 8:46

1 Answer 1

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();
            });
    })
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please show an example of mocha integration with my registerUser function?
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?
you could execute from a Makefile or from npm scripts variable, but the easiest way is mocha test/ -R spec
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.<anonymous> (/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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.