node/deps/npm/node_modules/json-stringify-nice/test/basic.js
Myles Borins 2e54524955
deps: update npm to 7.0.0-rc.3
PR-URL: https://github.com/nodejs/node/pull/35474
Reviewed-By: Ruy Adorno <ruyadorno@github.com>
Reviewed-By: Ujjwal Sharma <ryzokuken@disroot.org>
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2020-10-07 09:59:49 -04:00

69 lines
1.7 KiB
JavaScript

const t = require('tap')
const stringify = require('../')
t.test('basic sorting operation with default 2-space indent', t => {
t.plan(1)
t.matchSnapshot(stringify({
z: 1,
y: 'z',
obj: { a: {}, b: 'x' },
a: { b: 1, a: 2},
yy: 'a',
}), 'mix of objects and out of order keys')
})
t.test('throws same error on cycles as JSON.stringify', t => {
t.plan(1)
const cycle = { a: { b: { c: {} } } }
cycle.a.b.c = cycle.a
try {
JSON.stringify(cycle)
} catch (builtinEr) {
t.throws(() => stringify(cycle), builtinEr, 'same error as builtin')
}
})
t.test('spaces can be set', t => {
t.plan(5)
const obj = {
z: 1,
y: 'z',
obj: { a: {}, b: 'x' },
a: { b: 1, a: 2},
yy: 'a',
}
t.matchSnapshot(stringify(obj, 0, '\t'), 'tab')
t.matchSnapshot(stringify(obj, null, ' ^_^ '), 'space face')
t.matchSnapshot(stringify(obj, false, 3), 'the number 3')
t.matchSnapshot(stringify(obj, false, ''), 'empty string')
t.matchSnapshot(stringify(obj, false, false), 'boolean false')
})
t.test('replacer function is used', t => {
t.plan(1)
const obj = {
z: 1,
y: 'z',
obj: { a: {}, b: 'x' },
a: { b: 1, a: { nested: true} },
yy: 'a',
}
const replacer = (key, val) =>
key === 'a' ? { hello: '📞 yes', 'this is': '🐕', ...val }
: val
t.matchSnapshot(stringify(obj, replacer), 'replace a val with phone doggo')
})
t.test('sort keys explicitly with a preference list', t => {
t.plan(1)
const obj = {
z: 1,
y: 'z',
obj: { a: {}, b: 'x' },
a: { b: 1, a: { nested: true} },
yy: 'a',
}
const preference = ['obj', 'z', 'yy']
t.matchSnapshot(stringify(obj, preference), 'replace a val with preferences')
})