Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai: Assert a variable does not exist

How do I use Chai to verify that some variable does not exist?

That is to say, my js doesn't have a statement var never_declared, and I want to verify that there isn't any such variable.

I have tried all of following, but Karma keeps complaining "Can't find variable: never_declared". Which is exactly what I'm trying to test.

should.not.exist(never_declared);
expect(never_declared).to.not.exist;
expect(never_declared).to.not.exist;
expect(never_declared).to.be.undefined;
expect(never_declared).to.be.an('undefined');
assert.isNotObject(never_declared);
assert.isUndefined(never_declared);
like image 682
chernevik Avatar asked Sep 03 '26 23:09

chernevik


1 Answers

If Karma has a problem with it you can use another valid Chai form like:

expect(typeof never_declared).to.eq('undefined');

or

expect(typeof never_declared === "undefined").to.be.true;
like image 111
ebpa Avatar answered Sep 05 '26 16:09

ebpa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!