Alex Rivera | Logout

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

Asked 2011-01-10T15:20:22.813
32

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

Edit
Report

1 Answer

83

The answer seems to be ...

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

ie remove the name bit

answered 2011-01-11T10:29:27.073

Your Answer