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

Not throw exception in when.js then callback when assert fail. #955

Closed
taoyuan opened this issue Aug 20, 2013 · 3 comments
Closed

Not throw exception in when.js then callback when assert fail. #955

taoyuan opened this issue Aug 20, 2013 · 3 comments

Comments

@taoyuan
Copy link

taoyuan commented Aug 20, 2013

Using when.js for promise something, checking callback's parameter.

var when = require('when');
var assert = require('chai').assert;
var fail = assert.fail;

describe('when', function () {

    it('should reject', function (done) {
        var expected = 123;
        var d = when.defer();
        d.promise.then(
            fail,
            function(err) {
                assert.equal(err, 1); // should throw exception, but timeout
                done();
            }
        );
        d.reject(expected);
    });
});

Should throw exception, but throw timeout after 2000ms.

1 failing

  1) when reject:
     Error: timeout of 2000ms exceeded
      at null.<anonymous> (./node_modules/mocha/lib/runnable.js:165:14)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Using buster, it's work:

var when = require('when');

var buster = require('buster');
buster.spec.expose();

var assert = buster.assert;
var fail = buster.assertions.fail;

describe('when', function (){
    it('reject', function (done) {
        var expected = 123;
        var d = when.defer();

        d.promise.then(
            fail,
            function(value) {
                assert.equals(value, 1);
                done();
            }
        );
        d.reject(expected);
    });
});

Throw the proper exception:

when: AF
Failure: when reject
    [assert.equals] 123 expected to be equal to 1
    at ./test/reject-buster.test.js:17:24
    at NearRejectedProxy.when (./node_modules/when/when.js:465:11)
    at Object._message (./node_modules/when/when.js:375:25)
    at deliver (./node_modules/when/when.js:270:7)
    at ./node_modules/when/when.js:482:5
    at Object.drainQueue [as _onImmediate] (./node_modules/when/when.js:801:4)
    at processImmediate [as _immediateCallback] (timers.js:330:15)

1 test case, 1 test, 1 assertion, 1 failure, 0 errors, 0 timeouts
Finished in 0.011s
@jonathanong
Copy link
Contributor

with the latest mocha, you just wanna return the promise

@boneskull
Copy link
Contributor

for the sake of a bit more explanation...

@taoyuan chai.assert throws an AssertionError when the assertion fails. I don't know Buster, but I'm willing to bet it doesn't.

Your test will timeout because the call to done() will never get hit, because of the thrown exception.

In Mocha v1.20.1, this is my output:

  when
Potentially unhandled rejection [2] AssertionError: expected 123 to equal 1
    at /Volumes/samara/projects/boneskull/adhoc/test/when.spec.js:13:24
    at tryCatchReject (/Volumes/samara/projects/boneskull/adhoc/node_modules/when/lib/makePromise.js:806:30)
    at runContinuation1 (/Volumes/samara/projects/boneskull/adhoc/node_modules/when/lib/makePromise.js:776:4)
    at Rejected.when (/Volumes/samara/projects/boneskull/adhoc/node_modules/when/lib/makePromise.js:615:4)
    at Pending.run (/Volumes/samara/projects/boneskull/adhoc/node_modules/when/lib/makePromise.js:430:13)
    at runQueue (/Volumes/samara/projects/boneskull/adhoc/node_modules/when/lib/Scheduler.js:75:18)
    at Scheduler._drain (/Volumes/samara/projects/boneskull/adhoc/node_modules/when/lib/Scheduler.js:50:3)
    at Scheduler.drain (/Volumes/samara/projects/boneskull/adhoc/node_modules/when/lib/Scheduler.js:26:9)
    at process._tickCallback (node.js:419:13)
    1) should reject


  0 passing (2s)
  1 failing

  1) when should reject:
     Error: timeout of 2000ms exceeded
      at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:139:19)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

As @jonathanong wrote, you want to simply return the promise instead of using done(). But, this will do what you want (I think):

  it('should reject', function (done) {
    var expected = 123;
    var d = when.defer();
    d.promise
      .then(fail, function (err) {
        assert.equal(err, 1); // should throw exception, but timeout
      })
      .then(done, done);
    d.reject(expected);
  });

@taoyuan
Copy link
Author

taoyuan commented Jul 14, 2014

@boneskull It's ok now. I run the test again using the old version [email protected], [email protected] and [email protected]. It appears that the issue caused by the [email protected].

In version [email protected], the test can not throw any exception but timeout shown:

  ․

  0 passing (2s)
  1 failing

  1) when should reject:
     Error: timeout of 2000ms exceeded
      at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:139:19)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Now the same test in version [email protected] throws the right exception and fails for timeout. It's correct.

Of cause, @boneskull gives the better way to test reject. Thanks.

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

3 participants