Skip to content

Commit 0b4055e

Browse files
Trottaddaleax
authored andcommitted
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: #25956 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
1 parent 37d207c commit 0b4055e

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

lib/internal/assert.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
function assert(value, message) {
4+
if (!value) {
5+
require('assert')(value, message);
6+
}
7+
}
8+
9+
module.exports = assert;

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
'lib/vm.js',
8585
'lib/worker_threads.js',
8686
'lib/zlib.js',
87+
'lib/internal/assert.js',
8788
'lib/internal/assert/assertion_error.js',
8889
'lib/internal/async_hooks.js',
8990
'lib/internal/bash_completion.js',

test/parallel/test-internal-assert.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
6+
const assert = require('assert');
7+
const internalAssert = require('internal/assert');
8+
9+
// Should not throw.
10+
internalAssert(true);
11+
internalAssert(true, 'fhqwhgads');
12+
13+
assert.throws(() => { internalAssert(false); }, assert.AssertionError);
14+
assert.throws(() => { internalAssert(false, 'fhqwhgads'); },
15+
{ code: 'ERR_ASSERTION', message: 'fhqwhgads' });

0 commit comments

Comments
 (0)