assert-throws-with-message.ts 572 B

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