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

375
Views
Throw new Error with Object in TypeScript Jest

How do I throw a new Error with Object containing Error details in typeScript?

I am using jest for testing typescript code and I want to throw new Error as shown below:

mock.spyOn(AppUtils,'util_plane').mockImplementation(() => {
        throw new Error("Error Message",{status:403})
})

when I do this, the compiler complains saying:

Expected 0-1 arguments, but got 2.ts(2554)

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

0

Error constructor doesn't expect {status: number} to be passed to it. You can create and use custom error class:

class CustomError extends Error {
    status: number;

    constructor(message: string, { status }: { status: number }) {
        super(message);
        this.status = status
    }
}

// ...

throw new CustomError('Error Message', { status: 403 })

Or you could assign status property to the error after creation:

throw Object.assign(new Error('Error Message'), { status: 403 })
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!