Is there any way to assert that string
is one of array
elements with chai bdd? I couldn't find that in Chai API
expect("bar").to.be.one.of(["bar", "key"]);
Differences between expect and should First of all, notice that the expect require is just a reference to the expect function, whereas with the should require, the function is being executed. var chai = require('chai') const expect = chai. expect const should = chai.
You can use deep-equal-in-any-order plugin. Chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order. It works in a similar way as deep. equal but it doesn't check the order of the arrays (at any level of nested objects and arrays).
Chai assertion library is an external javascript library used to write assertions. Compared to what we write directly in javascript, this assertion library needs less time & effort and easy use. Chai assertion library is available by default in the Postman.
When you use deepEqual , Chai will go down the object hierarchy and compare every value of every property. Example: const a = {"a": "a"}; const b = {"a": "a"}; expect(a). to. equal(b); // false, as a refers to a different object than b expect(a).
Flip the check around:
expect(["bar", "key"]).to.include("bar");
Updated for 2021
The answer below prints out a better error message
expect(res.status).to.be.oneOf([400, 401]);
The error message clearly defines what the expected value is and the returned value.
E.g.
expect(200).to.be.oneOf([400, 401]);
Returns
AssertionError: expected 200 to be one of [ 400, 401 ]
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