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 remended rules configuration that has a "warning level" configured for every rule. Warning level can be of 0, 1 or 2 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 remended rules configuration that has a "warning level" configured for every rule. Warning level can be of 0, 1 or 2 values.
1 Answer
Reset to default 7You 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)