GithubHelp home page GithubHelp logo

Assert errors about code HOT 12 CLOSED

hapijs avatar hapijs commented on June 23, 2024
Assert errors

from code.

Comments (12)

ulrikstrid avatar ulrikstrid commented on June 23, 2024

Can't you use something like this?

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

from code.

gagle avatar gagle commented on June 23, 2024

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

from code.

AdriVanHoudt avatar AdriVanHoudt commented on June 23, 2024

Hoek.assert(!err, err); ?

from code.

gagle avatar gagle commented on June 23, 2024

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()
  });
});

from code.

AdriVanHoudt avatar AdriVanHoudt commented on June 23, 2024

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 ?

from code.

gagle avatar gagle commented on June 23, 2024

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

from code.

ulrikstrid avatar ulrikstrid commented on June 23, 2024

Why would you want to throw in your test suite?

from code.

gagle avatar gagle commented on June 23, 2024

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

from code.

ulrikstrid avatar ulrikstrid commented on June 23, 2024

Shouldn't the stacktrace be shown anyway?

from code.

cjihrig avatar cjihrig commented on June 23, 2024

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)

from code.

cjihrig avatar cjihrig commented on June 23, 2024

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.

from code.

lock avatar lock commented on June 23, 2024

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.

from code.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.