mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
assert: create internal/assert micro-module
For use in built-in modules that could benefit from `assert()` without having to load the entire module (unless an AssertionError actually occurs): lib/internal/assert.js. PR-URL: https://github.com/nodejs/node/pull/25956 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
This commit is contained in:
parent
64745c3ade
commit
5d609bb11c
9
lib/internal/assert.js
Normal file
9
lib/internal/assert.js
Normal file
@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
function assert(value, message) {
|
||||
if (!value) {
|
||||
require('assert')(value, message);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = assert;
|
1
node.gyp
1
node.gyp
@ -85,6 +85,7 @@
|
||||
'lib/vm.js',
|
||||
'lib/worker_threads.js',
|
||||
'lib/zlib.js',
|
||||
'lib/internal/assert.js',
|
||||
'lib/internal/assert/assertion_error.js',
|
||||
'lib/internal/async_hooks.js',
|
||||
'lib/internal/buffer.js',
|
||||
|
15
test/parallel/test-internal-assert.js
Normal file
15
test/parallel/test-internal-assert.js
Normal file
@ -0,0 +1,15 @@
|
||||
// Flags: --expose-internals
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
const internalAssert = require('internal/assert');
|
||||
|
||||
// Should not throw.
|
||||
internalAssert(true);
|
||||
internalAssert(true, 'fhqwhgads');
|
||||
|
||||
assert.throws(() => { internalAssert(false); }, assert.AssertionError);
|
||||
assert.throws(() => { internalAssert(false, 'fhqwhgads'); },
|
||||
{ code: 'ERR_ASSERTION', message: 'fhqwhgads' });
|
Loading…
Reference in New Issue
Block a user