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

error,test: migrating error to internal/errors #11505

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,9 @@ function _throws(shouldThrow, block, expected, message) {
var actual;

if (typeof block !== 'function') {
throw new TypeError('"block" argument must be a function');
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'function',
typeof block);
}

if (typeof expected === 'string') {
Expand Down
68 changes: 37 additions & 31 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,40 +655,46 @@ try {
'Message incorrectly marked as generated');
}

// Verify that throws() and doesNotThrow() throw on non-function block
function testBlockTypeError(method, block) {
let threw = true;

try {
method(block);
threw = false;
} catch (e) {
assert.strictEqual(e.toString(),
'TypeError: "block" argument must be a function');
}
{
// Verify that throws() and doesNotThrow() throw on non-function block
const validationFunction = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});

const testBlockTypeError = (method, block) => {
let threw = true;

try {
method(block);
threw = false;
} catch (e) {
validationFunction(e);
}

assert.ok(threw);
};

assert.ok(threw);
testBlockTypeError(assert.throws, 'string');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion, but I think it would be nice if these could be turned into
testBlockTypeError(() => assert.throws('string')); etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax Tempted to leave this PR as-is just so I can put your suggestion on a list for Code + Learn... Is that unhealthy? :-P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s perfectly okay :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax Having a hard time writing this up for Code + Learn in a way that makes it clear why we want to do this. (Probably because I see it as a style preference.) Would you want to try? If not, no biggie, I'll just leave it off the list. We have enough tasks for the next Code + Learn.

testBlockTypeError(assert.doesNotThrow, 'string');
testBlockTypeError(assert.throws, 1);
testBlockTypeError(assert.doesNotThrow, 1);
testBlockTypeError(assert.throws, true);
testBlockTypeError(assert.doesNotThrow, true);
testBlockTypeError(assert.throws, false);
testBlockTypeError(assert.doesNotThrow, false);
testBlockTypeError(assert.throws, []);
testBlockTypeError(assert.doesNotThrow, []);
testBlockTypeError(assert.throws, {});
testBlockTypeError(assert.doesNotThrow, {});
testBlockTypeError(assert.throws, /foo/);
testBlockTypeError(assert.doesNotThrow, /foo/);
testBlockTypeError(assert.throws, null);
testBlockTypeError(assert.doesNotThrow, null);
testBlockTypeError(assert.throws, undefined);
testBlockTypeError(assert.doesNotThrow, undefined);
}

testBlockTypeError(assert.throws, 'string');
testBlockTypeError(assert.doesNotThrow, 'string');
testBlockTypeError(assert.throws, 1);
testBlockTypeError(assert.doesNotThrow, 1);
testBlockTypeError(assert.throws, true);
testBlockTypeError(assert.doesNotThrow, true);
testBlockTypeError(assert.throws, false);
testBlockTypeError(assert.doesNotThrow, false);
testBlockTypeError(assert.throws, []);
testBlockTypeError(assert.doesNotThrow, []);
testBlockTypeError(assert.throws, {});
testBlockTypeError(assert.doesNotThrow, {});
testBlockTypeError(assert.throws, /foo/);
testBlockTypeError(assert.doesNotThrow, /foo/);
testBlockTypeError(assert.throws, null);
testBlockTypeError(assert.doesNotThrow, null);
testBlockTypeError(assert.throws, undefined);
testBlockTypeError(assert.doesNotThrow, undefined);

// https://github.com/nodejs/node/issues/3275
// eslint-disable-next-line no-throw-literal
assert.throws(() => { throw 'error'; }, (err) => err === 'error');
Expand Down