assert-throws-with-message.ts 610 B

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