assert-throws-with-message.ts 540 B

1234567891011121314
  1. /**
  2. * Helper method for creating tests which assert a given error message when the operation is attempted.
  3. */
  4. export function assertThrowsWithMessage(operation: () => Promise<any>, message: string | (() => string)) {
  5. return async () => {
  6. try {
  7. await operation();
  8. fail('Should have thrown');
  9. } catch (err) {
  10. const messageString = typeof message === 'function' ? message() : message;
  11. expect(err.message).toEqual(expect.stringContaining(messageString));
  12. }
  13. };
  14. }