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

How do I run before and after asynchronous specs in jasmine 2.0 #526

Closed
wheresrhys opened this issue Feb 21, 2014 · 7 comments
Closed

How do I run before and after asynchronous specs in jasmine 2.0 #526

wheresrhys opened this issue Feb 21, 2014 · 7 comments

Comments

@wheresrhys
Copy link

The new done() syntax, at first glance, appears to be far less flexible than the old syntax. Using done how do I implement something like:

it('should allow multi stage asynchronisity', function () {
    runs(function () {
        //set up
        var initialState
        // call something asynchronous
        expect(middleState).toEqual(initialState);
    });

    waits(200);

    runs(function () {
        expect(middleState).not.toEqual(initialState);
       // call something else asynchronous
        expect(finalState).toEqual(middleState);
    });

    waits(200);

    runs(function () {
       // call something else asynchronous
        expect(finalState).not.toEqual(middleState);
    });
});
@infews
Copy link
Contributor

infews commented Feb 21, 2014

beforeEach and afterEach also take done callbacks. So you can do your setup in a before, then call done, then have your it do your expectations.

Something more like:

describe('should allow multi stage asynchronisity', function () {
    beforeEach(function (done) {
        //set up
        var initialState
        // call something asynchronous
        done();
    });

    it(function (done) {
        expect(middleState).not.toEqual(initialState);
       // call something else asynchronous
        expect(finalState).toEqual(middleState);
        done();
    });
});

@wheresrhys
Copy link
Author

Hmmm... So does this limit me to 3 stages and only via the artificial step
of a wrapping my spec with a before and after in a describe? If so this is
a worse API than 1.3

On Friday, February 21, 2014, Davis W. Frank notifications@github.com
wrote:

beforeEach and afterEach also take done callbacks. So you can do your
setup in a before, then call done, then have your it do your expectations.

Something more like:

describe('should allow multi stage asynchronisity', function () {
beforeEach(function (done) {
//set up
var initialState
// call something asynchronous
done();
});

it(function (done) {
    expect(middleState).not.toEqual(initialState);
   // call something else asynchronous
    expect(finalState).toEqual(middleState);
    done();
});

});

Reply to this email directly or view it on GitHubhttps://github.com//issues/526#issuecomment-35764811
.

Sent with thumbs (mostly the right one, which is developing a bit of a
callous) from my Nokia 3210

@slackersoft
Copy link
Member

You can have multiple befores with done callbacks like so:

describe('should allow multi stage asynchronisity', function () {
    beforeEach(function (done) {
        //set up
        var initialState
        // call something asynchronous
        done();
    });

    beforeEach(function (done) {
        // check some intermediate state
        // call something asynchronous
        done();
    });

    it(function (done) {
        expect(middleState).not.toEqual(initialState);
       // call something else asynchronous
        expect(finalState).toEqual(middleState);
        done();
    });
});

The befores will be called in the order they're defined.

@wheresrhys
Copy link
Author

Thanks @slackersoft. I'll use that approach. I Still thinks it's a poor API if you're forced to use beforeEachs that are only meant for a single spec. Perhaps the API should be changed so that if an it is passed multiple functions as arguments then they are called in order?

@sheelc
Copy link
Contributor

sheelc commented Feb 24, 2014

The hope is that the main done callback is only executed once all asynchronous calls have completed (allowing the user to handle as many async steps as are necessary in a single before or it)

Using callback functions, your test could look something like this:

it("multi-stage async", function(done) {
    var initialState;

    somethingAsynchronous(function() {
        expect(middleState).not.toEqual(initialState);
        somethingElseAsync(function() {
            expect(finalState).not.toEqual(middleState);
            done();
        });

        expect(finalState).toEqual(middleState);
    });

    expect(middleState).toEqual(initialState);
});

Using promises (so that the expectations appear in order they are called), the same test could look like this:

it("multi-stage async with promises", function(done) {
    var initialState;

    var promise = somethingAsynchronous();
    expect(middleState).toEqual(initialState);

    promise.then(function() {
        expect(middleState).not.toEqual(initialState);
        var innerPromise = somethingElseAsync();
        expect(finalState).toEqual(middleState);
        return innerPromise;
    }).then(function() {
        expect(finalState).not.toEqual(middleState);
        done();
    });
});

(Also for #530 which is related to this issue, it is worth noting that expect can be called in a before if so desired.)

Does that help this issue?

@jeremyhill-up
Copy link

I don't like to be a complainer, but that's so much more boilerplate for so much less functionality. One of the major draws of Jasmine was the simple syntax (describe, it, beforeEach, etc). Now beforeEach is overloaded to mean significantly different things, the clean syntax of waits, runs is gone -- it just feels like an anti-pattern to me.

@infews
Copy link
Contributor

infews commented Jun 23, 2014

I think you're pointing out problems that many projects run into with testing asynchronous code asynchronously.

We moved to the done() syntax as the async-heavy community found it workable in Mocha. We're not averse to improving it over time. Thanks for the feedback and thanks for using Jasmine.

Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants