all files / core/errors/ error-match-error.ts

100% Statements 16/16
100% Branches 31/31
100% Functions 2/2
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29     55×   55× 49×   40× 29×     11×                  
import { MatchError } from "../_errors";
 
export class ErrorMatchError extends MatchError {
 
  public constructor(actualError: Error, shouldMatch: boolean, expectedErrorType?: new (...args: Array<any>) => Error, expectedErrorMessage?: string) {
    super(actualError, expectedErrorMessage, "");
 
    if (expectedErrorType || expectedErrorMessage) {
      if (expectedErrorType === undefined || expectedErrorType === null || (expectedErrorMessage && actualError instanceof expectedErrorType && expectedErrorMessage !== actualError.message)) {
         this._message = `Expected an error with message "${expectedErrorMessage}" to ${!shouldMatch ? "not " : ""}have been thrown, but it was${!shouldMatch ? "" : "n't"}.`;
      }
      else if (expectedErrorMessage === undefined || (actualError && !(actualError instanceof expectedErrorType) && expectedErrorMessage === actualError.message)) {
         this._message = `Expected an error of type ${(<any>expectedErrorType)["name"]} to ${!shouldMatch ? "not " : ""}have been thrown, but ${shouldMatch ? (<any>actualError)["name"] + " was thrown instead" : "it was"}.`;
      }
      else {
         this._message = `Expected an error with message "${expectedErrorMessage}" and type ${(<any>expectedErrorType)["name"]} to ${!shouldMatch ? "not " : ""}have been thrown, but it was${!shouldMatch ? "" : "n't"}.`;
      }
    }
    else {
      if (shouldMatch) {
        this._message = `Expected an error to be thrown but no errors were thown.`;
      }
      else {
        this._message = `Expected an error not to be thrown but an error was thown.`;
      }
    }
  }
}