From a3a377492643a840675291dcdd3719169c4cd64a Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 15 Dec 2017 14:14:27 -0800 Subject: [PATCH] test: nicer matchers output (#1605) This patch improves matchers to provide more information once some of them fail. --- utils/testrunner/Matchers.js | 12 +++++++++++- utils/testrunner/examples/fail.js | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/utils/testrunner/Matchers.js b/utils/testrunner/Matchers.js index e3edddff398..a58ddcf336b 100644 --- a/utils/testrunner/Matchers.js +++ b/utils/testrunner/Matchers.js @@ -43,7 +43,7 @@ class Expect { function applyMatcher(matcherName, matcher, inverse, value, ...args) { const result = matcher.call(null, value, ...args); - const message = result.message || `expect.${matcherName} failed`; + const message = `expect.${matcherName} failed` + (result.message ? `: ${result.message}` : ''); console.assert(result.pass !== inverse, message); } } @@ -51,42 +51,52 @@ class Expect { const DefaultMatchers = { toBe: function(value, other, message) { + message = message || `${value} == ${other}`; return { pass: value === other, message }; }, toBeFalsy: function(value, message) { + message = message || `${value}`; return { pass: !value, message }; }, toBeTruthy: function(value, message) { + message = message || `${value}`; return { pass: !!value, message }; }, toBeGreaterThan: function(value, other, message) { + message = message || `${value} > ${other}`; return { pass: value > other, message }; }, toBeGreaterThanOrEqual: function(value, other, message) { + message = message || `${value} >= ${other}`; return { pass: value >= other, message }; }, toBeLessThan: function(value, other, message) { + message = message || `${value} < ${other}`; return { pass: value < other, message }; }, toBeLessThanOrEqual: function(value, other, message) { + message = message || `${value} <= ${other}`; return { pass: value <= other, message }; }, toBeNull: function(value, message) { + message = message || `${value} == null`; return { pass: value === null, message }; }, toContain: function(value, other, message) { + message = message || `${value} ⊇ ${other}`; return { pass: value.includes(other), message }; }, toEqual: function(value, other, message) { + message = message || `${JSON.stringify(value)} ≈ ${JSON.stringify(other)}`; return { pass: JSON.stringify(value) === JSON.stringify(other), message }; }, diff --git a/utils/testrunner/examples/fail.js b/utils/testrunner/examples/fail.js index 4dddcd7a69f..bfb08888cbf 100644 --- a/utils/testrunner/examples/fail.js +++ b/utils/testrunner/examples/fail.js @@ -25,9 +25,27 @@ const {it, fit, xit} = runner; const {beforeAll, beforeEach, afterAll, afterEach} = runner; describe('testsuite', () => { - it('failure', async (state) => { + it('toBe', async (state) => { + expect(2 + 2).toBe(5); + }); + it('toBeFalsy', async (state) => { + expect(true).toBeFalsy(); + }); + it('toBeTruthy', async (state) => { expect(false).toBeTruthy(); }); + it('toBeGreaterThan', async (state) => { + expect(2).toBeGreaterThan(3); + }); + it('toBeNull', async (state) => { + expect(2).toBeNull(); + }); + it('toContain', async (state) => { + expect('asdf').toContain('e'); + }); + it('toEqual', async (state) => { + expect([1,2,3]).toEqual([1,2,3,4]); + }); }); runner.run();