'How to check data in Jest
I'm using the Jest package to test my Node application server-side. I need to check the value and type of the result from my request to the server.
Is it possible to expect the result to be an object like this?
And how to check
string[]?
The result is from the request:
{
"definitions": [
"One who subsists on charity; a dependent. South."
],
"pos": "n.",
"word": "ELEEMOSYNARY"
}
My check to be:
definitions - string[]
pos - "n."
word - string
My code is :
** expect(typeof (res.body.definitions)).toEqual('string'); ???? **
expect(res.body.pos).toEqual('n.');
expect(typeof (res.body.word)).toEqual('string');
Solution 1:[1]
You can check the length of definitions or check if Array.isArray() is true, and also the string at position zero like this:
// Either this for the array check
expect(res.body.definitions.length).toEqual(1);
// Or this for the array check
expect(Array.isArray(res.body.definitions)).toStrictEqual(true);
// And this for the string check of the array
expect(res.body.definitions[0]).toEqual("One who subsists on charity; a dependent. South.");
I'm assuming you have this api request/response mocked so you can rely on the value of definitions[0] to check the string.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | I'm Joe Too |
