To test multiple values that are returned using Jest, you can use the expect.arrayContaining
matcher. This matcher allows you to check that an expected array contains all the elements that are returned in the actual array.
For example, if you have a function that returns an array of numbers [1,2,3]
, you could test it like this:
test('returns an array of numbers', () => {
expect(myFunction()).toEqual(expect.arrayContaining([1,2,3]));
});
This will ensure that the array returned by myFunction
contains all the expected numbers, but it may contain additional values as well. If you want to ensure that the array only contains the expected values, you can add the expect.arrayContaining
matcher to an expect.not.arrayContaining
matcher:
test('returns only expected numbers', () => {
expect(myFunction()).toEqual(expect.not.arrayContaining([expect.anything()]));
expect(myFunction()).toEqual(expect.arrayContaining([1,2,3]));
});
This will ensure that the array returned by myFunction
does not contain any other values besides those specified in the expected array.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-09-19 11:00:00 +0000
Seen: 11 times
Last updated: Sep 07 '21
What is the process to incorporate multiple inheritance using OpenZeppelin upgradeable contracts?
Can the SequentialChain of Langchain handle multiple inputs?
How can a data-frame with multiple indices be created?
How can a single writer and multiple readers achieve concurrency with only one write?
On occasion, when executing a test suite for multiple classes, the act of mocking is omitted.
How can I search multiple databases on the local host?
How to connect or associate multiple files in Visual Studio Code on Ubuntu OS?
What is the method to combine multiple rows of a dataframe into a single value?
Can you whitelist multiple domains using command prompt for CORS Anywhere?