Skip to content

Commit

Permalink
#56 Add more tests to tst.context.js
Browse files Browse the repository at this point in the history
Reviewed by: David Pacheco <dap@joyent.com>
Approved by: David Pacheco <dap@joyent.com>
  • Loading branch information
melloc committed Jul 16, 2019
1 parent 03d7f04 commit f849e28
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions test/tst.context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,56 @@

var mod_assert = require('assert');
var mod_verror = require('../lib/verror');
var mod_isError = require('core-util-is').isError;
var mod_vm = require('vm');

var VError = mod_verror.VError;
var WError = mod_verror.WError;

var err = new Error();
var verr = new VError(err);
mod_assert.ok(mod_isError(verr.cause()));
var prog1 = 'callback(new Error(), "Error")';
var prog2 = 'var e = new Error(); e.name = "BarError"; callback(e, "BarError")';

function runTests(cerr, name) {
var verr;

/*
* The constructor should recognize the other context's Error as an
* error for wrapping, and not as an options object.
*/
verr = new VError(cerr);
mod_assert.equal(verr.cause(), cerr);

verr = new VError({ cause: cerr });
mod_assert.equal(verr.cause(), cerr);

/*
* The assertions done at each step while walking the cause chain
* should be okay with the other context's Error.
*/
mod_assert.deepEqual(
mod_verror.findCauseByName(cerr, 'FooError'), null);
mod_assert.equal(
mod_verror.findCauseByName(verr, name), cerr);

/*
* Verify that functions that take an Error as an argument
* accept the Error created in the other context.
*/
mod_assert.deepEqual(mod_verror.cause(cerr), null);
mod_assert.deepEqual(mod_verror.info(cerr), {});
mod_assert.equal(typeof (mod_verror.fullStack(cerr)), 'string');
}

var context = mod_vm.createContext({
'callback': function callback(err2) {
mod_assert.ok(mod_isError(err2));
var verr2 = new VError(err);
mod_assert.ok(mod_isError(verr2.cause()));
}
'callback': runTests
});
mod_vm.runInContext('callback(new Error())', context);

/*
* We run the same set of tests using two different errors: one whose name is
* the default "Error", and one whose name has been changed.
*
* Note that changing the name is not the same as having a constructor that
* inherits from Error. Such Errors are not currently supported when
* constructed in another context.
*/
mod_vm.runInContext(prog1, context);
mod_vm.runInContext(prog2, context);

0 comments on commit f849e28

Please sign in to comment.