From 0b4055e616bbf53bf4561527e67934aa401365f0 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 5 Feb 2019 21:05:24 -0800 Subject: [PATCH] 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 Reviewed-By: Minwoo Jung --- lib/internal/assert.js | 9 +++++++++ node.gyp | 1 + test/parallel/test-internal-assert.js | 15 +++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 lib/internal/assert.js create mode 100644 test/parallel/test-internal-assert.js diff --git a/lib/internal/assert.js b/lib/internal/assert.js new file mode 100644 index 00000000000000..b5eb88c93b61db --- /dev/null +++ b/lib/internal/assert.js @@ -0,0 +1,9 @@ +'use strict'; + +function assert(value, message) { + if (!value) { + require('assert')(value, message); + } +} + +module.exports = assert; diff --git a/node.gyp b/node.gyp index 1490f7910ec290..7fc9e85cc33741 100644 --- a/node.gyp +++ b/node.gyp @@ -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', diff --git a/test/parallel/test-internal-assert.js b/test/parallel/test-internal-assert.js new file mode 100644 index 00000000000000..b34657d3a67f84 --- /dev/null +++ b/test/parallel/test-internal-assert.js @@ -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' });