chai#AssertionError TypeScript Examples

The following examples show how to use chai#AssertionError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: errors.ts    From polar with MIT License 6 votes vote down vote up
export async function expectErrorAsync (
  f: () => Promise<any>,
  matchMessage?: string | RegExp
): Promise<void> {
  const noError = new AssertionError("Async error was expected but no error was thrown");
  const message = `Async error should have had message "${String(matchMessage)}" but got "`;
  const notExactMatch = new AssertionError(message);
  const notRegexpMatch = new AssertionError(message);
  try {
    await f();
  } catch (err) {
    if (matchMessage === undefined) {
      return;
    }
    if (typeof matchMessage === "string") {
      if (err.message !== matchMessage) {
        notExactMatch.message += `${String(err.message)}"`;
        throw notExactMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
      }
    } else {
      if (matchMessage.exec(err.message) === null) {
        notRegexpMatch.message += `${String(err.message)}"`;
        throw notRegexpMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
      }
    }
    return;
  }
  throw noError; // eslint-disable-line @typescript-eslint/no-throw-literal
}
Example #2
Source File: errors.ts    From polar with MIT License 6 votes vote down vote up
export function expectPolarError (
  f: () => any,
  errorDescriptor: ErrorDescriptor,
  matchMessage?: string | RegExp,
  errorMessage?: string
): void {
  try {
    const returnValue = f();
    if (returnValue instanceof Promise) {
      throw new Error("Please use expectPolarErrorAsync() when working with async code");
    }
  } catch (error) {
    assert.instanceOf(error, PolarError, errorMessage);
    assert.equal(error.number, errorDescriptor.number, errorMessage);
    assert.notMatch(
      error.message,
      /%[a-zA-Z][a-zA-Z0-9]*%/,
      "PolarError has an non-replaced variable tag"
    );

    if (typeof matchMessage === "string") {
      assert.include(error.message, matchMessage, errorMessage);
    } else if (matchMessage !== undefined) {
      assert.match(error.message, matchMessage, errorMessage);
    }

    return;
  }
  throw new AssertionError( // eslint-disable-line @typescript-eslint/no-throw-literal
    `PolarError number ${errorDescriptor.number} expected, but no Error was thrown`
  );
}
Example #3
Source File: errors.ts    From algo-builder with Apache License 2.0 6 votes vote down vote up
export async function expectErrorAsync(
	f: () => Promise<any>,
	matchMessage?: string | RegExp
): Promise<void> {
	const noError = new AssertionError("Async error was expected but no error was thrown");
	const message = `Async error should have had message "${String(matchMessage)}" but got "`;
	const notExactMatch = new AssertionError(message);
	const notRegexpMatch = new AssertionError(message);
	try {
		await f();
	} catch (err: any) {
		if (matchMessage === undefined) {
			return;
		}
		if (typeof matchMessage === "string") {
			if (err.message !== matchMessage) {
				notExactMatch.message += `${String(err.message)}"`;
				throw notExactMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
			}
		} else {
			if (matchMessage.exec(err.message) === null) {
				notRegexpMatch.message += `${String(err.message)}"`;
				throw notRegexpMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
			}
		}
		return;
	}
	throw noError; // eslint-disable-line @typescript-eslint/no-throw-literal
}
Example #4
Source File: errors.ts    From algo-builder with Apache License 2.0 6 votes vote down vote up
export function expectBuilderError(
	f: () => any,
	errorDescriptor: ErrorDescriptor,
	matchMessage?: string | RegExp,
	errorMessage?: string
): void {
	try {
		const returnValue = f();
		if (returnValue instanceof Promise) {
			throw new Error("Please use expectBuilderErrorAsync() when working with async code");
		}
	} catch (error) {
		assert.instanceOf(error, BuilderError, errorMessage);
		if (error instanceof BuilderError) {
			assert.equal(error.number, errorDescriptor.number, errorMessage);
			assert.notMatch(
				error.message,
				/%[a-zA-Z][a-zA-Z0-9]*%/,
				"BuilderError has an non-replaced variable tag"
			);

			if (typeof matchMessage === "string") {
				assert.include(error.message, matchMessage, errorMessage);
			} else if (matchMessage !== undefined) {
				assert.match(error.message, matchMessage, errorMessage);
			}
		}

		return;
	}
	throw new AssertionError( // eslint-disable-line @typescript-eslint/no-throw-literal
		`BuilderError number ${errorDescriptor.number} expected, but no Error was thrown` // eslint-disable-line @typescript-eslint/restrict-template-expressions
	);
}
Example #5
Source File: runtime-errors.ts    From algo-builder with Apache License 2.0 6 votes vote down vote up
export function expectRuntimeError(
	f: () => any,
	errorDescriptor: ErrorDescriptor,
	matchMessage?: string | RegExp,
	errorMessage?: string
): void {
	try {
		f();
	} catch (error) {
		assert.instanceOf(error, RuntimeError, errorMessage);
		if (error instanceof RuntimeError) {
			assert.equal(error.number, errorDescriptor.number, errorMessage);
			assert.notMatch(
				error.message,
				/%[a-zA-Z][a-zA-Z0-9]*%/,
				"RuntimeError has an non-replaced variable tag"
			);

			if (typeof matchMessage === "string") {
				assert.include(error.message, matchMessage, errorMessage);
			} else if (matchMessage !== undefined) {
				assert.match(error.message, matchMessage, errorMessage);
			}
		}

		return;
	}
	throw new AssertionError( // eslint-disable-line @typescript-eslint/no-throw-literal
		`RuntimeError number ${errorDescriptor.number} expected, but no Error was thrown`
	);
}
Example #6
Source File: errors.ts    From polar with MIT License 5 votes vote down vote up
export async function expectPolarErrorAsync (
  f: () => Promise<any>,
  errorDescriptor: ErrorDescriptor,
  matchMessage?: string | RegExp
): Promise<void> {
  const error = new AssertionError(
    `PolarError number ${errorDescriptor.number} expected, but no Error was thrown`
  );

  const match = String(matchMessage);
  const notExactMatch = new AssertionError(
    `PolarError was correct, but should have include "${match}" but got "`
  );

  const notRegexpMatch = new AssertionError(
    `PolarError was correct, but should have matched regex ${match} but got "`
  );

  try {
    await f();
  } catch (error) {
    assert.instanceOf(error, PolarError);
    assert.equal(error.number, errorDescriptor.number);
    assert.notMatch(
      error.message,
      /%[a-zA-Z][a-zA-Z0-9]*%/,
      "PolarError has an non-replaced variable tag"
    );

    if (matchMessage !== undefined) {
      if (typeof matchMessage === "string") {
        if (!error.message.includes(matchMessage)) {
          notExactMatch.message += `${String(error.message)}`;
          throw notExactMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
        }
      } else {
        if (matchMessage.exec(error.message) === null) {
          notRegexpMatch.message += `${String(error.message)}`;
          throw notRegexpMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
        }
      }
    }

    return;
  }

  throw error; // eslint-disable-line @typescript-eslint/no-throw-literal
}
Example #7
Source File: errors.ts    From algo-builder with Apache License 2.0 5 votes vote down vote up
/* eslint-disable sonarjs/cognitive-complexity */
export async function expectBuilderErrorAsync(
	f: () => Promise<any>,
	errorDescriptor: ErrorDescriptor,
	matchMessage?: string | RegExp
): Promise<void> {
	// We create the error here to capture the stack trace before the await.
	// This makes things easier, at least as long as we don't have async stack
	// traces. This may change in the near-ish future.
	const error = new AssertionError(
		`BuilderError number ${errorDescriptor.number} expected, but no Error was thrown` // eslint-disable-line @typescript-eslint/restrict-template-expressions
	);

	const match = String(matchMessage);
	const notExactMatch = new AssertionError(
		`BuilderError was correct, but should have include "${match}" but got "`
	);

	const notRegexpMatch = new AssertionError(
		`BuilderError was correct, but should have matched regex ${match} but got "`
	);

	try {
		await f();
	} catch (error) {
		assert.instanceOf(error, BuilderError);
		if (error instanceof BuilderError) {
			assert.equal(error.number, errorDescriptor.number);
			assert.notMatch(
				error.message,
				/%[a-zA-Z][a-zA-Z0-9]*%/,
				"BuilderError has an non-replaced variable tag"
			);

			if (matchMessage !== undefined) {
				if (typeof matchMessage === "string") {
					if (!error.message.includes(matchMessage)) {
						notExactMatch.message += `${String(error.message)}`;
						throw notExactMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
					}
				} else {
					if (matchMessage.exec(error.message) === null) {
						notRegexpMatch.message += `${String(error.message)}`;
						throw notRegexpMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
					}
				}
			}
		}

		return;
	}

	throw error; // eslint-disable-line @typescript-eslint/no-throw-literal
}
Example #8
Source File: runtime-errors.ts    From algo-builder with Apache License 2.0 5 votes vote down vote up
/* eslint-disable sonarjs/cognitive-complexity */
export async function expectRuntimeErrorAsync(
	f: () => Promise<any>,
	errorDescriptor: ErrorDescriptor,
	matchMessage?: string | RegExp
): Promise<void> {
	// We create the error here to capture the stack trace before the await.
	// This makes things easier, at least as long as we don't have async stack
	// traces. This may change in the near-ish future.
	const error = new AssertionError(
		`RuntimeError number ${errorDescriptor.number} expected, but no Error was thrown`
	);

	const match = String(matchMessage);
	const notExactMatch = new AssertionError(
		`RuntimeError was correct, but should have include "${match}" but got "`
	);

	const notRegexpMatch = new AssertionError(
		`RuntimeError was correct, but should have matched regex ${match} but got "`
	);

	try {
		await f();
	} catch (error) {
		if (!(error instanceof RuntimeError)) {
			assert.instanceOf(error, RuntimeError);
			return;
		}
		assert.equal(error.number, errorDescriptor.number);
		assert.notMatch(
			error.message,
			/%[a-zA-Z][a-zA-Z0-9]*%/,
			"RuntimeError has an non-replaced variable tag"
		);
		if (matchMessage !== undefined) {
			if (typeof matchMessage === "string") {
				if (!error.message.includes(matchMessage)) {
					notExactMatch.message += `${String(error.message)}`;
					throw notExactMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
				}
			} else {
				if (matchMessage.exec(error.message) === null) {
					notRegexpMatch.message += `${String(error.message)}`;
					throw notRegexpMatch; // eslint-disable-line @typescript-eslint/no-throw-literal
				}
			}
		}

		return;
	}

	throw error; // eslint-disable-line @typescript-eslint/no-throw-literal
}