Skip to content

Commit 51a4e66

Browse files
committed
use json-stringify-safe in render.js to not throw on circular structures
1 parent 35ba8a3 commit 51a4e66

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

lib/render.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Stream = require('stream');
22
var json = typeof JSON === 'object' ? JSON : require('jsonify');
3+
var getSerialize = require('json-stringify-safe').getSerialize()
34

45
module.exports = Render;
56

@@ -70,8 +71,8 @@ function encodeResult (res, count) {
7071
output += outer + '---\n';
7172
output += inner + 'operator: ' + res.operator + '\n';
7273

73-
var ex = json.stringify(res.expected) || '';
74-
var ac = json.stringify(res.actual) || '';
74+
var ex = json.stringify(res.expected, getSerialize) || '';
75+
var ac = json.stringify(res.actual, getSerialize) || '';
7576

7677
if (Math.max(ex.length, ac.length) > 65) {
7778
output += inner + 'expected:\n' + inner + ' ' + ex + '\n';

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"dependencies" : {
1212
"jsonify" : "~0.0.0",
1313
"deep-equal" : "~0.0.0",
14-
"defined" : "~0.0.0"
14+
"defined" : "~0.0.0",
15+
"json-stringify-safe": "~4.0.0"
1516
},
1617
"devDependencies" : {
1718
"tap" : "~0.3.0",

test/circular-things.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var tape = require('../');
2+
var tap = require('tap');
3+
4+
tap.test('circular test', function (assert) {
5+
var test = tape.createHarness({ exit : false });
6+
var tc = tap.createConsumer();
7+
8+
var rows = [];
9+
tc.on('data', function (r) { rows.push(r) });
10+
tc.on('end', function () {
11+
// console.log("rs", rows)
12+
13+
// console.log("deepEqual?")
14+
15+
assert.same(rows, [
16+
"TAP version 13"
17+
, "circular"
18+
, { id: 1
19+
, ok: false
20+
, name: " should be equal"
21+
, operator: "equal"
22+
, expected: "{}"
23+
, actual: '{"circular":"[Circular]"}'
24+
}
25+
, "tests 1"
26+
, "pass 0"
27+
, "fail 1"
28+
])
29+
assert.end()
30+
})
31+
32+
// tt.equal(10, 10)
33+
// tt.end()
34+
35+
test.stream.pipe(tc);
36+
37+
test("circular", function (t) {
38+
t.plan(1)
39+
var circular = {}
40+
circular.circular = circular
41+
t.equal(circular, {})
42+
})
43+
})

0 commit comments

Comments
 (0)