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

beforeAll/afterAll don't work with focused specs #732

Closed
hankduan opened this issue Dec 23, 2014 · 13 comments
Closed

beforeAll/afterAll don't work with focused specs #732

hankduan opened this issue Dec 23, 2014 · 13 comments

Comments

@hankduan
Copy link

beforeAll and afterAll will run once per spec (like beforeEach and afterEach) when you use focused specs:

describe('focused spec', function() {
  var passed;

  beforeAll(function() {
    console.log('beforeAll')
    passed = 0;
  });

  afterAll(function() {
    console.log('afterAll')
    expect(passed).toEqual(2);
  });

  iit('should pass1', function() {
    ++passed;
  });

  iit('should pass3', function() {
    ++passed;
  });
});

The output is:

beforeAll
afterAll
FbeforeAll
afterAll
F

Failures: 
1) focused spec should pass1
1.1) Expected 1 to equal 2.
    Error: Expected 1 to equal 2.
        at Object.<anonymous> (/workspace/minijasminenode/spec/focused_spec.js:11:20)

2) focused spec should pass3
2.1) Expected 1 to equal 2.
    Error: Expected 1 to equal 2.
        at Object.<anonymous> (/workspace/minijasminenode/spec/focused_spec.js:11:20)

2 specs, 2 failures
Finished in 0.007 seconds
@Gerg
Copy link
Contributor

Gerg commented Dec 23, 2014

This is known behavior for the current release, though I agree it is not intuitive. Is this causing you issues?

@hankduan
Copy link
Author

I'm working on getting protractor to work with jasmine2.0 (angular/protractor#362), and noticed this when I was writing some tests for the integration I'm doing.

It's causing me issue in the sense that I feel like this issue will be brought up as people start using jasmine.

Another thing I noticed is that you can't put expects in afterAll/beforeAll unless you use a focused spec, unlike afterEach/beforeEach. Is this also a known behavior?

i.e.

describe('beforeEach/afterEach/it', function() {
  afterAll(function() {
    expect(1).toEqual(2);
  });

  it('should share same scope', function() {
    ++this.x;
  });
});

will not fail, but if you replace the it with a iit, it will fail

@slackersoft
Copy link
Member

Are you using jasmine 2.1 or 2.0? beforeAll, afterAll, fit, and fdescribe are only officially in 2.1 and should support running expects just fine. I notice your example is using iit which is not the syntax that jasmine itself now supports.

@hankduan
Copy link
Author

Sorry iit is something I added that just calls fit. I'm using 2.1.3.

Basically running something like this:

describe('beforeEach/afterEach/it', function() {
  afterAll(function() {
    console.log('here');
    expect(1).toEqual(2);
  });

  // note this is a regular 'it'. If you use 'fit', the expect in 'afterAll' works normally.
  it('should share same scope', function() {
    expect(1).toEqual(1); //random code
  });
});

Will execute the afterAll because the console.log prints. It also says there is 1 spec. However, the expect(1).toEqual(2) does not register as a failure.

Here's my output:

.here


1 spec, 0 failures
Finished in 0.005 seconds

@slackersoft
Copy link
Member

Failures in afterAlls are reported with the suite completion event and not with the spec itself. This means that whatever reporter you're using in lib/cli.js needs to also listen for the suiteDone event and report if there are any failedExpectations. (The jasmine npm does this for you).

This happens because by the time the afterAll is actually invoked, the specs in the suite have already reported their completion state. Sorry for the confusion.

@hankduan
Copy link
Author

Ah, thank you for the clarification. I'll fix up the reporter to also look at the suite completion event.

My original question still stands though. If you have multiple fit in a describe, the afterAll/beforeAll runs each time the fit runs. Is this something you guys will fix, or should I note that as working as intended?

@slackersoft
Copy link
Member

That is the current behavior as designed, but I probably wouldn't rely on it explicitly. We agree it's not the most awesome thing, but the fit and fdescribe functionality is treated the same as if a list of spec ids were passed into Env.execute in which case we don't know the order and just fall back to the safest way to get green specs, instead of trying to figure out exactly the right times to run the beforeAll and afterAlls. This way if unordered ids are passed in, we can make sure that regardless of the order of the ids passed in, we can make sure the appropriate setup has been run.

@slackersoft
Copy link
Member

It sounds like this is resolved now, so I'm going to close this issue. Please re-open if you feel it hasn't been fully resolved.

@juliemr
Copy link

juliemr commented Jan 22, 2015

FYI -this behavior is causing some confusion for Protractor users - see angular/protractor#1743.

It would be great if it could be addressed in a future version, or if there were a good place to point to for documentation on how fit or fdescribe will affect your test run.

@dtabuenc
Copy link

Not only does it run once for each it, but it seems to run additional times as well. Please see here:

http://jsfiddle.net/wzAyL/257/

Gerg added a commit that referenced this issue Jan 24, 2015
- This requires passing if runnables are set to the Suite. Hopefully in
  the future we will change how focused runnables and *Alls interact so
  this is no longer necessary.

[#732]
@Gerg
Copy link
Contributor

Gerg commented Jan 24, 2015

I pushed a fix for the additional beforeAll runs.

We agree that the interaction between beforeAll/afterAll and fit/fdescribe is confusing. We would like to change this in a future release.

@PaddyMann
Copy link

Wondering why this was closed - is there a separate location we can track progress on this being fixed? (i.e. beforeAll / afterAll only being called once with a focused describe block)

@slackersoft
Copy link
Member

As of Jasmine 2.3, beforeAll and afterAll should behave the same with focused suites as without. I.e. they should only run once, instead of before each spec.

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

6 participants