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

39,896

Solution 1

The answer seems to be ...

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

ie remove the name bit

Solution 2

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

expect(A.NONEXISTINGPROP).toBeUndefined();
Share:
39,896
wmitchell
Author by

wmitchell

Full Stack Engineer however I especially enjoy building RIA apps. Current experience is in E-Commerce but chances are if you can name it I've worked in your industry. Fallen in love with Angular JS, bower and bootstrap and have developed several enterprise level apps in Angular.

Updated on November 21, 2020

Comments

  • wmitchell
    wmitchell over 3 years

    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