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

Forbid node-style done callback in mocha it tests #8966

Closed
jridgewell opened this issue Apr 26, 2017 · 1 comment
Closed

Forbid node-style done callback in mocha it tests #8966

jridgewell opened this issue Apr 26, 2017 · 1 comment

Comments

@jridgewell
Copy link
Contributor

jridgewell commented Apr 26, 2017

Imagine code like this:

    it('test stuff', done => {
      unit.doSomething();
      setTimeout(() => {
        expect(somethingDoneAsync).equal('value');
        done();
      }, 0);
    });

But, there's a trap! If that expectation does not pass, we never hit the done callback and the assertion error will be swallowed!

The only appropriate way to use done callbacks in tests are:

// Promises. This is what we should be doing.
    it('test stuff', () => {
      unit.doSomething();
      return new Promise(resolve => {
        setTimeout(resolve, 0));
      }).then(() => {
        expect(somethingDoneAsync).equal('value');
      });
    });
// Node style. But why would you do this?
      setTimeout(() => {
        let error = null;
        try {
          expect(somethingDoneAsync).equal('value');
        } catch (e) {
          error = e;
        }
        done(error);
      }, 0);
@jridgewell
Copy link
Contributor Author

extensions/amp-font/0.1/test/test-amp-font.js
55:  it('should timeout while loading custom font', function(done) {
67:  it('should load custom font', function(done) {

test/functional/test-timer.js
63:    timer.delay(done);
81:    timer.delay(done);

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

No branches or pull requests

4 participants