Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one unit test routes with Express?
    text
    copied!<p>I'm in the process of learning Node.js and have been playing around with <a href="http://expressjs.com" rel="noreferrer">Express</a>. Really like the framework;however, I'm having trouble figuring out how to write a unit/integration test for a route.</p> <p>Being able to unit test simple modules is easy and have been doing it with <a href="http://visionmedia.github.com/mocha/" rel="noreferrer">Mocha</a>; however, my unit tests with Express fail since the response object I'm passing in doesn't retain the values.</p> <p><em>Route-Function Under Test (routes/index.js):</em></p> <pre><code>exports.index = function(req, res){ res.render('index', { title: 'Express' }) }; </code></pre> <p><em>Unit Test Module:</em></p> <pre><code>var should = require("should") , routes = require("../routes"); var request = {}; var response = { viewName: "" , data : {} , render: function(view, viewData) { viewName = view; data = viewData; } }; describe("Routing", function(){ describe("Default Route", function(){ it("should provide the a title and the index view name", function(){ routes.index(request, response); response.viewName.should.equal("index"); }); }); }); </code></pre> <p>When I run this, it fails for "Error: global leaks detected: viewName, data".</p> <ol> <li><p>Where am I going wrong so that I can get this working?</p></li> <li><p>Is there a better way for me to unit test my code at this level?</p></li> </ol> <p><strong>Update</strong> 1. Corrected code snippet since I initially forgot "it()".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload