Skip to content

Commit 7ec6a69

Browse files
Trottitaloacasas
authored andcommitted
test: add missing initialization in test-assert
test-assert contains Boolean checks without initializing the Boolean to false. It will be true thanks to previous tests in the file. Block-scope all instances of `threw` so that side effects like this are not an issue. Add missing initializations for `threw` in the tests where it is missing. PR-URL: #11191 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent b766dab commit 7ec6a69

File tree

1 file changed

+72
-58
lines changed

1 file changed

+72
-58
lines changed

test/parallel/test-assert.js

+72-58
Original file line numberDiff line numberDiff line change
@@ -348,37 +348,44 @@ assert.throws(makeBlock(thrower, a.AssertionError));
348348
assert.throws(makeBlock(thrower, TypeError));
349349

350350
// when passing a type, only catch errors of the appropriate type
351-
let threw = false;
352-
try {
353-
a.throws(makeBlock(thrower, TypeError), a.AssertionError);
354-
} catch (e) {
355-
threw = true;
356-
assert.ok(e instanceof TypeError, 'type');
351+
{
352+
let threw = false;
353+
try {
354+
a.throws(makeBlock(thrower, TypeError), a.AssertionError);
355+
} catch (e) {
356+
threw = true;
357+
assert.ok(e instanceof TypeError, 'type');
358+
}
359+
assert.strictEqual(true, threw,
360+
'a.throws with an explicit error is eating extra errors',
361+
a.AssertionError);
357362
}
358-
assert.strictEqual(true, threw,
359-
'a.throws with an explicit error is eating extra errors',
360-
a.AssertionError);
361-
threw = false;
362363

363364
// doesNotThrow should pass through all errors
364-
try {
365-
a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
366-
} catch (e) {
367-
threw = true;
368-
assert.ok(e instanceof TypeError);
365+
{
366+
let threw = false;
367+
try {
368+
a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
369+
} catch (e) {
370+
threw = true;
371+
assert.ok(e instanceof TypeError);
372+
}
373+
assert.strictEqual(true, threw, 'a.doesNotThrow with an explicit error is ' +
374+
'eating extra errors');
369375
}
370-
assert.strictEqual(true, threw, 'a.doesNotThrow with an explicit error is ' +
371-
'eating extra errors');
372376

373377
// key difference is that throwing our correct error makes an assertion error
374-
try {
375-
a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
376-
} catch (e) {
377-
threw = true;
378-
assert.ok(e instanceof a.AssertionError);
378+
{
379+
let threw = false;
380+
try {
381+
a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
382+
} catch (e) {
383+
threw = true;
384+
assert.ok(e instanceof a.AssertionError);
385+
}
386+
assert.strictEqual(true, threw,
387+
'a.doesNotThrow is not catching type matching errors');
379388
}
380-
assert.strictEqual(true, threw,
381-
'a.doesNotThrow is not catching type matching errors');
382389

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

392399
// make sure that validating using constructor really works
393-
threw = false;
394-
try {
395-
assert.throws(
396-
function() {
397-
throw ({}); // eslint-disable-line no-throw-literal
398-
},
399-
Array
400-
);
401-
} catch (e) {
402-
threw = true;
400+
{
401+
let threw = false;
402+
try {
403+
assert.throws(
404+
function() {
405+
throw ({}); // eslint-disable-line no-throw-literal
406+
},
407+
Array
408+
);
409+
} catch (e) {
410+
threw = true;
411+
}
412+
assert.ok(threw, 'wrong constructor validation');
403413
}
404-
assert.ok(threw, 'wrong constructor validation');
405414

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

416425
// https://github.com/nodejs/node/issues/3188
417-
threw = false;
426+
{
427+
let threw = false;
418428

419-
let AnotherErrorType;
420-
try {
421-
const ES6Error = class extends Error {};
429+
let AnotherErrorType;
430+
try {
431+
const ES6Error = class extends Error {};
422432

423-
AnotherErrorType = class extends Error {};
433+
AnotherErrorType = class extends Error {};
424434

425-
const functionThatThrows = function() {
426-
throw new AnotherErrorType('foo');
427-
};
435+
const functionThatThrows = function() {
436+
throw new AnotherErrorType('foo');
437+
};
428438

429-
assert.throws(functionThatThrows, ES6Error);
430-
} catch (e) {
431-
threw = true;
432-
assert(e instanceof AnotherErrorType,
433-
`expected AnotherErrorType, received ${e}`);
434-
}
439+
assert.throws(functionThatThrows, ES6Error);
440+
} catch (e) {
441+
threw = true;
442+
assert(e instanceof AnotherErrorType,
443+
`expected AnotherErrorType, received ${e}`);
444+
}
435445

436-
assert.ok(threw);
446+
assert.ok(threw);
447+
}
437448

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

517528
// #2893
518-
try {
519-
assert.throws(function() {
520-
assert.ifError(null);
521-
});
522-
} catch (e) {
523-
threw = true;
524-
assert.strictEqual(e.message, 'Missing expected exception..');
529+
{
530+
let threw = false;
531+
try {
532+
assert.throws(function() {
533+
assert.ifError(null);
534+
});
535+
} catch (e) {
536+
threw = true;
537+
assert.strictEqual(e.message, 'Missing expected exception..');
538+
}
539+
assert.ok(threw);
525540
}
526-
assert.ok(threw);
527541

528542
// #5292
529543
try {

0 commit comments

Comments
 (0)