DEV Community


console.assert has a point

Adam Crockett
I work at ForgeRock as Staff UI Engineer, I play with all sorts really. Lately WASM is my toy of interest.
・1 min read

You know about assert? It's that strange thing we don't talk about in JavaScript, historically, assert is a non standard feature which checks if a condition is true and if not, it throws. Modern browsers (and node module, "assert") actually contain console.assert, which I guess is a nice way of unwrapping an error in an if statement 🤷‍♂️, I'm sure gonna try it...

Now those of you who unit test, and I hope that's everyone (not always true), you understand the concept, perhaps console.assert could offer a way to write tests without a framework... Perhaps, but lets think about tests as they stand. In JavaScript, tests require much tooling to exercise code, they are kept in isolation and this means we can make suites, nice... It's not broken, don't fix it right?

Not exactly, it's important to know what or friends in the Rust community, are doing. In Rust, tests are wrote in the same file as the source, this makes testing feel not like an aside task, but a core part of the work.
Could it be that we could use the same way of working, adopt inline testing?

"But Adam, that's dumb! We would ship tests with our code..." Wait wait hang on, your neglecting the fact that our source is hardly ever shipped as is, we could be compiling and cutting out our tests, we could be using native assertion and some tool to just snip around this and cut it out of a deployment. Not such a heavy tooling approach?

Imagine, tests with the same source maps?

Cool idea Huh?

Discussion (0)