New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supporting promises #79
Comments
Not that I'm aware of, but I also know nothing about promises so have no idea what would it take to support it. |
👍 for this being added. mocha supports them and that's a huge reason as to why I use it |
Why not keep using mocha? We're not trying to move anyone away from it. |
@hueniverse Since that's the case, Why would someone use this over something like mocha? |
Lab is really an internal solution. It's not advocated that anyone use it over mocha. Correct me if I'm wrong. |
lab works better than mocha with domains (which hapi uses). It has all the features we need for developing hapi. Beyond that, we're not trying to increase adoption. People who use it and want new features usually add them themselves and we accept them as long as they keep things simple. |
@geek Would you be open to revisit this ? |
@Marsup sure, how do you imagine promises being used within lab? |
What about returning a promise and let lab handle the then/catch to trigger the done ? |
That works, any chance we can make it a dev dependency? |
Sure, since the promise is provided by the test you don't need a hard dependency on it. |
I am not sure if this made it into some release or not. If it did, would appreciate pointing me to a test case / doc that shows how it works. I have been yo-yo ing between mocha and lab because of domains/exceptions and promises. thx |
I was having a bit of trouble figuring out how to get lab to work with promises. When an assertion fails within a promise's then block, the error is unhandled and the test times out rather than reporting actual and expected values like normal. The key to getting things working is to be sure to have a catch block at the end that passes the error to lab's done function - failed assertions will be reported as normal. Simple example below: var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
import Promise from 'bluebird';
lab.experiment('math', function() {
lab.test('failed assertion within a promise', function (done) {
new Promise(function(resolve, reject) {
return resolve(2+2);
})
.then(function(four) {
Code.expect(four).to.equal(3);
done();
})
.catch(function(error) {
// this makes sure any assertion errors are handled and reported by lab
done(error);
});
});
}); Now rather than getting unhandled errors printed to the console and your test timing out, you'll get a nice failed test report like normal. This kind of solution may be obvious, but in case anyone else is struggling with promises in lab I thought I'd post this! |
@Traviskn, great comment, I also was struggling with testing promises. An example would be a great addition to the README. A test even more. |
There are some situation where @Traviskn example doesn't work. When you have an assert in lab.test('expect an error from a promise', (done) => {
return new Promise((resolve, reject) => {
try {
resolve(2);
}
catch (err) {
reject(err);
}
}).then((result) => {
console.log('resolved');
done(new Error('promise should be rejected and caught'));
}).catch((error) => {
console.log('rejected, this does not trigger');
Code.expect(error).to.exist();
done(error);
});
}); complete example 'use strict';
const Code = require('code');
const Lab = require('lab');
const lab = exports.lab = Lab.script();
lab.experiment('promises resolving rejects', () => {
// @Traviskn example, works as expected when resolved
lab.test('failed assertion within a promise, resolve', (done) => {
return new Promise((resolve, reject) => {
try {
resolve(2);
}
catch (err) {
reject(err);
}
}).then((result) => {
console.log('1) resolved');
Code.expect(result).to.equal(3);
done();
}).catch((error) => {
console.log('1) rejected');
done(error);
});
});
// @Traviskn example, also fails on rejected but not on the assert
lab.test('failed assertion within a promise, reject', (done) => {
return new Promise((resolve, reject) => {
try {
throw new Error('fail in promise');
resolve(2);
}
catch (err) {
reject(err);
}
}).then((result) => {
console.log('2) resolved, this does not trigger');
Code.expect(result).to.equal(3);
done();
}).catch((error) => {
console.log('2) rejected');
done(error);
});
});
// another example, works as expected
lab.test('expect an error from a promise, reject', (done) => {
return new Promise((resolve, reject) => {
try {
throw new Error('fail in promise');
resolve(2);
}
catch (err) {
reject(err);
}
}).then((result) => {
console.log('3) resolved, this does not trigger');
done();
}).catch((error) => {
console.log('3) rejected');
Code.expect(error).to.exist();
done();
});
});
// another example, doesn't fail when error doesn't exist
lab.test('expect an error from a promise, resolved', (done) => {
return new Promise((resolve, reject) => {
try {
resolve(2);
}
catch (err) {
reject(err);
}
}).then((result) => {
console.log('4) resolved');
done();
}).catch((error) => {
console.log('4) rejected, this does not trigger');
Code.expect(error).to.exist();
done(error);
});
});
// best practice example, it does fail even when error doesn't exist
lab.test('expect an error from a promise', (done) => {
return new Promise((resolve, reject) => {
try {
resolve(2);
}
catch (err) {
reject(err);
}
}).then((result) => {
console.log('5) resolved');
done(new Error('promise should be rejected and caught'));
}).catch((error) => {
console.log('5) rejected, this does not trigger');
Code.expect(error).to.exist();
done(error);
});
});
}); |
@geek can I take a shot at it ? |
@robkorv thanks for the very complete examples! definitely worth documenting somewhere |
@Traviskn your welcome. |
@robkorv Nope, my main goal with that feature is to use async/await, eliminating the need for such a library. |
@Marsup, didn't know about async/await. Looks promising 😄. |
@Marsup sounds good to me, thank you. |
I too might be interested in using Lab with async/await. Has there been any progress on this lately? Also, if this is still happening, could this issue be reopened as a feature request to more accurately reflect the status of this issue? |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Does Lab support promise testing similar to Mocha?
The text was updated successfully, but these errors were encountered: