In Chai, you can do stuff like the following:
expect({}).to.exist;
exist
is not a function call, but this still works in testing frameworks. The opposite (expect({}).to.not.exist
) causes tests to fail, but again, exist
is not a function call.
How do these assertions work without making me call a function? In fact, if I try to say expect({}).to.exist()
the test fails because exist
is not a function.
I figured it out (or at least, I figured out a method). Use JavaScript getters:
var throws = {
get a() {
throw new Error('a');
},
get b() {
throw new Error('b');
},
get c() {
throw new Error('c');
}
};
When doing throws.a
, throws.b
, or throws.c
, the appropriate error will be thrown.
From that point it is rather easy to build the assertions which are contained within Chai.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With