1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | class 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 | }
|
30 | exports.FunctionMatchers = FunctionMatchers;
|
31 | function expectFunction(fn) {
|
32 | return new FunctionMatchers(fn);
|
33 | }
|
34 | exports.expectFunction = expectFunction;
|
35 | exports.default = expectFunction;
|
36 |
|
\ | No newline at end of file |