-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assert: deprecate assert.fail partially
Using `assert.fail()` with more than one argument is not intuitive to use and has no benefit over using a message on its own. Therefore this introduces a runtime deprecation in case it is used in that way. PR-URL: #18418 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- Loading branch information
Showing
5 changed files
with
142 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
common.expectWarning( | ||
'DeprecationWarning', | ||
'assert.fail() with more than one argument is deprecated. ' + | ||
'Please use assert.strictEqual() instead or only pass a message.' | ||
); | ||
|
||
// Two args only, operator defaults to '!=' | ||
assert.throws(() => { | ||
assert.fail('first', 'second'); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
name: 'AssertionError [ERR_ASSERTION]', | ||
message: '\'first\' != \'second\'', | ||
operator: '!=', | ||
actual: 'first', | ||
expected: 'second' | ||
}); | ||
|
||
// Three args | ||
assert.throws(() => { | ||
assert.fail('ignored', 'ignored', 'another custom message'); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
name: 'AssertionError [ERR_ASSERTION]', | ||
message: 'another custom message', | ||
operator: undefined, | ||
actual: 'ignored', | ||
expected: 'ignored' | ||
}); | ||
|
||
// Three args with custom Error | ||
assert.throws(() => { | ||
assert.fail(typeof 1, 'object', new TypeError('another custom message')); | ||
}, { | ||
name: 'TypeError', | ||
message: 'another custom message', | ||
operator: undefined, | ||
actual: undefined, | ||
expected: undefined, | ||
code: undefined | ||
}); | ||
|
||
// No third arg (but a fourth arg) | ||
assert.throws(() => { | ||
assert.fail('first', 'second', undefined, 'operator'); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
name: 'AssertionError [ERR_ASSERTION]', | ||
message: '\'first\' operator \'second\'', | ||
operator: 'operator', | ||
actual: 'first', | ||
expected: 'second' | ||
}); | ||
|
||
// The stackFrameFunction should exclude the foo frame | ||
assert.throws( | ||
function foo() { assert.fail('first', 'second', 'message', '!==', foo); }, | ||
(err) => !/^\s*at\sfoo\b/m.test(err.stack) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,40 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable prefer-common-expectserror */ | ||
|
||
const common = require('../common'); | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
// No args | ||
assert.throws( | ||
() => { assert.fail(); }, | ||
common.expectsError({ | ||
{ | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
name: 'AssertionError [ERR_ASSERTION]', | ||
message: 'Failed', | ||
operator: undefined, | ||
actual: undefined, | ||
expected: undefined | ||
}) | ||
} | ||
); | ||
|
||
// One arg = message | ||
common.expectsError(() => { | ||
assert.throws(() => { | ||
assert.fail('custom message'); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
name: 'AssertionError [ERR_ASSERTION]', | ||
message: 'custom message', | ||
operator: undefined, | ||
actual: undefined, | ||
expected: undefined | ||
}); | ||
|
||
// One arg = Error | ||
common.expectsError(() => { | ||
assert.throws(() => { | ||
assert.fail(new TypeError('custom message')); | ||
}, { | ||
type: TypeError, | ||
name: 'TypeError', | ||
message: 'custom message', | ||
operator: undefined, | ||
actual: undefined, | ||
expected: undefined | ||
}); | ||
|
||
// Two args only, operator defaults to '!=' | ||
common.expectsError(() => { | ||
assert.fail('first', 'second'); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
message: '\'first\' != \'second\'', | ||
operator: '!=', | ||
actual: 'first', | ||
expected: 'second' | ||
}); | ||
|
||
// Three args | ||
common.expectsError(() => { | ||
assert.fail('ignored', 'ignored', 'another custom message'); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
message: 'another custom message', | ||
operator: undefined, | ||
actual: 'ignored', | ||
expected: 'ignored' | ||
}); | ||
|
||
// Three args with custom Error | ||
common.expectsError(() => { | ||
assert.fail(typeof 1, 'object', new TypeError('another custom message')); | ||
}, { | ||
type: TypeError, | ||
message: 'another custom message', | ||
operator: undefined, | ||
actual: 'number', | ||
expected: 'object' | ||
}); | ||
|
||
// No third arg (but a fourth arg) | ||
common.expectsError(() => { | ||
assert.fail('first', 'second', undefined, 'operator'); | ||
}, { | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
message: '\'first\' operator \'second\'', | ||
operator: 'operator', | ||
actual: 'first', | ||
expected: 'second' | ||
}); | ||
|
||
// The stackFrameFunction should exclude the foo frame | ||
assert.throws( | ||
function foo() { assert.fail('first', 'second', 'message', '!==', foo); }, | ||
(err) => !/^\s*at\sfoo\b/m.test(err.stack) | ||
); |