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

215
Views
Broma: para verificar si una instancia específica de clase llama a una función en JavaScript/Typescript

Estoy probando algunos middlewares express con broma.

 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", }) ); });

mi respuesta de error:

 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; } }

Me las arreglé para verificar si ErrorResponse con propiedad específica se llama en la next función, pero no garantiza que el objeto ErrorResponse contenga solo 3 propiedades ( statusCode , errCode , message ) si alguien cambia ErrorResponse para agregar una propiedad más, como details

Me gustaría hacer algo a continuación y garantizar que el objeto ErrorResponse contiene solo 3 propiedades ( 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" ) ); });

¿Puedo saber si hay una manera de hacerlo en broma?

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

0

Después de una mirada superficial a la documentación de broma, parece que expect.extend podría hacer lo que desea:

 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!