0

I'm trying to resolve my test but nothing seems to work. Currently I'm using mocha, supertest and chai, but even if request was performed successfully, I'm seeing this annoying error I can't get rid of:

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

Code:

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);
    });

});    
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Apart from changing the then to include the function or the arrow key func, add a catch block after .then for unresolved promise rejection. Don't assume that there are no errors without a catch for a promise reject scenario. – user2347763 Apr 09 '18 at 11:47
  • 1
    Thank you very much, in the meantime I solved it just like you said :) – Viktorija Filipov Apr 09 '18 at 18:00

1 Answers1

1

You are passing done into then.

Therefore, done will be passed an argument.

However, if you pass an argument to done, it expects it to be an error.

Try this instead:

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

...or using an arrow function:

.then(() => done())
Steve Holgado
  • 11,508
  • 3
  • 24
  • 32
  • > it expects it to be an error Yes, but what *is* an error? I keep gettin `done() invoked with non-Error` no matter what I pass to `done()`. – yPhil Feb 04 '21 at 11:41
  • @yPhil This `done` callback accepts an instance of `Error` or a falsy value. Docs here: https://mochajs.org/#asynchronous-code – Steve Holgado Feb 04 '21 at 12:03
  • Thanks! Now I just need to find out exactly what those two things are (Error instance (or subclass thereof) or a falsy value) notice that the docs don't provide any definition or link :( My app is throwing the error that gets passed on to `done()` so I need to know what an acceptable error *exactly* is ; like maybe a list. But Ok, I'll try `false`. – yPhil Feb 04 '21 at 13:14
  • @yPhil Please see docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error and https://developer.mozilla.org/en-US/docs/Glossary/Falsy – Steve Holgado Feb 04 '21 at 13:17
  • @yPhil No problem :) – Steve Holgado Feb 04 '21 at 13:22