So I'm trying to write tests for my REST API (built on top of Express and Mongoose), but I have run into some trouble.
I have followed a lot of examples and tutorials, which suggest that my solution below should work, but it isn't - I am getting a Error: global leak detected: path
It seems that the line that is causing it is .post( '/api/invoices' )
- but I cannot figure out why.
var app = require("../app").app,
request = require("supertest");
describe("Invoice API", function() {
it( "GET /api/invoices should return 200", function (done) {
request(app)
.get( '/api/invoices' )
.expect( 200, done );
});
it( "GET /api/invoices/_wrong_id should return 500", function (done) {
request(app)
.get( '/api/invoices/_wrong_id' )
.expect( 500, done );
});
it( "POST /api/invoices should return 200", function (done) {
request(app)
.post( '/api/invoices' )
.set( 'Content-Type', 'application/json' )
.send( { number: "200" } )
.expect( 200, done );
});
});
What's happening is that somewhere in your code you're missing your var
declaration. Mocha is smart enough to detect this in your entire project, not just your test files.
As in, you're probably doing this:
path = require('path');
instead of
var path = require('path');
Or maybe even...
var fs = require('fs') //<--- notice the missing comma
path = require('path');
When you don't declare your variables they get attached to the global scope. In Node.js, that's global
and in the browser that's window
.