message
message

Reputation: 4603

Chai expect to be one of array elements

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"]);

Upvotes: 19

Views: 15480

Answers (2)

PathToLife
PathToLife

Reputation: 1183

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 ]

Upvotes: 11

Adrian Lynch
Adrian Lynch

Reputation: 8494

Flip the check around:

expect(["bar", "key"]).to.include("bar");

Upvotes: 18

Related Questions