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 in Nodejs
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, anAssertionError
is thrown with amessage
property set equal to the value of themessage
parameter. If themessage
parameter isundefined
, a default error message is assigned. If themessage
parameter is an instance of anError
then it will be thrown instead of theAssertionError
. If no arguments are passed in at allmessage
will be set to the string:'No value argument passed to assert.ok()'
.
Assert in Grida source code
I searched for assert across grida repository, and found 94 hits.
I chose this code snippet from grida-react-canvas.
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
About me:
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.
I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com
My Github — https://github.com/ramu-narasinga
My website — https://ramunarasinga.com
My Youtube channel — https://www.youtube.com/@thinkthroo
Learning platform — https://thinkthroo.com
Codebase Architecture — https://app.thinkthroo.com/architecture
Best practices — https://app.thinkthroo.com/best-practices
Production-grade projects — https://app.thinkthroo.com/production-grade-projects
Top comments (0)