I strongly suspect this is to do with the api using an asynchronous call, but so far as I can see, I have 'awaits' in all the right places.
The unit test making the call:
const { expect } = require('@jest/globals');
const { getWorkerId } = require('../api');
describe("Workers api", () => {
test("it should return a status of 200", async () => {
let response = await getWorkerId(21882000)
expect(response.status).to.equal('200')
});
});
and the api itself:
const axios = require('axios');
const getWorkerId = async (id) => {
await axios.get(`http://localhost:8080/v1/workers/${id}`)
.then(function (response) {
console.log(response.status);
return response;
})
.catch(function (error) {
console.log('Error number: ', error.errno);
console.log('Error code: ', error.code);
return error;
})
};
module.exports = { getWorkerId };
The line 'console.log(response.status)' outputs '200' as expected and if I change the code to display 'response', sure enough there it is. Yet, my unit test's expect call comes back with: 'TypeError: Cannot read property 'equal' of undefined'.x