Jest unit test case not working as expected for rejected promise of inner function

Viewed 13

I have a function as follows

public async getfacts(): Promise<any> {
        let facts: fact[] = [];
        return new Promise(async(resolve,reject) => {
            try{            
                let files = await fspromise.readdir(`${envConfig.facts_path}`); 
                for(const filename of files){
                    if(path.extname(filename).toLowerCase() == '.zip'){
                        let file_path = path.join(`${envConfig.facts_path}`,filename);
                        let artfact = new ArtFact(filename,file_path);   
                        artfact =  await this.processArtfact(artfact);                
                        facts.push(artfact);                    
                    }
                };
                
                resolve(facts);         
            }
            catch(error){
                LoggerWrapper.error(`Error while accessing ZIP facts from the source path`, error);
                throw error;
            }
        });
    }

for this I wrote the test case for negative scenario where in the inner function processArtfact rejects or throws error , the test case is as below

test('test get artfacts rejection', async () => {
    const mockGetRestClient = jest.fn();
    artService.processArtfact = mockGetRestClient;
    mockGetRestClient.mockRejectedValue(new Error('Async error message'));
    try {
      const response = await artService.getfacts();
      expect(artService.processArtfact).toHaveBeenCalled();
    }
    catch (e) { 
      const a = e ;
      expect(e.message).toBe('Async error message')
    }
  });

but my test case never goes in the catch part,what is that i'm doing wrong is there a better to test the same scenario and my outer function also throws an error but i'm not able to catch it in jest unit test.

0 Answers
Related