Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect deep property to have any of multiple values

In Chai assertion library, we can assert a deep property to exist and have a value:

expect(obj).to.have.deep.property("field1.field2", 1);

But, what if we need to assert this property to have one of multiple values? In this case, the test should pass if obj has a field1.field2 property that has 0 or 1 or 2 value.


FYI, I need this to check that a ESLint plugin ships with a recommended rules configuration that has a "warning level" configured for every rule. Warning level can be of 0, 1 or 2 values.

like image 269
alecxe Avatar asked Jun 29 '16 17:06

alecxe


People also ask

Should I assert vs vs expect?

The assert and expect interfaces do not modify Object. prototype , whereas should does. So they are a better choice in an environment where you cannot or do not want to change Object.

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.

Which assertion styles are considered BDD style in testing?

The BDD style comes in two flavors: expect and should . Both use the same chainable language to construct assertions, but they differ in the way an assertion is initially constructed.

What is chai in JavaScript?

Chai is an assertion library that is mostly used alongside Mocha. It can be used both as a BDD / TDD assertion library for NodeJS and can be paired with any JavaScript testing framework. It has several interfaces that a developer can choose from and looks much like writing tests in English sentences.


1 Answers

You can use .oneOf():

expect(obj).to.have.deep.property('field1.field2').that.is.oneOf([ 0, 1, 2 ])

Or .within():

expect(obj).to.have.deep.property('field1.field2').that.is.within(0, 2)
like image 134
robertklep Avatar answered Nov 14 '22 16:11

robertklep