How to use the oneOf function from chai
Find comprehensive JavaScript chai.oneOf code examples handpicked from public code repositorys.
chai.oneOf is a Chai assertion method that checks whether a given value is one of a set of expected values.
202 203 204 205 206 207 208 209 210 211
}); MessageBlock(RED); RED.node.get().emit('input', msg); return RED.node.get().await() .then(() => { assert.oneOf(RED.node.message().payload.content, ['Message 1', 'Message 2', 'Message 3']); }); }); /*it('should have even distribution for a randomly picked message', function() {
+ 3 other calls in file
How does chai.oneOf work?
chai.oneOf works by taking a single value to be tested and an array of expected values as arguments. It then checks whether the tested value is equal to at least one of the expected values using the === operator. If the tested value is equal to one of the expected values, the assertion passes. If the tested value is not equal to any of the expected values, the assertion fails and an error is thrown. chai.oneOf is a syntactic sugar for the Chai include assertion with a single value to be tested. It provides a more intuitive way to test whether a value is included in a set of expected values, particularly when dealing with large arrays or complex objects. chai.oneOf can be used in combination with other Chai assertion methods to test various conditions and expectations in JavaScript code, such as testing the results of functions, the properties of objects, or the elements of arrays.
Ai Example
1 2 3 4 5 6 7 8 9 10 11
const { expect } = require("chai"); describe("chai.oneOf", () => { it("should check if a value is one of the expected values", () => { const fruits = ["apple", "banana", "orange"]; expect("apple").to.be.oneOf(fruits); expect("pear").to.not.be.oneOf(fruits); expect(42).to.not.be.oneOf(fruits); }); });
In this example, we have an array of fruits and we use the chai.oneOf assertion method to check whether a value is included in the array. The first expectation checks whether the string 'apple' is one of the expected values in the fruits array, which should pass. The second expectation checks whether the string 'pear' is one of the expected values in the fruits array, which should fail. The third expectation checks whether the number 42 is one of the expected values in the fruits array, which should also fail. Note that chai.oneOf can also be used in conjunction with other Chai assertion methods, such as to.be.equal, to.be.above, to.be.below, and so on. For example: javascript Copy code
chai.expect is the most popular function in chai (8749 examples)