Background: I have a collection of functions that are used inside a template engine, where their output is always cast to strings. This means that when testing them, what I'm really interested in is their string representation.
Is there any way to “hook” into assert.*()
to modify the input before each assertion?
That would save me from explicitly casting the output each time:
it("should foo bar", () => {
assert.strictEqual("" + myFunction1(foo), "bar")
assert.strictEqual("" + myFunction1(baz), "fiz")
})
Altering assert functions isn't a great idea because any other call to assert.strictEqual
will lie to you, producing unexpected output. E.g. assert.strictEqual(true, true)
will return false
because ""+true
is not equal to true
.
A better alternative is to use a utility function to proxy all your assert.strictEqual
calls:
function assertWithCast (val1, val2) {
assert.strictEqual("" + val1, val2)
}
Then use it in your tests:
it("should foo bar", () => {
assertWithCast(myFunction1(foo), "bar")
assertWithCast(myFunction1(baz), "fiz")
})