const chaiAsPromised = require('chai-as-promised');
const chai = require('chai');
const expect = chai.expect;
chai.use(chaiAsPromised);
describe('[sample unit]', function() {
it('should pass functionToTest with true input', function() {
expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
});
});
This test passes??? I am using "chai": "3.5.0", "chai-as-promised": "5.2.0",
expect(...)
returns a promise itself, which will either be resolved or rejected, depending on the test.
For Mocha to test the outcome of that promise, you need to explicitly return it from the test case (this works because Mocha has built-in promise support):
describe('[sample unit]', function() {
it('should pass functionToTest with true input', function() {
return expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
});
});
Alternatively, you can use Mocha's "regular" callback-style async setup and chai-as-promised's .notify()
:
describe('[sample unit]', function() {
it('should pass functionToTest with true input', function(done) {
expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh").notify(done);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With