2

I'm new to Promises in JavaScript, and whilst it seems to be working for me to an extent, I'm unable to test the 'reject' value.

I'm passing through an Error, and want to ensure that it is an error and more importantly, that the error code matches what I'm expecting.

return new Promise(function(resolve, reject){
    tableService.deleteEntity(config.azureTable.tableName,
                              visitor.azureEntity(), function (error, response) {

        //  If successful, go on.
        if (!error) {
            resolve(response);
        }

        //  If unsuccessful, log error.
        else {
            /*  If we know it's a resourceNotFound
                that's causing the error, return that. */
            if (error.code === 'ResourceNotFound') {
                reject(new Error('Record not found'));
            }

            //  For unexpected errros.
            else {
                reject(new Error('Table service error (delete): ' + error));
            }
        }

    });

});

The test, in Mocha - using chai and chai-as-promised. Everything else is working (I have 24 passing tests) - but this one has me stuck!

it('return an error when the lookup fails', function (done) {

        storage.delete(globalUUID).then(function(sucess) {

            done(sucess);

        }, function(error) {

            expect(error).to.be.an(Error);
            done();

        });

    });

Any help would be greatly appreciated.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
Brody McKee
  • 203
  • 3
  • 5
  • 1
    And what happens? Why does the test fail? – David Losert Mar 09 '15 at 08:48
  • Sorry, vital information there! It simply times out. Every time. Yet, I can log out the error that's rejected and even go to done... When playing with the code, I managed to get it to tell me that 'error' was undefined a few times. – Brody McKee Mar 09 '15 at 09:31
  • 1
    Don't you need to `return` a promise to the test runner? – Bergi Mar 09 '15 at 10:26
  • @Bergi he's using the old `done` async syntax, but still it should be better to do use the new syntax. – Benjamin Gruenbaum Mar 09 '15 at 10:47
  • In the `new Promise(...)` code, `error` is an object. You need to `reject(new Error('Table service error (delete): ' + error.code))`. – Roamer-1888 Mar 09 '15 at 10:53
  • Could you show us your whole `delete()` method? – JLRishe Mar 11 '15 at 09:25
  • I've actually solved this (some hours ago) by moving to Bluebird for Promises. The storage.delete method was the first block of code. With a constructor for the Azure table entity. – Brody McKee Mar 12 '15 at 11:41
  • If anyone else faces an issue like this in future, promisify your callback functions and make you life (and testing) a log easier. Thanks everyone for your comments and helpful suggestions and for pointing me in the right direction. – Brody McKee Mar 12 '15 at 11:44

1 Answers1

0

You are not using chai-as-promised anywhere. If your first code example is the body of the storage.delete method, then your test should look like:

it('return an error when the lookup fails', function() {
    expect(storage.delete(globalUUID)).to.be.rejectedWith(Error);
});
sarneeh
  • 1,320
  • 1
  • 12
  • 27