UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

83 lines (71 loc) 2.23 kB
import { unknownToString } from './unknown-to-string.mjs'; describe('unknownToString', () => { test('string', () => { const result = unknownToString('aaaaa'); expect(result).toBe('aaaaa'); expect(JSON.stringify('aaaaa')).toBe('"aaaaa"'); }); test('number', () => { const result = unknownToString(1); expect(result).toBe('1'); expect(JSON.stringify(1)).toBe('1'); }); test('boolean', () => { const result = unknownToString(true); expect(result).toBe('true'); expect(JSON.stringify(true)).toBe('true'); }); test('symbol', () => { const result = unknownToString(Symbol('sym')); expect(result).toBe('Symbol(sym)'); expect(JSON.stringify(Symbol('sym'))).toBeUndefined(); }); test('function', () => { const result = unknownToString(() => 0); expect(result).toBe('() => 0'); expect(JSON.stringify(() => 0)).toBeUndefined(); }); test('undefined', () => { const result = unknownToString(undefined); expect(result).toBe('undefined'); expect(JSON.stringify(undefined)).toBeUndefined(); }); test('null', () => { const result = unknownToString(null); expect(result).toBe('null'); expect(JSON.stringify(null)).toBe('null'); }); test('object', () => { const result = unknownToString({ a: { b: 1 } }); expect(result).toBe('{"a":{"b":1}}'); expect(JSON.stringify({ a: { b: 1 } })).toBe('{"a":{"b":1}}'); }); test('object(prettyPrint=true)', () => { const result = unknownToString( { a: { b: 1 } }, { prettyPrintObject: true }, ); expect(result).toBe( [ // `{`, ` "a": {`, ` "b": 1`, ` }`, `}`, ].join('\n'), ); }); test('circular reference returns error message', () => { const mut_circular: { a: number; self?: unknown } = { a: 1 }; mut_circular.self = mut_circular; const result = unknownToString(mut_circular); // Should return an error message string instead of throwing expect(typeof result).toBe('string'); expect(result).toMatch(/circular|serialize/iu); }); test('BigInt value', () => { const result = unknownToString(BigInt(123)); expect(result).toBe('123'); }); });