1

I have created this class:

export class ErrorList  {

  
    public HostelNotFoundError(details: ErrorDetail): FunctionalError {
        return new FunctionalError('1', 'Can\'t find this hostel', details);
    }

and in the service:

throw new ErrorList().HostelNotFoundError({} });

I would like to know if in Jest is possible to do something like:

rejects.toThrow(HostelNotFoundError);
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
  • 1
    isn't the actual thrown error `FunctionalError ` ? take a look at this https://stackoverflow.com/questions/46042613/how-to-test-type-of-thrown-exception-in-jest – Arjun Nayak Jun 22 '20 at 11:16

1 Answers1

2

HostelNotFoundError is not of type FunctionalError, it is a method of the ErrorList class that returns a new instance of FunctionalError. So in your unit-test you have to use:

rejects.toThrow(FunctionalError);

Note that you can use toMatch to validate e.g. the error-message or toMatchObject to validate the error's properties:

rejects.toMatch('Can\'t find this hostel');
eol
  • 23,236
  • 5
  • 46
  • 64