UNPKG

1.16 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3class FunctionMatchers {
4 constructor(fn) {
5 this.fn = fn;
6 }
7 execute() {
8 try {
9 return { thrown: false, result: this.fn() };
10 }
11 catch (error) {
12 return { thrown: true, error };
13 }
14 }
15 match(handleFailure, handleSuccess) {
16 const report = this.execute();
17 return report.thrown ? handleFailure(report.error) : handleSuccess(report.result);
18 }
19 get throws() {
20 return this.match(x => expect(x), value => {
21 throw new Error(`Expecting the matched function to throw an error, but it returns a value: ${value}`);
22 });
23 }
24 get returns() {
25 return this.match(error => {
26 throw new Error(`Expecting the matched function to return a value, but it throws an error: ${error}`);
27 }, x => expect(x));
28 }
29}
30exports.FunctionMatchers = FunctionMatchers;
31function expectFunction(fn) {
32 return new FunctionMatchers(fn);
33}
34exports.expectFunction = expectFunction;
35exports.default = expectFunction;
36//# sourceMappingURL=expect-function.js.map
\No newline at end of file