Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jasmine unit testing - testing for an undefined property of an object

I have the following statement

expect(A.["BAR"].name).toEqual("foo");  

which due to the fact my object A has the top level property "BAR" and bar has the value "foo" passes.

I'd like to test my structure to confirm a property "NONEXISTINGPROP" has not be defined. e.g.

expect(A.["NONEXISTINGPROP"].name).not.toBeDefined();  

However I seem to get

  "TypeError: A.[NONEXISTINGPROP] is undefined"  

in the jasmine test runner this is exactly what I want to confirm. Any idea why Jasmine is crying. I was hoping for it to pass this.

Thanks SO

like image 845
wmitchell Avatar asked Jan 10 '11 15:01

wmitchell


People also ask

How do you know if Jasmine is undefined?

ToBeUndefined() This matcher helps to check whether any variable is previously undefined or not, basically it works simply opposite to the previous matcher that is toBeDefined. In the following example, we will learn how to use this matcher.

What is done () in Jasmine?

Using the done() Method in Your Jasmine-driven Asynchronous JavaScript Tests. Jasmine. Async is an add-on library for Jasmine that provides additional functionality to do asynchronous testing. Modeled after Mocha's async test support, it brings the done() function to the Jasmine unit testing environment.

Is Jasmine a unit test?

Jasmine is one of the popular JavaScript unit testing frameworks which is capable of testing synchronous and asynchronous JavaScript code. It is used in BDD (behavior-driven development) programming which focuses more on the business value than on the technical details.


2 Answers

The answer seems to be ...

expect(A.NONEXISTINGPROP).not.toBeDefined();  

ie remove the name bit

like image 170
wmitchell Avatar answered Sep 23 '22 03:09

wmitchell


As a follow-up, Jasmine has had toBeUndefined since v1.3.0 (see here).

expect(A.NONEXISTINGPROP).toBeUndefined(); 
like image 34
jonathanGB Avatar answered Sep 22 '22 03:09

jonathanGB