Skip to content

Commit

Permalink
feat(assertion): Add .toBeUndefined() matcher (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion authored Sep 1, 2022
1 parent 48c43f0 commit 5c24139
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/lib/Assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ export class Assertion<T> {
});
}

/**
* Check if the value is `undefined`
*
* @returns the assertion instance
*/
public toBeUndefined(): this {
const error = new AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to be undefined`
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the value NOT to be undefined"
});

return this.execute({
assertWhen: this.actual === undefined,
error,
invertedError
});
}

/**
* Check if the value is `null`.
*
Expand Down
26 changes: 26 additions & 0 deletions test/lib/Assertion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,32 @@ describe("[Unit] Assertion.test.ts", () => {
});
});

describe(".toBeUndefined", () => {
context("when the value is undefined", () => {
it("returns the assertion instance", () => {
const test = new Assertion(undefined);

assert.deepStrictEqual(test.toBeUndefined(), test);
assert.throws(() => test.not.toBeUndefined(), {
message: "Expected the value NOT to be undefined",
name: AssertionError.name
});
});
});

context("when the value is NOT undefined", () => {
it("throws an assertion error", () => {
const test = new Assertion("foo");

assert.throws(() => test.toBeUndefined(), {
message: "Expected <foo> to be undefined",
name: AssertionError.name
});
assert.deepStrictEqual(test.not.toBeUndefined(), test);
});
});
});

describe(".toBeNull", () => {
context("when the value is null", () => {
it("returns the assertion instance", () => {
Expand Down

0 comments on commit 5c24139

Please sign in to comment.