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

Assert errors #20

Closed
gagle opened this issue Mar 18, 2015 · 12 comments
Closed

Assert errors #20

gagle opened this issue Mar 18, 2015 · 12 comments
Assignees
Labels
non issue Issue is not a problem or requires changes

Comments

@gagle
Copy link

gagle commented Mar 18, 2015

How can we check that an error must not exist but if it exists, throw the error? This is for asynchronous functions. Right now I do:

var expect = code.expect;
it('foo', function(done) {
  foo(function (err) {
    expect(err).to.not.exist();
    done()
  });
});

But when the function returns an error, lab just tells that err exists when it should not, the stacktrace of the error is not printed.

I'd like to have a function with the identical behaviour of node's built-in assert.ifError(), something like:

var ifError = code.ifError;
it('foo', function(done) {
  foo(function (err) {
    ifError(err); // throws err if err is truthy
    done()
  });
});
@ulrikstrid
Copy link

Can't you use something like this?

expect(foo()).not.to.throw(err, 'message');

@gagle
Copy link
Author

gagle commented Mar 18, 2015

Nop, foo is asynchronous, it never throws the error, it is returned as an argument.

@AdriVanHoudt
Copy link
Contributor

Hoek.assert(!err, err); ?

@gagle
Copy link
Author

gagle commented Mar 18, 2015

hoek.assert() definitely solves the problem but it's not the ideal solution since hoek is a general utilities module, we need something more aligned with code. The implementation of code.ifError() is trivial.

exports.ifError = function (err) {
  if (err) throw err;
}

We can simply use the native function but it'd be good if it is integrated into code:

var assert = require('assert');
it('foo', function(done) {
  foo(function (err) {
    assert.ifError(err);
    done()
  });
});

@AdriVanHoudt
Copy link
Contributor

the reason Code doesn't throw is to continue the tests even if 1 fails. If you want the stack trace of an actual error you can use https://github.com/hapijs/hoek#displaystackslice ?

@gagle
Copy link
Author

gagle commented Mar 18, 2015

Well, I need the stack and the error description. If an error occurs I simply want to throw the error.

@ulrikstrid
Copy link

Why would you want to throw in your test suite?

@gagle
Copy link
Author

gagle commented Mar 18, 2015

To see why expect(err).to.not.exist(); is failing.

@ulrikstrid
Copy link

Shouldn't the stacktrace be shown anyway?

@cjihrig
Copy link
Contributor

cjihrig commented Mar 18, 2015

expect(err).to.not.exist() is the correct thing to do. It will behave like your proposed ifError() method (see below).

> Code = require('code');
{ expect: [Function], incomplete: [Function], count: [Function] }
> expect = Code.expect;
[Function]
> expect(null).to.not.exist();
{ _ref: null, _prefix: '', _location: 'repl:1.1', _flags: {} }
> expect(undefined).to.not.exist();
{ _ref: undefined,
  _prefix: '',
  _location: 'repl:1.8',
  _flags: {} }
> expect(new Error('foo')).to.not.exist();
Error: Expected [Error: foo] to not exist
    at null.<anonymous> (/private/tmp/node_modules/code/lib/index.js:142:17)
    at repl:1:33
    at REPLServer.defaultEval (repl.js:116:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:269:12)
    at emitOne (events.js:77:13)
    at REPLServer.emit (events.js:166:7)
    at REPLServer.Interface._onLine (readline.js:195:10)
    at REPLServer.Interface._line (readline.js:534:8)

@cjihrig cjihrig closed this as completed Mar 18, 2015
@cjihrig
Copy link
Contributor

cjihrig commented Mar 18, 2015

That's because Lab catches errors so the process does not crash and all of your tests can run. The same behavior should be seen if you explicitly throw an exception.

@hueniverse hueniverse added the non issue Issue is not a problem or requires changes label Mar 27, 2015
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
non issue Issue is not a problem or requires changes
Projects
None yet
Development

No branches or pull requests

5 participants