Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Jest: to check if a function is called by a specific instance of class in JavaScript/Typescript

I am testing some express middlewares with jest.

it("should throw 400 error if request.body.id is null", () => {
    const req = { body: { id: null } } as any;
    const res = {} as any;
    const next = jest.fn();
    myMiddleware(req, res, next);

    expect(next).toBeCalledWith(expect.any(ErrorResponse));

    expect(next).toBeCalledWith(
        expect.objectContaining({
            statusCode: 400,
            errCode: "error-0123-2342",
            message: "Field id is missing",
        })
    );
});

my ErrorResponse:

export class ErrorResponse extends Error {
    public statusCode: number;

    public errCode: string;

    constructor(
        statusCode: number = 500,
        errCode: string = "error-123-1993",
        message: string = "Internal Server Error"
    ) {
        super(message);
        this.statusCode = statusCode;
        this.errCode = errCode;
    }
}

I manage to check if the ErrorResponse with specific property is called in the next function, but it doesn't guarantee the ErrorResponse Object contains only 3 properties (statusCode, errCode, message) if someone change the ErrorResponse to add One more property, such as details.

I would like to do something below and guarantee the ErrorResponse Object contains only 3 properties (statusCode, errCode, message).

it("should throw 400 error if request.body.id is null", () => {
    const req = { body: { id: null } } as any;
    const res = {} as any;
    const next = jest.fn();
    myMiddleware(req, res, next);

    expect(next).toBeCalledWith(
        new ErrorResponse(
            400,
            "error-3123-2332",
            "Field id is missing"
        )
    );
});

May I know if there is an way to do it in jest?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After a cursory look at the jest documentation, it seems that expect.extend might do what you want:

expect.extend({
  toBeErrorResponse(received) {
    if (received instanceof ErrorResponse &&
        Object.keys(received).length === 2)
      return {
        message: () => "expected no ErrorResponse",
        pass: true,
      };
    else
      return {
        message: () => "expected an ErrorResponse",
        pass: false,
      };
  }
});
test("my ErrorResponse", () => {
  const next = jest.fn();
  next(new ErrorResponse(400, "E", "M"));
  expect(next).toBeCalledWith(expect.toBeErrorResponse());
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!