admin管理员组文章数量:1024054
I am using the assert module in node.js
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?
I am using the assert module in node.js https://www.npmjs./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?
Share Improve this question asked Nov 28, 2015 at 10:37 guagay_wkguagay_wk 28.1k64 gold badges200 silver badges309 bronze badges2 Answers
Reset to default 3Here 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.
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/doku.php?id=strawman:assert
I am using the assert module in node.js
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?
I am using the assert module in node.js https://www.npmjs./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?
Share Improve this question asked Nov 28, 2015 at 10:37 guagay_wkguagay_wk 28.1k64 gold badges200 silver badges309 bronze badges2 Answers
Reset to default 3Here 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.
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/doku.php?id=strawman:assert
本文标签: javascriptEasy way to enabledisable assert statements in nodejsStack Overflow
版权声明:本文标题:javascript - Easy way to enabledisable assert statements in node.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745545386a2155368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论