Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 336
Pavel S. Avatar asked May 03 '13 10:05

Pavel S.


People also ask

What is Chai's assertion style to compare the contents of objects?

Important Concept: By default, all assertions in Chai are performing a strict equality comparison. Thus, asserting that an array of objects has a member object will cause those two objects to be compared strictly. Adding in the deep flag signals to the assertion to instead use deep equality for the comparison.

Should I assert vs vs expect?

Note expect and should uses chainable language to construct assertions, but they differ in the way an assertion is initially constructed. In the case of should , there are also some caveats and additional tools to overcome the caveats. var expect = require('chai').

Is Chai an assertion library?

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

What is chai expect?

expect , should = chai. should(); The expect interface provides a function as a starting point for chaining your language assertions. It works on node. js and in all browsers.


1 Answers

Alternatively if you want to check the value along with the property you can do

expect(myObject).to.have.property('b', myValue);

You won't need the plugin for this check.

like image 85
slay Avatar answered Sep 29 '22 02:09

slay