Supertest/mocha错误:检测到全局泄漏:路径

rag*_*lka 0 mocha.js mongoose node.js express

所以我正在尝试为我的REST API编写测试(构建在Express和Mongoose之上),但我遇到了一些麻烦.

我已经关注了很多示例和教程,这些示例和教程表明我的解决方案应该可行,但事实并非如此 - 我得到了一个 Error: global leak detected: path

似乎导致它的线是.post( '/api/invoices' )- 但我无法弄清楚为什么.

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 );
    });

});
Run Code Online (Sandbox Code Playgroud)

JP *_*son 5

发生的事情是你的代码中某处缺少你的var声明.Mocha非常聪明,可以在整个项目中检测到这一点,而不仅仅是测试文件.

就像在,你可能会这样做:

path = require('path');
Run Code Online (Sandbox Code Playgroud)

代替

var path = require('path');
Run Code Online (Sandbox Code Playgroud)

或者甚至可能......

var fs = require('fs')     //<--- notice the missing comma
    path = require('path');
Run Code Online (Sandbox Code Playgroud)

如果不声明变量,它们将附加到全局范围.在Node.js中,那就是global在浏览器中window.