Below is the unit test code I have written:
import { jest } from '@jest/globals';
import { getOnDemandEnvironmentInstances } from './../getOnDemandEnvironmentInstances';
import { MockClient } from './aws-sdk-client-mock';
describe('getOnDemandEnvironmentInstances', () => {
it('should return instances correctly when there are reservations and instances', async () => {
const mockClient = new MockClient;
const mockClientSend = jest.spyOn(mockClient, "send");
await getOnDemandEnvironmentInstances(mockClient);
expect(mockClientSend).toHaveBeenCalled();
});
I am getting below error:
TypeError: Cannot read properties of undefined (reading ‘reduce’)
13 | const Reservations = data.Reservations;
14 |
> 15 | const instanceList = Reservations.reduce((prev, current) => {
| ^
16 | return prev.concat(current.Instances);
17 | }, []);
I am trying to write unit tests for below code:
import { DescribeInstancesCommand } from "@aws-sdk/client-ec2";
export async function getOnDemandEnvironmentInstances(ec2Client) {
const describeInstancesCommand = new DescribeInstancesCommand({
Filters: [
{ Name: "tag:Phase", Values: ["OnDemandEnv"] },
{ Name: "tag:CbzStackTag", Values: ["cbzwebtier-test-ondemand"] },
{ Name: "tag:Platform", Values: ["Windows"] },
],
});
const data = await ec2Client.send(describeInstancesCommand);
const Reservations = data.Reservations;
const instanceList = Reservations.reduce((prev, current) => {
return prev.concat(current.Instances);
}, []);
return instanceList;
}
I was expecting the test run to just check whether mockClient(mock of ec2 client) has been called or not. But it is throwing some other errors based on actual code.
Can someone please guide what am I ding wrong or what needs to added/modified?
New contributor
Madhuri Verma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.