diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2017-12-09 20:20:07 -0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-01-16 19:57:35 +0100 |
commit | f76ef504326f8a37cdb0d3dae705239d685abffc (patch) | |
tree | 20e43cd73b2a0b4ef88a0fe7c32e4c3c42f36c48 /test | |
parent | 27925c4086d52e3858cbfabf0e55f52c20bce8ac (diff) | |
download | android-node-v8-f76ef504326f8a37cdb0d3dae705239d685abffc.tar.gz android-node-v8-f76ef504326f8a37cdb0d3dae705239d685abffc.tar.bz2 android-node-v8-f76ef504326f8a37cdb0d3dae705239d685abffc.zip |
assert: improve simple assert
This improves the error message in simple asserts by using the
real call information instead of the already evaluated part.
PR-URL: https://github.com/nodejs/node/pull/17581
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ron Korving <ron@ronkorving.nl>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-assert.js | 118 |
1 files changed, 115 insertions, 3 deletions
diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 4cab6c691a..c1b32c1e5c 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -25,6 +25,7 @@ const common = require('../common'); const assert = require('assert'); +const { EOL } = require('os'); const a = assert; function makeBlock(f) { @@ -753,14 +754,24 @@ common.expectsError( assert.equal(Object.keys(assert).length, Object.keys(a).length); /* eslint-enable no-restricted-properties */ assert(7); + + // Test setting the limit to zero and that assert.strict works properly. + const tmpLimit = Error.stackTraceLimit; + Error.stackTraceLimit = 0; common.expectsError( - () => assert(), + () => { + assert.ok( + typeof 123 === 'string' + ); + }, { code: 'ERR_ASSERTION', type: assert.AssertionError, - message: 'undefined == true' + message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + + `assert.ok(typeof 123 === 'string')${EOL}` } ); + Error.stackTraceLimit = tmpLimit; } common.expectsError( @@ -768,7 +779,108 @@ common.expectsError( { code: 'ERR_ASSERTION', type: assert.AssertionError, - message: 'null == true' + message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + + `assert.ok(null)${EOL}` + } +); +common.expectsError( + () => assert(typeof 123 === 'string'), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + + `assert(typeof 123 === 'string')${EOL}` + } +); + +{ + // Test caching + const fs = process.binding('fs'); + const tmp = fs.close; + fs.close = common.mustCall(tmp, 1); + function throwErr() { + // eslint-disable-next-line prefer-assert-methods + assert( + (Buffer.from('test') instanceof Error) + ); + } + common.expectsError( + () => throwErr(), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + + `assert(Buffer.from('test') instanceof Error)${EOL}` + } + ); + common.expectsError( + () => throwErr(), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + + `assert(Buffer.from('test') instanceof Error)${EOL}` + } + ); + fs.close = tmp; +} + +common.expectsError( + () => { + a( + (() => 'string')() + // eslint-disable-next-line + === + 123 instanceof + Buffer + ); + }, + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + + `assert((() => 'string')()${EOL}` + + ` // eslint-disable-next-line${EOL}` + + ` ===${EOL}` + + ` 123 instanceof${EOL}` + + ` Buffer)${EOL}` + } +); + +common.expectsError( + () => assert(null, undefined), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + + `assert(null, undefined)${EOL}` + } +); + +common.expectsError( + () => assert.ok.apply(null, [0]), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: '0 == true' + } +); + +common.expectsError( + () => assert.ok.call(null, 0), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: '0 == true' + } +); + +common.expectsError( + () => assert.ok.call(null, 0, 'test'), + { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: 'test' } ); |