smartexpect/ts/namespaces/function.ts

29 lines
760 B
TypeScript

import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for function-specific matchers
*/
export class FunctionMatchers {
constructor(private assertion: Assertion<Function>) {}
toThrow(expectedError?: any) {
return this.assertion.customAssertion(
(value) => {
let threw = false;
try {
(value as Function)();
} catch (e: any) {
threw = true;
if (expectedError) {
if (typeof expectedError === 'function') {
return e instanceof expectedError;
}
return e === expectedError;
}
}
return threw;
},
`Expected function to throw${expectedError ? ` ${expectedError}` : ''}`
);
}
}