Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add toThrowErrorMessage to function assertions
  • Loading branch information
Alex0jk committed Dec 28, 2021
commit 2c4f4e45c521b3eaca72b622c2146094eb5c042d
25 changes: 25 additions & 0 deletions src/lib/FunctionAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,29 @@ export class FunctionAssertion extends Assertion<Function> {
invertedError
});
}

/**
* Check if the value throws angit error with a specific message.
*
* @returns the assertion instance
*/
public toThrowErrorMessage(errorMessage: string): this {
const errorExecution = functionExecution(this.actual);
const assert = errorExecution && errorExecution.message === errorMessage;
const error = new AssertionError({
actual: this.actual,
expected: errorMessage,
message: `Expected to throw error with message <'${errorMessage}'>`
});
const invertedError = new AssertionError({
actual: this.actual,
message: `Expected value to NOT throw error with message <'${errorMessage}'>`
});

return this.execute({
assertWhen: !!assert,
error,
invertedError
});
}
}
57 changes: 48 additions & 9 deletions test/lib/FunctionAssertion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ class CustomError extends Error{
}
}

const testCustomErrorFunction = () => { throw new CustomError("Custom Error"); };
const testErrorFunction = () => { throw new TypeError("Error"); };
const testCustomErrorFunction = () => { throw new CustomError("Custom Error Message"); };
const testErrorFunction = () => { throw new TypeError("Error Message"); };
const testNormalFunction = () => { return 0; };

describe("[Unit] FunctionAssertion.test.ts", () => {
describe(".toThrowException", () => {
describe(".toThrowError", () => {
context("when the value throws an exception", () => {
it("returns the assertion instance", () => {
const test = new FunctionAssertion(testErrorFunction);

assert.deepStrictEqual(test.toThrowError(new TypeError("Error")), test);
assert.throws(() => test.not.toThrowError(new TypeError("Error")), {
message: "Expected value to NOT throw error <TypeError> with message <'Error'>",
assert.deepStrictEqual(test.toThrowError(new TypeError("Error Message")), test);
assert.throws(() => test.not.toThrowError(new TypeError("Error Message")), {
message: "Expected value to NOT throw error <TypeError> with message <'Error Message'>",
name: "AssertionError"
});
});
Expand All @@ -43,9 +43,9 @@ describe("[Unit] FunctionAssertion.test.ts", () => {
it("returns the assertion instance", () => {
const test = new FunctionAssertion(testCustomErrorFunction);

assert.deepStrictEqual(test.toThrowError(new CustomError("Custom Error")), test);
assert.throws(() => test.not.toThrowError(new CustomError("Custom Error")), {
message: "Expected value to NOT throw error <CustomError> with message <'Custom Error'>",
assert.deepStrictEqual(test.toThrowError(new CustomError("Custom Error Message")), test);
assert.throws(() => test.not.toThrowError(new CustomError("Custom Error Message")), {
message: "Expected value to NOT throw error <CustomError> with message <'Custom Error Message'>",
name: "AssertionError"
});
});
Expand Down Expand Up @@ -99,4 +99,43 @@ describe("[Unit] FunctionAssertion.test.ts", () => {
});
});
});

describe(".toThrowErrorMessage", () => {
context("when the value throws an error, with a specific message", () => {
it("returns the assertion instance", () => {
const test = new FunctionAssertion(testErrorFunction);

assert.deepStrictEqual(test.toThrowErrorMessage("Error Message"), test);
assert.throws(() => test.not.toThrowErrorMessage("Error Message"), {
message: "Expected value to NOT throw error with message <'Error Message'>",
name: "AssertionError"
});
});
});


context("when the value does NOT throw error", () => {
it("throws an assertion error", () => {
const test = new FunctionAssertion(testNormalFunction);

assert.throws(() => test.toThrowErrorMessage("Error"), {
message: "Expected to throw error with message <'Error'>",
name: "AssertionError"
});
assert.deepStrictEqual(test.not.toThrowErrorMessage("Error"), test);
});
});

context("when the value does throw error, but with a different message", () => {
it("throws an assertion error", () => {
const test = new FunctionAssertion(testCustomErrorFunction);

assert.throws(() => test.toThrowErrorMessage("Custom Error Message Diff"), {
message: "Expected to throw error with message <'Custom Error Message Diff'>",
name: "AssertionError"
});
assert.deepStrictEqual(test.not.toThrowErrorMessage("Custom Error Message Diff"), test);
});
});
});
});