|
1 | 1 | import { AssertionError } from "assert";
|
2 | 2 |
|
3 | 3 | import { Assertion } from "./Assertion";
|
| 4 | +import { ErrorAssertion } from "./ErrorAssertion"; |
| 5 | +import { TypeFactory } from "./helpers/TypeFactories"; |
4 | 6 |
|
5 | 7 | export type AnyFunction = (...args: any[]) => any;
|
6 | 8 |
|
7 |
| -function functionExecution<T extends AnyFunction>(func: T): Error | undefined { |
8 |
| - try { |
9 |
| - func(); |
10 |
| - return undefined; |
11 |
| - } catch (error) { |
12 |
| - return error instanceof Error |
13 |
| - ? error |
14 |
| - : Error(`The function threw something that is not an Error: ${error}`); |
15 |
| - } |
| 9 | +interface Class<T> extends Function { |
| 10 | + prototype: T; |
16 | 11 | }
|
17 | 12 |
|
18 |
| -function assertion<E extends Error>(error: E | undefined , expectedError: E): boolean { |
19 |
| - return !!error |
20 |
| - && error?.name === expectedError.name |
21 |
| - && error?.message === expectedError.message; |
22 |
| -} |
| 13 | +const NoThrow = Symbol("NoThrow"); |
23 | 14 |
|
24 | 15 | export class FunctionAssertion<T extends AnyFunction> extends Assertion<T> {
|
25 | 16 |
|
26 | 17 | constructor(actual: T) {
|
27 | 18 | super(actual);
|
28 | 19 | }
|
29 | 20 |
|
| 21 | + private captureError(): unknown | typeof NoThrow { |
| 22 | + try { |
| 23 | + this.actual(); |
| 24 | + return NoThrow; |
| 25 | + } catch (error) { |
| 26 | + return error; |
| 27 | + } |
| 28 | + } |
| 29 | + |
30 | 30 | /**
|
31 |
| - * Check if the value throws an error. |
| 31 | + * Check if the function throws anything when called. |
32 | 32 | *
|
33 | 33 | * @returns the assertion instance
|
34 | 34 | */
|
35 |
| - public toThrowError<E extends Error>(expectedError?: E): this { |
36 |
| - const expected = expectedError || new Error(); |
37 |
| - const errorExecution = functionExecution(this.actual); |
| 35 | + public toThrow(): this { |
| 36 | + const captured = this.captureError(); |
38 | 37 | const error = new AssertionError({
|
39 |
| - actual: this.actual, |
40 |
| - expected, |
41 |
| - message: `Expected to throw error <${expected.name}> with message <'${expected.message || ""}'>` |
| 38 | + actual: captured, |
| 39 | + message: "Expected the function to throw when called" |
42 | 40 | });
|
43 | 41 | const invertedError = new AssertionError({
|
44 |
| - actual: this.actual, |
45 |
| - message: `Expected value to NOT throw error <${expected.name}> with message <'${expected.message || ""}'>` |
| 42 | + actual: captured, |
| 43 | + message: "Expected the function NOT to throw when called" |
46 | 44 | });
|
47 | 45 |
|
48 | 46 | return this.execute({
|
49 |
| - assertWhen: expectedError |
50 |
| - ? assertion(errorExecution, expected) |
51 |
| - : errorExecution instanceof Error, |
| 47 | + assertWhen: captured !== NoThrow, |
| 48 | + error, |
| 49 | + invertedError |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check if the function throws an {@link Error}. If the `ErrorType` is passed, |
| 55 | + * it also checks if the error is an instance of the specific type. |
| 56 | + * |
| 57 | + * @example |
| 58 | + * ``` |
| 59 | + * expect(throwingFunction) |
| 60 | + * .toThrowError() |
| 61 | + * .toHaveMessage("Oops! Something went wrong...") |
| 62 | + * |
| 63 | + * expect(myCustomFunction) |
| 64 | + * .toThrowError(MyCustomError) |
| 65 | + * .toHaveMessage("Something failed!"); |
| 66 | + * ``` |
| 67 | + * |
| 68 | + * @param ErrorType optional error type constructor to check the thrown error |
| 69 | + * against. If is not provided, it defaults to {@link Error} |
| 70 | + * @returns a new {@link ErrorAssertion} to assert over the error |
| 71 | + */ |
| 72 | + public toThrowError(): ErrorAssertion<Error>; |
| 73 | + public toThrowError<E extends Error>(ExpectedType: Class<E>): ErrorAssertion<E>; |
| 74 | + public toThrowError<E extends Error>(ExpectedType?: Class<E>): ErrorAssertion<E> { |
| 75 | + const captured = this.captureError(); |
| 76 | + |
| 77 | + if (captured === NoThrow) { |
| 78 | + throw new AssertionError({ |
| 79 | + actual: captured, |
| 80 | + message: "Expected the function to throw when called" |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + const ErrorType = ExpectedType ?? Error; |
| 85 | + const error = new AssertionError({ |
| 86 | + actual: captured, |
| 87 | + message: `Expected the function to throw an error instance of <${ErrorType.name}>` |
| 88 | + }); |
| 89 | + const invertedError = new AssertionError({ |
| 90 | + actual: captured, |
| 91 | + message: `Expected the function NOT to throw an error instance of <${ErrorType.name}>` |
| 92 | + }); |
| 93 | + |
| 94 | + this.execute({ |
| 95 | + assertWhen: captured instanceof ErrorType, |
| 96 | + error, |
| 97 | + invertedError |
| 98 | + }); |
| 99 | + |
| 100 | + return new ErrorAssertion(captured as E); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Check if the function throws a non-error value when called. Additionally, |
| 105 | + * you can pass a {@link TypeFactory} in the second argument so the returned |
| 106 | + * assertion is for the specific value type. Otherwise, a basic |
| 107 | + * {@link Assertion Assertion<unknown>} instance is returned. |
| 108 | + * |
| 109 | + * @example |
| 110 | + * ``` |
| 111 | + * expect(raiseValue) |
| 112 | + * .toThrowValue() |
| 113 | + * .toBeEqual(someValue); |
| 114 | + * |
| 115 | + * expect(raiseExitCode) |
| 116 | + * .toThrowValue(TypeFactories.Number) |
| 117 | + * .toBeNegative(); |
| 118 | + * ``` |
| 119 | + * |
| 120 | + * @param expected the value the function is expected to throw |
| 121 | + * @param typeFactory optional type factory to perform more specific |
| 122 | + * assertions over the thrown value |
| 123 | + * @returns the factory assertion or a basic assertion instance |
| 124 | + */ |
| 125 | + public toThrowValue<S, A extends Assertion<S>>(typeFactory?: TypeFactory<S, A>): A { |
| 126 | + const captured = this.captureError(); |
| 127 | + |
| 128 | + if (captured === NoThrow) { |
| 129 | + throw new AssertionError({ |
| 130 | + actual: captured, |
| 131 | + message: "Expected the function to throw a value" |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + const error = new AssertionError({ |
| 136 | + actual: captured, |
| 137 | + message: typeFactory |
| 138 | + ? `Expected the function to throw a value of type "${typeFactory.typeName}"` |
| 139 | + : "Expected the function to throw a value" |
| 140 | + }); |
| 141 | + const invertedError = new AssertionError({ |
| 142 | + actual: captured, |
| 143 | + message: typeFactory |
| 144 | + ? `Expected the function NOT to throw a value of type "${typeFactory.typeName}"` |
| 145 | + : "Expected the function NOT to throw a value" |
| 146 | + }); |
| 147 | + const isTypeMatch = typeFactory?.predicate(captured) ?? true; |
| 148 | + |
| 149 | + this.execute({ |
| 150 | + assertWhen: captured !== NoThrow && isTypeMatch, |
52 | 151 | error,
|
53 | 152 | invertedError
|
54 | 153 | });
|
| 154 | + |
| 155 | + return typeFactory?.predicate(captured) |
| 156 | + ? new typeFactory.Factory(captured) |
| 157 | + : new Assertion(captured) as A; |
55 | 158 | }
|
56 | 159 | }
|
0 commit comments