From 7744e2eb03f9676854d90caba0b879372fe816fe Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Tue, 17 Aug 2021 10:05:19 +0530 Subject: [PATCH] feat: add success test descriptions --- helpers/__tests__/postwomanTesting.spec.js | 163 +++++++++++---------- helpers/postwomanTesting.ts | 47 ++++-- 2 files changed, 120 insertions(+), 90 deletions(-) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index 1a19a908..e9eebc0f 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -1,9 +1,16 @@ -import runTestScriptWithVariables, { PASS, FAIL } from "../postwomanTesting" +import runTestScriptWithVariables from "../postwomanTesting" +/** + * @param {string} script + * @param {number} index + */ function getTestResult(script, index) { return runTestScriptWithVariables(script).testResults[index].result } +/** + * @param {string} script + */ function getErrors(script) { return runTestScriptWithVariables(script).errors } @@ -27,24 +34,24 @@ describe("Error handling", () => { describe("toBe", () => { test("test for numbers", () => { - expect(getTestResult("pw.expect(1).toBe(2)", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(1).toBe(2)", 0)).toEqual("FAIL") - expect(getTestResult("pw.expect(1).toBe(1)", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(1).toBe(1)", 0)).toEqual("PASS") }) test("test for strings", () => { - expect(getTestResult("pw.expect('hello').toBe('bonjour')", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect('hi').toBe('hi')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('hello').toBe('bonjour')", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect('hi').toBe('hi')", 0)).toEqual("PASS") }) test("test for negative assertion (.not.toBe)", () => { - expect(getTestResult("pw.expect(1).not.toBe(1)", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(1).not.toBe(2)", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(1).not.toBe(1)", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(1).not.toBe(2)", 0)).toEqual("PASS") expect(getTestResult("pw.expect('world').not.toBe('planet')", 0)).toEqual( - PASS + "PASS" ) expect(getTestResult("pw.expect('world').not.toBe('world')", 0)).toEqual( - FAIL + "FAIL" ) }) }) @@ -61,10 +68,10 @@ describe("toHaveProperty", () => { `pw.expect(${JSON.stringify(dummyResponse)}).toHaveProperty("id")`, 0 ) - ).toEqual(PASS) + ).toEqual("PASS") expect( getTestResult(`pw.expect(${dummyResponse.id}).toBe(843)`, 0) - ).toEqual(PASS) + ).toEqual("PASS") }) test("test for negative assertion (.not.toHaveProperty)", () => { expect( @@ -74,38 +81,38 @@ describe("toHaveProperty", () => { )}).not.toHaveProperty("type")`, 0 ) - ).toEqual(PASS) + ).toEqual("PASS") expect( getTestResult( `pw.expect(${JSON.stringify(dummyResponse)}).toHaveProperty("type")`, 0 ) - ).toEqual(FAIL) + ).toEqual("FAIL") }) }) describe("toBeLevel2xx", () => { test("test for numbers", () => { - expect(getTestResult("pw.expect(200).toBeLevel2xx()", 0)).toEqual(PASS) - expect(getTestResult("pw.expect(200).not.toBeLevel2xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(300).toBeLevel2xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(300).not.toBeLevel2xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(200).toBeLevel2xx()", 0)).toEqual("PASS") + expect(getTestResult("pw.expect(200).not.toBeLevel2xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(300).toBeLevel2xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(300).not.toBeLevel2xx()", 0)).toEqual("PASS") }) test("test for strings", () => { - expect(getTestResult("pw.expect('200').toBeLevel2xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('200').toBeLevel2xx()", 0)).toEqual("PASS") expect(getTestResult("pw.expect('200').not.toBeLevel2xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect('300').toBeLevel2xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('300').toBeLevel2xx()", 0)).toEqual("FAIL") expect(getTestResult("pw.expect('300').not.toBeLevel2xx()", 0)).toEqual( - PASS + "PASS" ) }) test("failed to parse to integer", () => { expect(getTestResult("pw.expect(undefined).toBeLevel2xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect(null).toBeLevel2xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel2xx()", 0)).toEqual("FAIL") expect(() => { runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel2xx()") }).toThrow() @@ -114,26 +121,26 @@ describe("toBeLevel2xx", () => { describe("toBeLevel3xx()", () => { test("test for numbers", () => { - expect(getTestResult("pw.expect(300).toBeLevel3xx()", 0)).toEqual(PASS) - expect(getTestResult("pw.expect(300).not.toBeLevel3xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(400).toBeLevel3xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(400).not.toBeLevel3xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(300).toBeLevel3xx()", 0)).toEqual("PASS") + expect(getTestResult("pw.expect(300).not.toBeLevel3xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(400).toBeLevel3xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(400).not.toBeLevel3xx()", 0)).toEqual("PASS") }) test("test for strings", () => { - expect(getTestResult("pw.expect('300').toBeLevel3xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('300').toBeLevel3xx()", 0)).toEqual("PASS") expect(getTestResult("pw.expect('300').not.toBeLevel3xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect('400').toBeLevel3xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('400').toBeLevel3xx()", 0)).toEqual("FAIL") expect(getTestResult("pw.expect('400').not.toBeLevel3xx()", 0)).toEqual( - PASS + "PASS" ) }) test("failed to parse to integer", () => { expect(getTestResult("pw.expect(undefined).toBeLevel3xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect(null).toBeLevel3xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel3xx()", 0)).toEqual("FAIL") expect(() => { runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel3xx()") }).toThrow() @@ -142,26 +149,26 @@ describe("toBeLevel3xx()", () => { describe("toBeLevel4xx()", () => { test("test for numbers", () => { - expect(getTestResult("pw.expect(400).toBeLevel4xx()", 0)).toEqual(PASS) - expect(getTestResult("pw.expect(400).not.toBeLevel4xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(500).toBeLevel4xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(500).not.toBeLevel4xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(400).toBeLevel4xx()", 0)).toEqual("PASS") + expect(getTestResult("pw.expect(400).not.toBeLevel4xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(500).toBeLevel4xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(500).not.toBeLevel4xx()", 0)).toEqual("PASS") }) test("test for strings", () => { - expect(getTestResult("pw.expect('400').toBeLevel4xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('400').toBeLevel4xx()", 0)).toEqual("PASS") expect(getTestResult("pw.expect('400').not.toBeLevel4xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect('500').toBeLevel4xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('500').toBeLevel4xx()", 0)).toEqual("FAIL") expect(getTestResult("pw.expect('500').not.toBeLevel4xx()", 0)).toEqual( - PASS + "PASS" ) }) test("failed to parse to integer", () => { expect(getTestResult("pw.expect(undefined).toBeLevel4xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect(null).toBeLevel4xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel4xx()", 0)).toEqual("FAIL") expect(() => { runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel4xx()") }).toThrow() @@ -170,26 +177,26 @@ describe("toBeLevel4xx()", () => { describe("toBeLevel5xx()", () => { test("test for numbers", () => { - expect(getTestResult("pw.expect(500).toBeLevel5xx()", 0)).toEqual(PASS) - expect(getTestResult("pw.expect(500).not.toBeLevel5xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(200).toBeLevel5xx()", 0)).toEqual(FAIL) - expect(getTestResult("pw.expect(200).not.toBeLevel5xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(500).toBeLevel5xx()", 0)).toEqual("PASS") + expect(getTestResult("pw.expect(500).not.toBeLevel5xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(200).toBeLevel5xx()", 0)).toEqual("FAIL") + expect(getTestResult("pw.expect(200).not.toBeLevel5xx()", 0)).toEqual("PASS") }) test("test for strings", () => { - expect(getTestResult("pw.expect('500').toBeLevel5xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('500').toBeLevel5xx()", 0)).toEqual("PASS") expect(getTestResult("pw.expect('500').not.toBeLevel5xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect('200').toBeLevel5xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('200').toBeLevel5xx()", 0)).toEqual("FAIL") expect(getTestResult("pw.expect('200').not.toBeLevel5xx()", 0)).toEqual( - PASS + "PASS" ) }) test("failed to parse to integer", () => { expect(getTestResult("pw.expect(undefined).toBeLevel5xx()", 0)).toEqual( - FAIL + "FAIL" ) - expect(getTestResult("pw.expect(null).toBeLevel5xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel5xx()", 0)).toEqual("FAIL") expect(() => { runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel5xx()") }).toThrow() @@ -198,87 +205,87 @@ describe("toBeLevel5xx()", () => { describe("toHaveLength()", () => { test("test for strings", () => { - expect(getTestResult("pw.expect('word').toHaveLength(4)", 0)).toEqual(PASS) - expect(getTestResult("pw.expect('word').toHaveLength(5)", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('word').toHaveLength(4)", 0)).toEqual("PASS") + expect(getTestResult("pw.expect('word').toHaveLength(5)", 0)).toEqual("FAIL") expect(getTestResult("pw.expect('word').not.toHaveLength(4)", 0)).toEqual( - FAIL + "FAIL" ) expect(getTestResult("pw.expect('word').not.toHaveLength(5)", 0)).toEqual( - PASS + "PASS" ) }) test("test for arrays", () => { const fruits = "['apples', 'bananas', 'oranges', 'grapes', 'strawberries', 'cherries']" expect(getTestResult(`pw.expect(${fruits}).toHaveLength(6)`, 0)).toEqual( - PASS + "PASS" ) expect(getTestResult(`pw.expect(${fruits}).toHaveLength(7)`, 0)).toEqual( - FAIL + "FAIL" ) expect( getTestResult(`pw.expect(${fruits}).not.toHaveLength(6)`, 0) - ).toEqual(FAIL) + ).toEqual("FAIL") expect( getTestResult(`pw.expect(${fruits}).not.toHaveLength(7)`, 0) - ).toEqual(PASS) + ).toEqual("PASS") }) }) describe("toBeType()", () => { test("test for positive assertion", () => { expect(getTestResult("pw.expect('random').toBeType('string')", 0)).toEqual( - PASS + "PASS" ) expect(getTestResult("pw.expect(true).toBeType('boolean')", 0)).toEqual( - PASS + "PASS" ) - expect(getTestResult("pw.expect(5).toBeType('number')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(5).toBeType('number')", 0)).toEqual("PASS") expect( getTestResult("pw.expect(new Date()).toBeType('object')", 0) - ).toEqual(PASS) + ).toEqual("PASS") expect( getTestResult("pw.expect(undefined).toBeType('undefined')", 0) - ).toEqual(PASS) + ).toEqual("PASS") expect( getTestResult("pw.expect(BigInt(123)).toBeType('bigint')", 0) - ).toEqual(PASS) + ).toEqual("PASS") expect( getTestResult("pw.expect(Symbol('test')).toBeType('symbol')", 0) - ).toEqual(PASS) + ).toEqual("PASS") expect( getTestResult("pw.expect(function() {}).toBeType('function')", 0) - ).toEqual(PASS) + ).toEqual("PASS") }) test("test for negative assertion", () => { expect( getTestResult("pw.expect('random').not.toBeType('string')", 0) - ).toEqual(FAIL) + ).toEqual("FAIL") expect(getTestResult("pw.expect(true).not.toBeType('boolean')", 0)).toEqual( - FAIL + "FAIL" ) expect(getTestResult("pw.expect(5).not.toBeType('number')", 0)).toEqual( - FAIL + "FAIL" ) expect( getTestResult("pw.expect(new Date()).not.toBeType('object')", 0) - ).toEqual(FAIL) + ).toEqual("FAIL") expect( getTestResult("pw.expect(undefined).not.toBeType('undefined')", 0) - ).toEqual(FAIL) + ).toEqual("FAIL") expect( getTestResult("pw.expect(BigInt(123)).not.toBeType('bigint')", 0) - ).toEqual(FAIL) + ).toEqual("FAIL") expect( getTestResult("pw.expect(Symbol('test')).not.toBeType('symbol')", 0) - ).toEqual(FAIL) + ).toEqual("FAIL") expect( getTestResult("pw.expect(function() {}).not.toBeType('function')", 0) - ).toEqual(FAIL) + ).toEqual("FAIL") }) test("unexpected type", () => { expect(getTestResult("pw.expect('random').toBeType('unknown')", 0)).toEqual( - FAIL + "FAIL" ) }) }) diff --git a/helpers/postwomanTesting.ts b/helpers/postwomanTesting.ts index c9520039..c51ef417 100644 --- a/helpers/postwomanTesting.ts +++ b/helpers/postwomanTesting.ts @@ -19,15 +19,18 @@ type TestReportEndBlock = { } type TestReportEntry = { + result: "PASS" | "FAIL" | "ERROR" + message: string styles: { icon: string } -} & ({ result: "PASS" } | { result: "FAIL" | "ERROR"; message: string }) +} + type TestReport = TestReportStartBlock | TestReportEntry | TestReportEndBlock export default function runTestScriptWithVariables( script: string, - variables: TestScriptVariables + variables?: TestScriptVariables ) { const pw = { _errors: [], @@ -130,15 +133,21 @@ class Expectation { }) } - _pass() { - return this._testReports.push({ result: "PASS", styles: styles.PASS }) + _pass(message: string) { + return this._testReports.push({ + result: "PASS", + message, + styles: styles.PASS, + }) } // TEST METHODS DEFINED BELOW // these are the usual methods that would follow expect(...) toBe(value: any) { return this._satisfies(value) - ? this._pass() + ? this._pass( + this._fmtNot(`${this.expectValue} do (not)match with ${value}`) + ) : this._fail( this._fmtNot(`Expected ${this.expectValue} (not)to be ${value}`) ) @@ -149,7 +158,9 @@ class Expectation { Object.prototype.hasOwnProperty.call(this.expectValue, value), true ) - ? this._pass() + ? this._pass( + this._fmtNot(`${this.expectValue} do (not)have property ${value}`) + ) : this._fail( this._fmtNot( `Expected object ${this.expectValue} to (not)have property ${value}` @@ -165,7 +176,9 @@ class Expectation { ) } return this._satisfies(code >= 200 && code < 300, true) - ? this._pass() + ? this._pass( + this._fmtNot(`${this.expectValue} is (not)a 200-level status`) + ) : this._fail( this._fmtNot( `Expected ${this.expectValue} to (not)be 200-level status` @@ -181,7 +194,9 @@ class Expectation { ) } return this._satisfies(code >= 300 && code < 400, true) - ? this._pass() + ? this._pass( + this._fmtNot(`${this.expectValue} is (not)a 300-level status`) + ) : this._fail( this._fmtNot( `Expected ${this.expectValue} to (not)be 300-level status` @@ -197,7 +212,9 @@ class Expectation { ) } return this._satisfies(code >= 400 && code < 500, true) - ? this._pass() + ? this._pass( + this._fmtNot(`${this.expectValue} is (not)a 400-level status`) + ) : this._fail( this._fmtNot( `Expected ${this.expectValue} to (not)be 400-level status` @@ -213,7 +230,9 @@ class Expectation { ) } return this._satisfies(code >= 500 && code < 600, true) - ? this._pass() + ? this._pass( + this._fmtNot(`${this.expectValue} is (not)a 500-level status`) + ) : this._fail( this._fmtNot( `Expected ${this.expectValue} to (not)be 500-level status` @@ -224,7 +243,11 @@ class Expectation { toHaveLength(expectedLength: number) { const actualLength = this.expectValue.length return this._satisfies(actualLength, expectedLength) - ? this._pass() + ? this._pass( + this._fmtNot( + `Length expectation of (not)being ${expectedLength} is kept` + ) + ) : this._fail( this._fmtNot( `Expected length to be ${expectedLength} but actual length was ${actualLength}` @@ -253,7 +276,7 @@ class Expectation { ) } return this._satisfies(actualType, expectedType) - ? this._pass() + ? this._pass(this._fmtNot(`The type is (not)"${expectedType}"`)) : this._fail( this._fmtNot( `Expected type to be "${expectedType}" but actual type was "${actualType}"`