From 832ade18b61200030df9afae5d3ac42049fb7114 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Thu, 16 Nov 2023 01:31:53 -0600 Subject: [PATCH] feat: provide better error trace --- src/utilities/stringify.ts | 3 +- test/roarr/utilities/stringify.ts | 49 ++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/utilities/stringify.ts b/src/utilities/stringify.ts index 6ce0dcb..2558686 100644 --- a/src/utilities/stringify.ts +++ b/src/utilities/stringify.ts @@ -1,7 +1,6 @@ import { configure } from 'safe-stable-stringify'; const safeStringify = configure({ - circularValue: 'Magic circle!', deterministic: false, strict: false, }); @@ -11,7 +10,7 @@ export const stringify = (value: unknown): string => { return safeStringify(value) ?? ''; } catch (error) { // eslint-disable-next-line no-console - console.error('could not serialize value', value); + console.error('[roarr] could not serialize value', value); throw error; } diff --git a/test/roarr/utilities/stringify.ts b/test/roarr/utilities/stringify.ts index f11e594..59fa08b 100644 --- a/test/roarr/utilities/stringify.ts +++ b/test/roarr/utilities/stringify.ts @@ -1,13 +1,34 @@ import { stringify } from '../../../src/utilities/stringify'; import test from 'ava'; -test('stringifies key=value', (t) => { +test('stringifies key=value (string)', (t) => { t.is( stringify({ foo: 'bar', }), '{"foo":"bar"}', ); +}); + +test('stringifies key=value (number)', (t) => { + t.is( + stringify({ + foo: 123, + }), + '{"foo":123}', + ); +}); + +test('stringifies key=value (function)', (t) => { + t.is( + stringify({ + foo: () => {}, + }), + '{}', + ); +}); + +test('stringifies key=value (undefined)', (t) => { t.is( stringify({ foo: undefined, @@ -15,3 +36,29 @@ test('stringifies key=value', (t) => { '{}', ); }); + +test('stringifies key=value (null)', (t) => { + t.is( + stringify({ + foo: null, + }), + '{"foo":null}', + ); +}); + +test('stringifies key=value (Symbol)', (t) => { + t.is( + stringify({ + foo: Symbol('bar'), + }), + '{}', + ); +}); + +test('stringifies key=value (circular)', (t) => { + const foo = {}; + + foo['foo'] = foo; + + t.is(stringify(foo), '{"foo":"[Circular]"}'); +});