issue with Jest unit test case ,in scenario of mockrejectedvalue

Viewed 14

I'm trying to write the unit test case using jest for the following function

 public async getArtifacts(): Promise<any> {
        let artifacts: Artifact[] = [];
        return new Promise(async(resolve,reject) => {
            try{            
                let files = await fspromise.readdir(`${envConfig.artifacts_path}`); 
                for(const filename of files){
                    if(path.extname(filename).toLowerCase() == '.zip'){
                        let file_path = path.join(`${envConfig.artifacts_container_path}`,filename);
                        let artifact = new Artifact(filename,file_path);   
                        artifact =  await this.processArtifact(artifact);                
                        artifacts.push(artifact);                    
                    }
                };
                
                resolve(artifacts);         
            }
            catch(error){
                LoggerWrapper.error(`Error while accessing ZIP artifacts from the source path`, error);
                throw error;
            }
        });
    }    

I was trying to write a test case in which the processArtifact would be failing and throw and error and the same would be handled by catch block of getArtifacts,the unit test that i wrote is as follows

test('test get artifacts with exception', async () => {
    const mockGetRestClient = jest.fn();
    artifactService.processArtifact = mockGetRestClient;
     mockGetRestClient.mockRejectedValue(new InternalServerException());
    try {
      const response = await artifactService.getArtifacts();
      expect(artifactService.processArtifact).toHaveBeenCalled();
     // expect(response).toEqual(new InternalServerException);
      expect(artifactService.getArtifacts).toThrow('Error while accessing ZIP artifacts from the source path');
    }
    catch (e: any) {
        console.log(e)
     }
  }); 

but the unit test case fails ,the error that i get is

 Console

    console.log
      2022-09-16T06:29:02.154Z [error] : "Error while accessing ZIP artifacts from the source path"

      at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)

  ● test artifact service › test get artifacts with exception



      232 |     const mockGetRestClient = jest.fn();
      233 |     artifactService.processArtifact = mockGetRestClient;
    > 234 |      mockGetRestClient.mockRejectedValue(new InternalServerException());
          |                                          ^
      235 |     try {
      236 |       const response = await artifactService.getArtifacts();
      237 |       expect(artifactService.processArtifact).toHaveBeenCalled();

      at Object.<anonymous> (test/services/artifact-service.spec.ts:234:42)

  ● test artifact service › test get artifacts with exception

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      229 |   })
      230 |
    > 231 |   test('test get artifacts with exception', async () => {
          |   ^
      232 |     const mockGetRestClient = jest.fn();
      233 |     artifactService.processArtifact = mockGetRestClient;
      234 |      mockGetRestClient.mockRejectedValue(new InternalServerException());

      at test/services/artifact-service.spec.ts:231:3
      at Object.<anonymous> (test/services/artifact-service.spec.ts:20:10)

what is that i'm doing wrong i'm new to this jest unit testing ,not able to find exactly why its failing

PS - have tried adding the settimeout but still same issue.

0 Answers
Related