I'm trying to run the test of a promise, but the test fails arguing it exceeded timeout limit, and suggests to make sure I have the done clauses.
This is part of my test code:
$configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model
.then(function () {
done(new Error("Expected INVALID_MODEL error but got OK"));
}, function (error) {
chai.assert.isNotNull(error);
chai.expect(error.message).to.be.eq("INVALID_MODEL_ERROR");
chai.expect(error.kind).to.be.eq("ERROR_KIND");
chai.expect(error.path).to.be.eq("ERROR_PATH");
done();
})
.catch(done);
});
I have all the done clauses in there as you can see, so I don't know if I'm missing something in the test or the structure is just wrong.
Mocha supports testing promises without using done
as long as you return
the promise.
const expect = chai.expect
it('should error', function(){
return $configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model
.then(()=> { throw new Error("Expected INVALID_MODEL error but got OK")})
.catch(error => {
expect(error).to.not.be.null;
expect(error.message).to.equal("INVALID_MODEL_ERROR");
expect(error.kind).to.equal("ERROR_KIND");
expect(error.path).to.equal("ERROR_PATH");
})
})
Also look at chai-as-promised
to make promise testing more like standard chai assertions/expectations.
chai.should()
chai.use(require('chai-as-promised'))
it('should error', function(){
return $configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL)
.should.be.rejectedWith(/INVALID_MODEL_ERROR/)
})
On Node 7.6+ environments or where you have babel / babel-register you can also make use of the async
/ await
promise handlers
it('should error', async function(){
try {
await $configurations.updateConfiguration(configurations_driver.NOT_VALID_MODEL)
throw new Error("Expected INVALID_MODEL error but got OK")})
} catch (error) {
expect(error).to.not.be.null;
expect(error.message).to.equal("INVALID_MODEL_ERROR");
expect(error.kind).to.equal("ERROR_KIND");
expect(error.path).to.equal("ERROR_PATH");
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.