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

test: add missing initialization in test-assert #11191

Closed
wants to merge 1 commit into from
Closed
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
130 changes: 72 additions & 58 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,37 +348,44 @@ assert.throws(makeBlock(thrower, a.AssertionError));
assert.throws(makeBlock(thrower, TypeError));

// when passing a type, only catch errors of the appropriate type
let threw = false;
try {
a.throws(makeBlock(thrower, TypeError), a.AssertionError);
} catch (e) {
threw = true;
assert.ok(e instanceof TypeError, 'type');
{
let threw = false;
try {
a.throws(makeBlock(thrower, TypeError), a.AssertionError);
} catch (e) {
threw = true;
assert.ok(e instanceof TypeError, 'type');
}
assert.strictEqual(true, threw,
'a.throws with an explicit error is eating extra errors',
a.AssertionError);
}
assert.strictEqual(true, threw,
'a.throws with an explicit error is eating extra errors',
a.AssertionError);
threw = false;

// doesNotThrow should pass through all errors
try {
a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
} catch (e) {
threw = true;
assert.ok(e instanceof TypeError);
{
let threw = false;
try {
a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
} catch (e) {
threw = true;
assert.ok(e instanceof TypeError);
}
assert.strictEqual(true, threw, 'a.doesNotThrow with an explicit error is ' +
'eating extra errors');
}
assert.strictEqual(true, threw, 'a.doesNotThrow with an explicit error is ' +
'eating extra errors');

// key difference is that throwing our correct error makes an assertion error
try {
a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
} catch (e) {
threw = true;
assert.ok(e instanceof a.AssertionError);
{
let threw = false;
try {
a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
} catch (e) {
threw = true;
assert.ok(e instanceof a.AssertionError);
}
assert.strictEqual(true, threw,
'a.doesNotThrow is not catching type matching errors');
}
assert.strictEqual(true, threw,
'a.doesNotThrow is not catching type matching errors');

assert.throws(function() { assert.ifError(new Error('test error')); });
assert.doesNotThrow(function() { assert.ifError(null); });
Expand All @@ -390,18 +397,20 @@ assert.throws(() => {
'a.doesNotThrow ignores user message');

// make sure that validating using constructor really works
threw = false;
try {
assert.throws(
function() {
throw ({});
},
Array
);
} catch (e) {
threw = true;
{
let threw = false;
try {
assert.throws(
function() {
throw ({});
},
Array
);
} catch (e) {
threw = true;
}
assert.ok(threw, 'wrong constructor validation');
}
assert.ok(threw, 'wrong constructor validation');

// use a RegExp to validate error message
a.throws(makeBlock(thrower, TypeError), /test/);
Expand All @@ -414,26 +423,28 @@ a.throws(makeBlock(thrower, TypeError), function(err) {
});

// https://github.com/nodejs/node/issues/3188
threw = false;
{
let threw = false;

let AnotherErrorType;
try {
const ES6Error = class extends Error {};
let AnotherErrorType;
try {
const ES6Error = class extends Error {};

AnotherErrorType = class extends Error {};
AnotherErrorType = class extends Error {};

const functionThatThrows = function() {
throw new AnotherErrorType('foo');
};
const functionThatThrows = function() {
throw new AnotherErrorType('foo');
};

assert.throws(functionThatThrows, ES6Error);
} catch (e) {
threw = true;
assert(e instanceof AnotherErrorType,
`expected AnotherErrorType, received ${e}`);
}
assert.throws(functionThatThrows, ES6Error);
} catch (e) {
threw = true;
assert(e instanceof AnotherErrorType,
`expected AnotherErrorType, received ${e}`);
}

assert.ok(threw);
assert.ok(threw);
}

// https://github.com/nodejs/node/issues/6416
// Make sure circular refs don't throw.
Expand Down Expand Up @@ -515,15 +526,18 @@ testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
'{ a: NaN, b: Infinity, c: -Infinity }');

// #2893
try {
assert.throws(function() {
assert.ifError(null);
});
} catch (e) {
threw = true;
assert.strictEqual(e.message, 'Missing expected exception..');
{
let threw = false;
try {
assert.throws(function() {
assert.ifError(null);
});
} catch (e) {
threw = true;
assert.strictEqual(e.message, 'Missing expected exception..');
}
assert.ok(threw);
}
assert.ok(threw);

// #5292
try {
Expand Down