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.
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With