【问题标题】:Supertest/mocha Error: global leak detected: pathSupertest/mocha 错误:检测到全局泄漏:路径
【发布时间】:2012-08-19 01:58:02
【问题描述】:

所以我正在尝试为我的 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 );
    });

});

【问题讨论】:

    标签: node.js express mongoose mocha.js


    【解决方案1】:

    发生的情况是,在您的代码中某处您缺少 var 声明。 Mocha 足够聪明,可以在您的整个项目中检测到这一点,而不仅仅是您的测试文件。

    你可能正在这样做:

    path = require('path');
    

    而不是

    var path = require('path');
    

    或者甚至……

    var fs = require('fs')     //<--- notice the missing comma
        path = require('path');
    

    当您不声明变量时,它们会附加到全局范围。在 Node.js 中为global,在浏览器中为window

    【讨论】:

    • 是的!这确实是问题所在,虽然不是那么明显。我有一个像这样的for .. in 循环:for (path in object) ...,问题是我在循环之前没有声明var path。谢谢!
    猜你喜欢
    • 2012-01-11
    • 2017-11-21
    • 1970-01-01
    • 2015-08-10
    • 2021-11-30
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多