guagay_wk
guagay_wk

Reputation: 28030

Easy way to enable/disable assert statements in node.js

I am using the assert module in node.js https://www.npmjs.com/package/assert

In C/C++, one can easily enable/disable assert statements with macros. Can the same thing be done for node.js and javascript in general?

Upvotes: 6

Views: 2979

Answers (2)

trincot
trincot

Reputation: 350272

Here are some solutions:

1. Test a global variable before every call

const devMode = false; // or var, if you use older javascript version

devMode && assert(...);

This works in many cases as desired, but not when you use assert with side-effects, like here:

devMode && assert(value++, 'Value should not have been 0');
devMode && assert(myfunc(), 'myfunc unexpectedly returned false');
devMode && assert.throws(function() {
    home = '/home';
    throw new Error("Wrong value");
}, Error);

In above constructs, the arguments of the assert calls are not evaluated when not in devMode. So value will not be incremented, myfunc will not be called, and home will not be set.

If you use such constructs, look at the second solution:

2. Create a mock assert

In this solution you create a library with the same interface as assert and include either the real library or your mock library:

Your library could look like this:

var assert = function (value, message) {
    return true;
}
assert.ok = assert;
assert.fail = assert;
assert.equal = assert;
assert.notEqual = assert;
assert.deepEqual = assert;
assert.notDeepEqual = assert;
assert.strictEqual = assert;
assert.notStrictEqual = assert;
assert.ifError = assert;
// assert.throws should only be used to confirm that certain code will produce
// an error, but it should not change any state outside the function's scope.
// This mock version will NOT execute any of the functions passed to it.
assert.throws = assert;
// For assert.doesNotThrow the same choice is made:
assert.doesNotThrow = assert;

With this solution the arguments passed are evaluated, but this might still not be enough in case you pass function references, as is the case with assert.throws and assert.doesNotThrow. So you might want to implement the mock version differently, so that it does execute the function passed as first and/or second argument, even in production mode.

3. Parse your code for production

With this solution your aim is to remove any references to assert from your code. In case you only use assert calls as statements, not as part of an expression, and if there are no side-effects you depend on (see above), then make sure these assert statements are each on a separate line, and use a simple grep to remove these lines.

I will not elaborate in this direction, but you could make this parser more intelligent, and (by using regular expressions) let it unwrap expressions from their assert context. Such parser would replace:

 assert(value++, 'Value should not have been 0')

with:

 value++

With this solution you need to repeat the parsing operation whenever you deploy your application in production.

Upvotes: 3

Bob van Luijt
Bob van Luijt

Reputation: 7588

Note that the package you are using is adding asserts as a module and are therefor polyfills.

You can simply overwrite -part- of the Object to disable the polyfill.

Example:
Let's say that you want to disable the deepEqual assert that currently looks like this:

assert.deepEqual = function deepEqual(actual, expected, message) {
  if (!_deepEqual(actual, expected)) {
    fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  }
};

You can simply overwrite it by doing something like:

assert.deepEqual = function deepEqual(actual, expected, message) {
  // disable
};

FYI:
Assert statements are in progress: http://wiki.ecmascript.org/doku.php?id=strawman:assert

Upvotes: 2

Related Questions