I am facing an issue with creating unit tests using jest.
exportMyData.js
const createJobTrasferSetArray = async () => {
const data = exportData();
const jobTransferSet = [];
try {
data.forEach((element) => {
const JobPathArray = element.JobPath.split(/[,\r\n]+/)
.filter((JobPathElement) => JobPathElement !== '');
// Create an object
const jobTransferSetObj = {
coworkerID: element.CoworkerID,
jobPath: JobPathArray,
};
jobTransferSet.push(jobTransferSetObj);
});
return jobTransferSet;
} catch (e) {
console.error(`Couldn't create jobTransferSet Array: ${JSON.stringify(e)}`);
return e;
}
};
exportMyData.test.js
const exportData = require('./exportMyData');
describe('Read data from Excel and create formatted JSON output', () => {
//Test passed through
it('convert read data from Excel to an array', async () => {
mockedJobTransferSetRaw.mockReturnValue(jobTrasfarSetRaw);
const result = await exportData.createJobTrasferSetArray();
expect(result[0].coworkerID).toEqual(jobTransferSetArray[0].coworkerID);
expect(result[0].jobPath).toEqual(jobTransferSetArray[0].jobPath);
expect(result).not.toBeNull();
});
//Test fails
it('Error on convert to an array', async () => {
try {
const result = await exportData.createJobTrasferSetArray();
result.mockRejectedValue(new Error('something went wrong'));
} catch (error) {
expect(error.message).toEqual('Error: something went wrong');
}
});
});
I believe there is an error in my test case, but I am struggling to find the solution. Any help would be greatly appreciated!