Skip to content

Commit

Permalink
assert: create internal/assert micro-module
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Trott authored and addaleax committed Feb 8, 2019
1 parent 37d207c commit 0b4055e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/internal/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

function assert(value, message) {
if (!value) {
require('assert')(value, message);
}
}

module.exports = assert;
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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/bash_completion.js',
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-internal-assert.js
Original file line number Diff line number Diff line change
@@ -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' });

0 comments on commit 0b4055e

Please sign in to comment.