Skip to content

Commit

Permalink
feature(supertape) improve deep-equal support
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jan 15, 2019
1 parent 96409db commit f1b9ca3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
6 changes: 4 additions & 2 deletions .putout.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
28 changes: 23 additions & 5 deletions lib/supertape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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));
}

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 30 additions & 2 deletions test/supertape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
});
Expand All @@ -229,3 +256,4 @@ test('supertape: tape: deepEqual: diff: comment', async (t) => {
t.ok(comment.called, 'should call comment');
t.end();
});

0 comments on commit f1b9ca3

Please sign in to comment.