【发布时间】:2020-02-21 10:17:19
【问题描述】:
我不是开发人员,而是 QA 人员,并且是 Node.js 的新手。我需要有关 mocha 测试用例的帮助。
我有一个带有 Mocha 5.0.0 测试用例的 Node.js 8 微服务应用程序,该应用程序在本地运行良好,但在公司 Jenkins 服务器上因超时错误而失败。
代码如下:
test.js:
var assert = require('assert');
var request = require('request');
describe("My Unit Test",function(done){
this.timeout(30000);
// #1 Test to check if the REST app is running
it("should find the service to be running",function(){
request('http://my_server:myport', function (error, response, body) {
assert.equal(response.statusCode, 200, 'REST URL is up');
done();
});
...
});
});
本地输出:
> mocha test.js
My Unit Test
√ should find the service to be running (704ms)
1 passing
Jenkins 输出:
[2020-02-21 09:35:28] > mocha test.js --timeout 30000
[2020-02-21 10:22:54] 1) should find the service to be running
[2020-02-21 10:22:54] 0 passing (30s)
[2020-02-21 10:22:54] 1 failing
[2020-02-21 10:22:54] 1) My Unit Test
[2020-02-21 10:22:54] should find the service to be running:
[2020-02-21 10:22:54] Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/data/cibuild/.../workspace/test.js)
[2020-02-21 10:22:54]
[2020-02-21 10:23:12] npm ERR! Test failed. See above for more details.
[2020-02-21 10:23:12] Error: Command exited with status 1
我已将超时设置为 3000 到 30000,但我总是收到超时错误。
这是我的 package.json:
"scripts": {
"start": "node main.js",
"test": "mocha test.js --timeout 30000"
我知道 request() 现在已经过时了,而 got() 是首选,但是我不能对 test.js 文件进行太多更改,尽管我可以在某种程度上对其进行调整。你能帮我解决超时问题吗?
【问题讨论】:
-
很可能在您的本地环境中您可以访问
my_server:myport,但 Jekins 不能。不应该:单元测试应该与世界隔离。所以 1/ 这个地址实际上是正在测试的代码库,开发人员应该使用 supertest 或类似的方法,使用 application object 本身进行测试,或者 2/ 单元测试正在尝试处理集成测试而且它永远不会在 Jenkins 上工作。 -
@StockOverflaw 基于开场白“我不是开发人员,而是 QA 人员”,我猜这不是单元测试,也不是集成测试,而是 API 测试。所以这件事可以让 Jenkins 自动化。
-
Jenkins 服务器在公共 Internet 上可用。
标签: node.js unit-testing timeout mocha.js