promise support
'use strict';
// Load modules
const Code = require('code');
const Lab = require('lab');
// Test shortcuts
const lab = exports.lab = Lab.script();
const it = lab.test;
const expect = Code.expect;
const getAPromise = () => {
return new Promise((resolve, reject) => {
resolve(false);
});
};
it('test lab promise support', (done) => {
getAPromise()
.then((value) => {
expect(value).to.be.true();
done();
});
});
returns
Timed out (2000ms) - test lab promise support
expected
Expected false to be true
You're not handling the promise's catch, so yes normal behavior.
I think you need to return the promise to see the proper assertion error message.
it('test lab promise support', (done) => {
return getAPromise().then((value) => {
expect(value).to.be.true();
done();
});
});
Related: https://github.com/hapijs/lab/issues/553
@mtharrison unfortunately it's not working with the return promise
@Marsup
I'm not handling it by choice, because the promise resolves ok , it's not rejecting
It's the expect part that's throwing an error and it gets caught in the Promise mix
This is a simple example that can be fixed easily , but more complex tests require a lot of .catch() routines to get all the errors passed on to lab
I don't see this as a bug, but as an enhancement needed by lab
An assertion will always get caught by promises, there's no way around that, so returning the promise is the only way to deal with that, you shouldn't even have a done there.
It works for me with your exact code listing with the addition of the return statement:
$ ./node_modules/.bin/lab
x
Failed tests:
1) test lab promise support:
Expected false to be true
at /Users/.../test/index.js:28:33
1 of 1 tests failed
Test duration: 11 ms
No global variable leaks detected
Think this is only lab > 10.1.0 so check your version.
In which case handling the catch is the solution. Either way no issue with lab from my perspective, it's not even possible for lab to intervene in the promise api unless we monkey patch it which gets a strong no from me.
an example when simple gets complicated and return is not posible
'use strict';
// Load modules
const Code = require('code');
const Lab = require('lab');
// Test shortcuts
const lab = exports.lab = Lab.script();
const it = lab.test;
const expect = Code.expect;
const cache = {};
const putInCache = (key, value, ttl) => {
return new Promise((resolve, reject) => {
cache[key] = { value, ttl };
resolve();
});
};
const getFromCache = (key) => {
return new Promise((resolve, reject) => {
const item = cache[key];
if (item.ttl < Date.now() || true) { // fail this to fail the test
resolve(null);
}
else {
resolve(item.value);
}
});
};
it('test lab promise support (fails with timeout)', (done) => {
putInCache('test', 'works', Date.now() + 5).then(() => {
setTimeout(() => {
getFromCache('test').then((value) => {
expect(value).to.be.equal('works');
done();
});
}, 3);
});
});
it('test lab promise support (works - but complicated)', (done) => {
putInCache('test', 'works', Date.now() + 5).then(() => {
setTimeout(() => {
getFromCache('test').then((value) => {
try {
expect(value).to.be.equal('works');
done();
}
catch (err) {
done(err);
}
});
}, 3);
});
});
setTimeout should be wrapped into promises, no offense but your doing it wrong :)
none taken, I'm here to explore a solution that I can't wrap my head into just yet , thanks for the the follow-ups guys
You really need to understand the promise api to get that there's nothing we can do here. I know nodejs started to deal with unhandled promise rejections but I doubt lab want to go that way.
I was thinking about passing done() to expect and instead of throwing errors, pass those on to done directly
something like expect(value).to.be.equal('works').throwTo(done);
big thanks for the wrapping setTimeout hint, it worked beautifully
if anyone arrives here, here is a complete working example
'use strict';
// Load modules
const Code = require('code');
const Lab = require('lab');
// Test shortcuts
const lab = exports.lab = Lab.script();
const it = lab.test;
const expect = Code.expect;
const cache = {};
const putInCache = (key, value, ttl) => {
return new Promise((resolve, reject) => {
cache[key] = { value, ttl };
resolve();
});
};
const getFromCache = (key) => {
return new Promise((resolve, reject) => {
const item = cache[key];
if (item.ttl < Date.now() || true) { // fail this to fail the test
resolve(null);
}
else {
resolve(item.value);
}
});
};
const delay = (time) => {
return new Promise((fulfill) => {
setTimeout(fulfill, time);
});
};
it('test lab promise support (now workis with timeout wrapped)', (done) => {
return putInCache('test', 'works', Date.now() + 5)
.then(() => delay(3))
.then(() => getFromCache('test'))
.then((value) => {
expect(value).to.be.equal('works');
});
});
You're getting dangerously close to http://chaijs.com/plugins/chai-as-promised/.
for me the wrapping of timeout solves all my issues, that was a thought that I won't consider until the occasion rises
big thanks to @Marsup and @mtharrison for your time and knowledge guys
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.