I am using apiSauce library for api calls and I am facing issue while writing jest for api class methods.
ChemicalListApi.ts
import { ApiResponse } from 'apisauce';import { RestService } from '../RestService';import { ApiConfig } from '../serviceConfig';import { GetChemicalsRequest } from '../../Types/Chemical/chemicalListTypes';const api = new RestService();export class ChemicalListApi { chemicalsListApi = (queryParams: GetChemicalsRequest): Promise<ApiResponse<any>> => { return api.apiCall(ApiConfig.CHEMICAL_LIST_DATA, { queryParams }); };}
__test __/RegistrationApi.test.ts
import { ChemicalListApi } from '../chemicalServices/ChemicalListApi';describe('EmailService', () => { let instance: ChemicalListApi; beforeEach(() => { instance = new ChemicalListApi(); }); it('should get all users as an array', async () => { expect(instance).toBeInstanceOf(ChemicalListApi); const allUsers = await instance.chemicalsListApi({}); expect(allUsers).toBeDefined(); });});
Result:
Test Suites: 1 passed, 1 totalTests: 1 passed, 1 totalSnapshots: 0 totalTime: 2.761s, estimated 3sRan all test suites matching /RegistrationApi.test.ts/i.Jest did not exit one second after the test run has completed.This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.✨ Done in 34.62s.
Problem :
chemicalsListApi method is async method , by writing test cases like above is triggering api call. so I am getting warning like : 'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles
to troubleshoot this issue.'