testingexpress API需要本地服务器

我读过我可以在一个快速应用程序(nodeJS)与超级testing运行摩卡testing,因此它不需要在不同的terminal会话中运行应用程序。 无论我尝试它总是以连接错误结束。

为了configuration我们的持续集成,显然集成和unit testing(mocha,supertest,should)应该能够运行,而节点服务器也不运行

书面testing是validation我们的应用程序的内部API端点谁可以解释如何运行testing,而无需运行应用程序的快递服务器,以便他们可以与例如strider

您需要拆分调用app.listen生产代码,并确保在mochatesting运行期间不会执行。 我把所有的路由和设置代码放在app/index.js ,然后有一个单独的文件app/server.js ,它只有app/server.js代码来开始监听,连接到数据库等等。但是我的大部分应用程序详细信息在app/index.js中configuration,所以我可以用supertesttesting它们。

 //index.js var app = require("express")(); app.get("/", function (req, res) { res.type("text"); res.send("Welcome home"); }); module.exports = app; 

 //server.js #!/usr/bin/env node var app = require("./index"); app.listen(3000); 

 //index.mocha.js var app = require("./index"); var request = require("supertest")(app); var expect = require("expectacle"); describe("the home page", function () { it("should welome me in plain text", function(done) { request.get("/") .expect(200) .expect("Content-Type", "text/plain; charset=utf-8") .end(function (error, result) { expect(error).toBeFalsy(); expect(result.text).toBe("Welcome home"); done(); }); }); });