Skip to content

Commit 5c24139

Browse files
authored
feat(assertion): Add .toBeUndefined() matcher (#64)
1 parent 48c43f0 commit 5c24139

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/lib/Assertion.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ export class Assertion<T> {
128128
});
129129
}
130130

131+
/**
132+
* Check if the value is `undefined`
133+
*
134+
* @returns the assertion instance
135+
*/
136+
public toBeUndefined(): this {
137+
const error = new AssertionError({
138+
actual: this.actual,
139+
message: `Expected <${this.actual}> to be undefined`
140+
});
141+
const invertedError = new AssertionError({
142+
actual: this.actual,
143+
message: "Expected the value NOT to be undefined"
144+
});
145+
146+
return this.execute({
147+
assertWhen: this.actual === undefined,
148+
error,
149+
invertedError
150+
});
151+
}
152+
131153
/**
132154
* Check if the value is `null`.
133155
*

test/lib/Assertion.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ describe("[Unit] Assertion.test.ts", () => {
164164
});
165165
});
166166

167+
describe(".toBeUndefined", () => {
168+
context("when the value is undefined", () => {
169+
it("returns the assertion instance", () => {
170+
const test = new Assertion(undefined);
171+
172+
assert.deepStrictEqual(test.toBeUndefined(), test);
173+
assert.throws(() => test.not.toBeUndefined(), {
174+
message: "Expected the value NOT to be undefined",
175+
name: AssertionError.name
176+
});
177+
});
178+
});
179+
180+
context("when the value is NOT undefined", () => {
181+
it("throws an assertion error", () => {
182+
const test = new Assertion("foo");
183+
184+
assert.throws(() => test.toBeUndefined(), {
185+
message: "Expected <foo> to be undefined",
186+
name: AssertionError.name
187+
});
188+
assert.deepStrictEqual(test.not.toBeUndefined(), test);
189+
});
190+
});
191+
});
192+
167193
describe(".toBeNull", () => {
168194
context("when the value is null", () => {
169195
it("returns the assertion instance", () => {

0 commit comments

Comments
 (0)