Node.js 为什么我的Mocha测试(异步/等待)通过了,但同时抛出了一个错误?

Node.js 为什么我的Mocha测试(异步/等待)通过了,但同时抛出了一个错误?,node.js,async-await,mocha.js,Node.js,Async Await,Mocha.js,我是Mocha的新手,我一直在努力测试一个使用web服务生成的令牌的函数。测试通过了,但最后抛出了一个错误。问题似乎与异步/等待函数有关 如果注释掉.end函数,则测试通过。 调试我可以看到res.status是500,这意味着等待不起作用 我的测试文件代码如下: var supertest = require("supertest"); var should = require("should"); const getToken = require("./getToken"); var se

我是Mocha的新手,我一直在努力测试一个使用web服务生成的令牌的函数。测试通过了,但最后抛出了一个错误。问题似乎与异步/等待函数有关

如果注释掉.end函数,则测试通过。 调试我可以看到res.status是500,这意味着等待不起作用

我的测试文件代码如下:

var supertest = require("supertest");
var should = require("should");
const getToken = require("./getToken");

var server = supertest.agent("http://localhost:3000");

// Testing alarmStatusController
//  

describe("Get Alarm Status", () => {

    it("should return a json file and 200 if token valid", async () => {
        let token = await getToken.getValidToken();
        server
            .get("/api/node/path")
            .set('Authorization', 'Bearer ' + token)
            .expect("Content-type", /json/)
            .expect(200)
            .end(function (err, res) {

                res.status.should.equal(200);
                res.body.message.should.equal('Alarms fetched successfully');

            });
    });

});
试验结果如下:

Get Alarm Status
     √ should return a json file and 200 if token valid (38ms)

    1 passing (38ms)


C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\should\cjs\should.js:254
  throw new AssertionError(params);
  ^
AssertionError: expected 500 to be 200
at Assertion.fail (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\should\cjs\should.js:275:17)
at Assertion.value (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\should\cjs\should.js:356:19)
at Test.<anonymous> (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\test\alarmStatus_test.js:29:39)
at Test.assert (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\supertest\lib\test.js:181:6)
at localAssert (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\supertest\lib\test.js:131:12)
at C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\supertest\lib\test.js:128:5
at Test.Request.callback (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\superagent\lib\node\index.js:728:3)
at parser (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\superagent\lib\node\index.js:916:18)
at IncomingMessage.res.on (C:\Dev\Globalwatch3\WebServices\GlobalwatchApi\node_modules\superagent\lib\node\parsers\json.js:19:7)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
希望有人能帮忙。。。
谢谢

您可以使用承诺而不是结束回调:

const supertest = require("supertest");
const should = require("should");
const getToken = require("./getToken");
const server = supertest.agent("http://localhost:3000");

describe("Get Alarm Status", () => {

    it("should return a json file and 200 if token valid", async () => {
        let token = await getToken.getValidToken();

        const res = await server
            .get("/api/node/path")
            .set('Authorization', 'Bearer ' + token)
            .expect("Content-type", /json/)
            .expect(200);

        res.status.should.equal(200);
        res.body.message.should.equal('Alarms fetched successfully');
    });

});
server.end是带有回调的异步函数。因此,您的测试用例在调用server.end之后结束,而不是等待它完成。您需要在测试中使用done。