简体   繁体   中英

How to verify error message through jest mock for throw error inside catch block

I need help for jest mocking throw Error(JSON.stringify(studentErrorRes)); . I am not able to jest mock the catch block properly, I can do partial verification that error is thrown. I can do try block without any issue. Usually, when there is an error I do mock using .mockRejectedValue , it does not work in this case. Can someone please help me, how shall I mock this?

When I jest mock, I can successfully verify that error is getting thrown, but I am unable to verify what would be the exact error message? If I have more keys inside const studentErrorRes how shall I verify that in my mock that all my keys have proper values as expected? I hope I am not overthinking.

import { SNSEvent } from 'aws-lambda';

export const studentAPIGetHandler = async (event: SNSEvent): Promise<any> => {
  try {
    const studentID = event.studentInfo.studentID;
    const studentPortal = StudentService.getStudentInfo(studentID);
  } catch (error) {
    const studentErrorRes = {
      apiName: SuudentAPIName.Student_Message,
      myMessage: 'Unable to get student API response',
    };
    logger.error(studentErrorRes.myMessage, error);
    throw Error(JSON.stringify(studentErrorRes));
  }
};

Part of test case for catch block


it("Catch block test for error", async () => {


    try {
        await studentAPIGetHandler(event);
    } catch(e) {
        expect(e).toThrowError; 
// this verifies that error is thrown , but not exact error message

    }

    });

Here is the unit test solution:

index.ts :

import { StudentService } from './student.service';

type SNSEvent = any;
const SuudentAPIName = { Student_Message: 'Student_Message' };
const logger = console;

export const studentAPIGetHandler = async (event: SNSEvent): Promise<any> => {
  try {
    const studentID = event.studentInfo.studentID;
    const studentPortal = StudentService.getStudentInfo(studentID);
  } catch (error) {
    const studentErrorRes = {
      apiName: SuudentAPIName.Student_Message,
      myMessage: 'Unable to get student API response',
    };
    logger.error(studentErrorRes.myMessage, error);
    throw Error(JSON.stringify(studentErrorRes));
  }
};

student.service.ts :

export class StudentService {
  public static getStudentInfo(id) {
    return {} as any;
  }
}

index.test.ts :

import { studentAPIGetHandler } from './';
import { StudentService } from './student.service';

describe('59871106', () => {
  afterEach(() => {
    jest.restoreAllMocks();
  });
  it('should get student info', async () => {
    jest.spyOn(StudentService, 'getStudentInfo').mockReturnValueOnce({});
    const mEvent = { studentInfo: { studentID: 1 } };
    await studentAPIGetHandler(mEvent);
    expect(StudentService.getStudentInfo).toBeCalledWith(1);
  });

  it('should handle error', async () => {
    const mError = new Error('some error');
    jest.spyOn(StudentService, 'getStudentInfo').mockImplementationOnce(() => {
      throw mError;
    });
    jest.spyOn(console, 'error');
    const mEvent = { studentInfo: { studentID: 1 } };
    await expect(studentAPIGetHandler(mEvent)).rejects.toThrowError(
      JSON.stringify({ apiName: 'Student_Message', myMessage: 'Unable to get student API response' }),
    );
    expect(console.error).toBeCalledWith('Unable to get student API response', mError);
    expect(StudentService.getStudentInfo).toBeCalledWith(1);
  });
});

Unit test results with coverage report:

 PASS  src/stackoverflow/59871106/index.test.ts (12.591s)
  59871106
    ✓ should get student info (10ms)
    ✓ should handle error (12ms)

  console.error node_modules/jest-mock/build/index.js:860
    Unable to get student API response Error: some error
        at /Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/59871106/index.test.ts:16:20
        at Generator.next (<anonymous>)
        at /Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/59871106/index.test.ts:8:71
        at new Promise (<anonymous>)
        at Object.<anonymous>.__awaiter (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/59871106/index.test.ts:4:12)
        at Object.it (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/59871106/index.test.ts:15:40)
        at Object.asyncJestTest (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:102:37)
        at resolve (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:43:12)
        at new Promise (<anonymous>)
        at mapper (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
        at promise.then (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:73:41)
        at process._tickCallback (internal/process/next_tick.js:68:7)

--------------------|----------|----------|----------|----------|-------------------|
File                |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files           |    92.31 |      100 |    66.67 |    91.67 |                   |
 index.ts           |      100 |      100 |      100 |      100 |                   |
 student.service.ts |       50 |      100 |        0 |       50 |                 3 |
--------------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        14.685s

Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59871106

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM