Error: done() invoked with non-Error: {"req":{ ......} request done without error, but test didn't pass

Error: done() invoked with non-Error: {"req":{ ......} request done without error, but test didn't pass

我正在尝试解决我的测试,但似乎没有任何效果。目前我正在使用 mocha、supertest 和 chai,但即使请求成功执行,我也会看到这个烦人的错误,我无法摆脱它:

done() invoked with non-Error: {“req”:{ …} request done without error, but test didn't pass

代码:

let should = require('chai').should(),
    expect = require('chai').expect,
    constants = require('./constants'),
    supertest = require('supertest'),
    api = supertest(constants.ENVIRONMENT),
    express = require('express'),
    http = require('http'),
    request = require('request'),
    app = express(),
    bodyParser = require('body-parser'),
    port = process.env.PORT || 3000,
    assert = require('assert');
    
    describe('Create device API call', () => {

    function createToken() {
        return new Promise(resolve =>
            request.post(
                'myURl',
                { 
                 json: 
                        { 
                          "username": "myUsername", 
                          "password": "myPassword" 
                        } 
                },
                function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                        let token = (body.accessList[0].accessToken);
                        resolve(token);

                    }
                    else {
                        console.log("Server error get token")
                    }

                })
        )
    }

    it('should create device', async (done) => {

        let token = await createToken();

        //console.log(`Token is ${token}`);

        api.post('/deviceManifests/1/devices')
            .set('Accept', 'application/json')
            .set('Authorization', `Bearer ${token}`)
            .send({
                "name": "QA integration",
                "deviceKey": "somekey38957",
                "defaultBinding": true
            })
            .expect('content-Type', /json/)
            .expect(201)
            .expect(function (res, err) {
                assert.equal(res.body.name, 'QA integration');

            }).then(done);
    });

});    

您正在将 done 传递给 then

因此,done 将传递一个参数。

但是,如果您将参数传递给 done,它会认为这是一个错误。

试试这个:

.then(function () {
  done()
})

...或使用箭头函数:

.then(() => done())