assert in Nodejs and its usage in Grida source code
In this article, we will review the assert usage in Grida source code and I ran some experiments in a codesandbox repo to see this in action. Let’s get started.
assert is an alias of assert.ok(). Use this to test if value is truthy. It is equivalent to assert.equal(!!value, true, message).
If value is not truthy, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError. If no arguments are passed in at all message will be set to the string: 'No value argument passed to assert.ok()'.
I study large open-source projects and provide insights, give my repository a star.
assert( !(children && USER_CHILDREN), "NodeElement: children should not be provided when node has children");
If the condition here is not met, AssertionError is thrown in your console, but at this point I wanted to find out if this would stop the execution. For that reason, I setup a simple example on codesandbox and ran some experiments.
assert is going to stop your execution, this lets you add defensive mechanisms in place with a clear error message as to why the error has occured. Obviously you would set this error message yourself. Link to Codesandbox — https://codesandbox.io/p/devbox/kwz5ps
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.