Search code examples
javascriptunit-testingobjectassertionschai

How to check the value is in object with Chai?


Is it possible to test the value is contained within certain array with Chai assertion library?

Example:

var myObject = {
    a : 1,
    b : 2,
    c : 3
};
var myValue = 2;

I need to do something like this (however it is not working):

expect(myObject).values.to.contain(myValue);
//Error: Cannot read property 'to' of undefined

expect(myObject).to.contain(myValue);
//Error: Object #<Object> has no method 'indexOf'

How can I test this?


Solution

  • The chai fuzzy plugin has the functionality you needed.

    var myObject = {
        a : 1,
        b : 2,
        c : 3
    };
    var myValue = 2;
    myObject.should.containOneLike(myValue);