Skip to content
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

Closed
scottcorgan opened this issue Apr 17, 2014 · 23 comments
Closed

Supporting promises #79

scottcorgan opened this issue Apr 17, 2014 · 23 comments
Labels
support Questions, discussions, and general support

Comments

@scottcorgan
Copy link

Does Lab support promise testing similar to Mocha?

@hueniverse
Copy link
Contributor

Not that I'm aware of, but I also know nothing about promises so have no idea what would it take to support it.

@jakecraige
Copy link

👍 for this being added. mocha supports them and that's a huge reason as to why I use it

@hueniverse
Copy link
Contributor

Why not keep using mocha? We're not trying to move anyone away from it.

@jakecraige
Copy link

@hueniverse Since that's the case, Why would someone use this over something like mocha?

@danielb2
Copy link
Contributor

Lab is really an internal solution. It's not advocated that anyone use it over mocha. Correct me if I'm wrong.

@hueniverse
Copy link
Contributor

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.

@Marsup
Copy link
Contributor

Marsup commented Dec 16, 2014

@geek Would you be open to revisit this ?

@geek
Copy link
Member

geek commented Dec 16, 2014

@Marsup sure, how do you imagine promises being used within lab?

@Marsup
Copy link
Contributor

Marsup commented Dec 16, 2014

What about returning a promise and let lab handle the then/catch to trigger the done ?

@geek
Copy link
Member

geek commented Dec 17, 2014

That works, any chance we can make it a dev dependency?

@Marsup
Copy link
Contributor

Marsup commented Dec 17, 2014

Sure, since the promise is provided by the test you don't need a hard dependency on it.

@k-sheth
Copy link

k-sheth commented Jan 19, 2015

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

@Traviskn
Copy link

Traviskn commented Oct 9, 2015

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!

@robkorv
Copy link

robkorv commented Dec 18, 2015

@Traviskn, great comment, I also was struggling with testing promises. An example would be a great addition to the README. A test even more.

@robkorv
Copy link

robkorv commented Dec 18, 2015

There are some situation where @Traviskn example doesn't work. When you have an assert in then() or catch() and it doesn't trigger, the assert won't go off. You should add a custom error to done() in then() or catch() when you don't expect it to trigger.

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);
        });
    });
});

@Marsup
Copy link
Contributor

Marsup commented Dec 18, 2015

@geek can I take a shot at it ?

@Traviskn
Copy link

@robkorv thanks for the very complete examples! definitely worth documenting somewhere

@robkorv
Copy link

robkorv commented Dec 20, 2015

@Traviskn your welcome.
@Marsup https://github.com/domenic/chai-as-promised ported to lab/code would be great.

@Marsup
Copy link
Contributor

Marsup commented Dec 20, 2015

@robkorv Nope, my main goal with that feature is to use async/await, eliminating the need for such a library.

@robkorv
Copy link

robkorv commented Dec 20, 2015

@Marsup, didn't know about async/await. Looks promising 😄.

@geek
Copy link
Member

geek commented Dec 21, 2015

@Marsup sounds good to me, thank you.

@Ajedi32
Copy link

Ajedi32 commented Jan 14, 2016

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?

@Marsup Marsup added support Questions, discussions, and general support and removed question labels Sep 21, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

10 participants