From f279471858e648ec0b3e1aeeee78b89a6936c3eb Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Wed, 2 Dec 2020 02:25:37 +0100 Subject: [PATCH] Added assertion methods toHaveLength & toBeType (#1357) Co-authored-by: Liyas Thomas --- helpers/postwomanTesting.js | 42 ++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/helpers/postwomanTesting.js b/helpers/postwomanTesting.js index 13af1c975..d21247a78 100644 --- a/helpers/postwomanTesting.js +++ b/helpers/postwomanTesting.js @@ -89,10 +89,10 @@ class Expectation { } } _fail(message) { - this._testReports.push({ result: FAIL, message }) + return this._testReports.push({ result: FAIL, message }) } - _pass(message) { - this._testReports.push({ result: PASS }) + _pass() { + return this._testReports.push({ result: PASS }) } // TEST METHODS DEFINED BELOW // these are the usual methods that would follow expect(...) @@ -144,4 +144,40 @@ class Expectation { ? this._pass() : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 500-level status`)) } + toHaveLength(expectedLength) { + const actualLength = this.expectValue.length + return this._satisfies(actualLength, expectedLength) + ? this._pass() + : this._fail( + this._fmtNot( + `Expected length to be ${expectedLength} but actual length was ${actualLength}` + ) + ) + } + toBeType(expectedType) { + const actualType = typeof this.expectValue + if ( + [ + "string", + "boolean", + "number", + "object", + "undefined", + "bigint", + "symbol", + "function", + ].indexOf(expectedType) < 0 + ) { + return this._fail( + this._fmtNot( + `Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"` + ) + ) + } + return this._satisfies(actualType, expectedType) + ? this._pass() + : this._fail( + this._fmtNot(`Expected type to be "${expectedType}" but actual type was "${actualType}"`) + ) + } }