-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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: improve ifError #18247
assert: improve ifError #18247
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
let err; | ||
// Create some random error frames. | ||
(function a() { | ||
(function b() { | ||
(function c() { | ||
err = new Error('test error'); | ||
})(); | ||
})(); | ||
})(); | ||
|
||
(function x() { | ||
(function y() { | ||
(function z() { | ||
assert.ifError(err); | ||
})(); | ||
})(); | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
assert.js:* | ||
throw newErr; | ||
^ | ||
|
||
AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error | ||
at z (*/if-error-has-good-stack.js:*:* | ||
at y (*/if-error-has-good-stack.js:*:*) | ||
at x (*/if-error-has-good-stack.js:*:*) | ||
at Object.<anonymous> (*/if-error-has-good-stack.js:*:*) | ||
at c (*/if-error-has-good-stack.js:*:*) | ||
at b (*/if-error-has-good-stack.js:*:*) | ||
at a (*/if-error-has-good-stack.js:*:*) | ||
at Object.<anonymous> (*/if-error-has-good-stack.js:*:*) | ||
at Module._compile (module.js:*:*) | ||
at Object.Module._extensions..js (module.js:*:*) | ||
at Module.load (module.js:*:*) | ||
at tryModuleLoad (module.js:*:*) | ||
at Function.Module._load (module.js:*:*) | ||
at Function.Module.runMain (module.js:*:*) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert').strict; | ||
/* eslint-disable no-restricted-properties */ | ||
|
||
// Test that assert.ifError has the correct stack trace of both stacks. | ||
|
||
let err; | ||
// Create some random error frames. | ||
(function a() { | ||
(function b() { | ||
(function c() { | ||
err = new Error('test error'); | ||
})(); | ||
})(); | ||
})(); | ||
|
||
const msg = err.message; | ||
const stack = err.stack; | ||
|
||
(function x() { | ||
(function y() { | ||
(function z() { | ||
let threw = false; | ||
try { | ||
assert.ifError(err); | ||
} catch (e) { | ||
assert.equal(e.message, 'ifError got unwanted exception: test error'); | ||
assert.equal(err.message, msg); | ||
assert.equal(e.actual, err); | ||
assert.equal(e.actual.stack, stack); | ||
assert.equal(e.expected, null); | ||
assert.equal(e.operator, 'ifError'); | ||
threw = true; | ||
} | ||
assert(threw); | ||
})(); | ||
})(); | ||
})(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer a test showing a full stack trace, rather than this check. It would mean future edits are easier to understand and fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a separate test to check for the stack trace and minimized this test to the necessary parts. |
||
|
||
assert.throws( | ||
() => assert.ifError(new TypeError()), | ||
{ | ||
message: 'ifError got unwanted exception: TypeError' | ||
} | ||
); | ||
|
||
assert.throws( | ||
() => assert.ifError({ stack: false }), | ||
{ | ||
message: 'ifError got unwanted exception: { stack: false }' | ||
} | ||
); | ||
|
||
assert.throws( | ||
() => assert.ifError({ constructor: null, message: '' }), | ||
{ | ||
message: 'ifError got unwanted exception: ' | ||
} | ||
); | ||
|
||
assert.throws( | ||
() => { assert.ifError(false); }, | ||
{ | ||
message: 'ifError got unwanted exception: false' | ||
} | ||
); | ||
|
||
assert.doesNotThrow(() => { assert.ifError(null); }); | ||
assert.doesNotThrow(() => { assert.ifError(); }); | ||
assert.doesNotThrow(() => { assert.ifError(undefined); }); | ||
|
||
// https://github.com/nodejs/node-v0.x-archive/issues/2893 | ||
{ | ||
let threw = false; | ||
try { | ||
// eslint-disable-next-line no-restricted-syntax | ||
assert.throws(() => { | ||
assert.ifError(null); | ||
}); | ||
} catch (e) { | ||
threw = true; | ||
assert.strictEqual(e.message, 'Missing expected exception.'); | ||
assert(!e.stack.includes('throws'), e.stack); | ||
} | ||
assert(threw); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the goal of this code is not really clear, can you add some comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.