Reputation: 948
I have a function that makes an API
request and receives data in json
format.
async function getDataAboutWeather(url) {
return await new Promise(resolve => {
request(url, (err, res, body) => {
if (err) {
throw new Error(err);
};
const info = JSON.parse(body);
resolve(info);
});
});
};
I want to write a test for this function.
describe('getDataAboutWeather function', () => {
it('The request should success', () => {
const link = 'http://ip.jsontest.com';
expect(getDataAboutWeather(link)).to.eql({});
});
});
How to verify that my function works as it should?
Since at the moment I get an error.
AssertionError: expected {} to deeply equal {}
Upvotes: 1
Views: 43
Reputation: 1037
To test your function, you need to check if the API JSON is equal to the expected JSON. You can use the function below.
_.isEqual(object, other);
More information on this: How to determine equality for two JavaScript objects?
Upvotes: 1
Reputation: 322
getDataAboutWeather is an asynchronous function which means it returns a Promise. I would change the test to:
it('The request should success', async () => {
const link = 'http://ip.jsontest.com';
expect(await getDataAboutWeather(link)).to.eql({});
});
Upvotes: 2