From f1b9ca32489b0d387f2858698b0fa790040d7e16 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 15 Jan 2019 15:23:50 +0200 Subject: [PATCH] feature(supertape) improve deep-equal support --- .putout.json | 6 ++++-- lib/supertape.js | 28 +++++++++++++++++++++++----- package.json | 1 + test/supertape.js | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/.putout.json b/.putout.json index 25d7fce..4775f9b 100644 --- a/.putout.json +++ b/.putout.json @@ -1,11 +1,13 @@ { "match": { "lib/supertape.js": { - "remove-console": false + "remove-console": false, + "convert-tape-to-supertape": false }, "test/supertape.js": { "remove-only": false, - "remove-skip": false + "remove-skip": false, + "convert-tape-to-supertape": false } } } diff --git a/lib/supertape.js b/lib/supertape.js index 1f2dd69..c86135c 100644 --- a/lib/supertape.js +++ b/lib/supertape.js @@ -4,11 +4,12 @@ const tryTo = require('try-to-tape'); const tape = tryTo(require('tape')); const diff = require('jest-diff'); const strip = require('strip-ansi'); +const deepEqual = require('deep-equal'); const wrap = (test) => async (str, fn) => { await test(str, async (t) => { t.equal = equal(t, t.equal); - t.deepEqual = deepEqual(t, t.deepEqual); + t.deepEqual = getDeepEqual(t); await fn(t); }); @@ -29,18 +30,35 @@ const equal = (t, equal) => (a, b, msg) =>{ showDiff(a, b, comment); }; -const deepEqual = (t, deepEqual) => (a, b, msg) =>{ +const getDeepEqual = (t) => (a, b, msg) =>{ const {comment} = t; - deepEqual(a, b, msg); - showDiff(a, b, comment); + const is = deepEqual(a, b, { + strict: true + }); + + if (is) { + t.pass(msg); + } else { + t.fail(msg); + showDiff(a, b, comment); + } }; module.exports = getNewTape(tape); function showDiff(a, b, log) { - const diffed = diff(a, b); + const diffed = diff(convert(a), convert(b)); if (diffed && strip(diffed) !== 'Compared values have no visual difference.') log(diffed); } +const { + parse, + stringify, +} = JSON; + +function convert(a) { + return parse(stringify(a)); +} + diff --git a/package.json b/package.json index 9297967..72e2b64 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "report": "nyc report --reporter=text-lcov | coveralls" }, "dependencies": { + "deep-equal": "^1.0.1", "jest-diff": "^23.6.0", "strip-ansi": "^5.0.0", "tape": "^4.9.2", diff --git a/test/supertape.js b/test/supertape.js index 6314616..dd9c7a1 100644 --- a/test/supertape.js +++ b/test/supertape.js @@ -186,15 +186,17 @@ test('supertape: tape: equal', async (t) => { test('supertape: tape: deepEqual: diff', async (t) => { const deepEqual = stub(); + const fail = stub(); const comment = stub(); const tape = (str, fn) => { fn({ comment, - deepEqual, + fail, }); }; mockRequire('tape', tape); + mockRequire('deep-equal', deepEqual); const supertape = reRequire('..'); await supertape('hello world', (t) => { @@ -203,15 +205,40 @@ test('supertape: tape: deepEqual: diff', async (t) => { stopAll(); - t.ok(deepEqual.calledWith({}, {hello: 'world'}, 'should equal'), 'should call tape'); + t.ok(deepEqual.calledWith({}, {hello: 'world'}, {strict: true}), 'should call tape'); + t.end(); +}); + +test('supertape: tape: deepEqual: diff: pass', async (t) => { + const pass = stub(); + const comment = stub(); + const tape = (str, fn) => { + fn({ + pass, + comment, + }); + }; + + mockRequire('tape', tape); + const supertape = reRequire('..'); + + await supertape('hello world', (t) => { + t.deepEqual({hello: 'world'}, {hello: 'world'}, 'should equal'); + }); + + stopAll(); + + t.ok(pass.calledWith('should equal'), 'should call tape'); t.end(); }); test('supertape: tape: deepEqual: diff: comment', async (t) => { const deepEqual = stub(); + const fail = stub(); const comment = stub(); const tape = (str, fn) => { fn({ + fail, comment, deepEqual, }); @@ -229,3 +256,4 @@ test('supertape: tape: deepEqual: diff: comment', async (t) => { t.ok(comment.called, 'should call comment'); t.end(); }); +