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

False positives when mocha allowes async execution between "before" and test #1468

Closed
TheLudd opened this issue Dec 13, 2014 · 5 comments
Closed

Comments

@TheLudd
Copy link

TheLudd commented Dec 13, 2014

Below are 3 examples of tests that all pass but as far as I can see, they should not. The tests all go through a before or beforeEach function that performs changes necessary for the tests to pass, but they all do it on the next tick and without telling mocha to wait.

It appears that mocha allows at least one tick between the before functions and the tests. Is this correct? I would have expected it to run synchronously.

var chai = require('chai');
var Promise = require('bluebird');
chai.should()

describe('test', function() {
  var changedInBefore = 0;
  var changedInBeforeEach = 0;
  var nextTick = 0;

  before(function() {
    //Not returning promise, expecting the tests to run before setting changedInBefore to 1
    Promise.resolve().then(function() {
      changedInBefore = 1
    });
  });

  beforeEach(function() {
    //Not returning promise, expecting the tests to run before setting changedInBeforeEach to 1
    Promise.resolve().then(function() {
      changedInBeforeEach = 1
    });
  });

  beforeEach(function(done) {
    process.nextTick(function() {
      nextTick = 1;
    });
    done(); //Calling done before nextTick is set to 1, expecting tests to run before this happens
  });

  it('before with promise', function() {
    changedInBefore.should.equal(1);
  });

  it('beforeEach with promise', function() {
    changedInBeforeEach.should.equal(1);
  });

  it('beforeEach with next tick', function() {
    nextTick.should.equal(1);
  });

}
@TheLudd
Copy link
Author

TheLudd commented Dec 16, 2014

@boneskull When you say unconfirmed, do you mean that you haven't had any time to test it or that you tried and could not recreate it?

@maboiteaspam
Copy link

why you have not used done in before / beforeEach if you want them to sync ?

@TheLudd
Copy link
Author

TheLudd commented Dec 16, 2014

@maboiteaspam Not sure about your question...
My point is that I expect these tests to fail but they do not. I have tried to clarify by adding comments to my code.

@jbnicolai
Copy link

You seem to misunderstand how process.nextTick and Promise.resolve() work. Both will call their callback after the function (before/beforeEach) they are in, but before the next it block.

@TheLudd
Copy link
Author

TheLudd commented Jul 7, 2015

@jbnicolai No, there is no misunderstanding of process.nextTick and Promise.resolve. The question was about mocha. The clarification I still lack is about this:

It appears that mocha allows at least one tick between the before functions and the tests. Is this correct? I would have expected it to run synchronously.

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

4 participants