Skip to content
Merged
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
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