|
| 1 | +import util from 'node:util' |
| 2 | +import { describe, expect, test } from 'vitest' |
| 3 | +import { format } from '@vitest/utils' |
| 4 | + |
| 5 | +describe('format', () => { |
| 6 | + const obj = {} as any |
| 7 | + obj.obj = obj |
| 8 | + |
| 9 | + test.each([ |
| 10 | + [''], |
| 11 | + ['test'], |
| 12 | + [{ obj: { nested: true }, value: 1 }], |
| 13 | + ['test %s', 'test'], |
| 14 | + ['test %s %s', 'test', 'test'], |
| 15 | + ['test %s %s', 'test', 'test', 'test'], |
| 16 | + ['%s', 100], |
| 17 | + ['%s', 100n], |
| 18 | + ['%s', -0], |
| 19 | + ['%s', null], |
| 20 | + ['%s', null, 'next'], |
| 21 | + ['%d', 100], |
| 22 | + ['%d', 100n], |
| 23 | + ['%d', null], |
| 24 | + ['%d', {}], |
| 25 | + ['%d', {}, 'next'], |
| 26 | + ['%i', 100], |
| 27 | + ['%i', 100n], |
| 28 | + ['%i', null], |
| 29 | + ['%i', {}], |
| 30 | + ['%i', {}, 'next'], |
| 31 | + ['%f', 100], |
| 32 | + ['%f', 100n], |
| 33 | + ['%f', null], |
| 34 | + ['%f', {}], |
| 35 | + ['%f', {}, 'next'], |
| 36 | + ['%o', 'string'], |
| 37 | + ['%o', 100], |
| 38 | + ['%o', 100n], |
| 39 | + ['%o', null], |
| 40 | + ['%o', {}], |
| 41 | + ['%o', {}, 'next'], |
| 42 | + ['%O', 'string'], |
| 43 | + ['%O', 100], |
| 44 | + ['%O', 100n], |
| 45 | + ['%O', null], |
| 46 | + ['%O', {}], |
| 47 | + ['%O', {}, 'next'], |
| 48 | + ['%c', 'css value'], |
| 49 | + ['%c', 'css value', 'some other value'], |
| 50 | + ['%c %f', 'css value', '100.00'], |
| 51 | + ['%j', 'string'], |
| 52 | + ['%j', 100], |
| 53 | + ['%j', null], |
| 54 | + ['%j', {}], |
| 55 | + ['%j', {}, 'next'], |
| 56 | + ['%j', { obj }], |
| 57 | + ['%j', { fn: () => {} }], |
| 58 | + ['%%', 'string'], |
| 59 | + ])('format(%s)', (formatString, ...args) => { |
| 60 | + expect(format(formatString, ...args), `failed ${formatString}`).toBe(util.format(formatString, ...args)) |
| 61 | + }) |
| 62 | + |
| 63 | + test('cannont serialize some values', () => { |
| 64 | + expect(() => format('%j', 100n)).toThrowErrorMatchingInlineSnapshot('"Do not know how to serialize a BigInt"') |
| 65 | + }) |
| 66 | + |
| 67 | + test.each( |
| 68 | + [ |
| 69 | + { |
| 70 | + name: 'without format', |
| 71 | + args: [{ n: { a: { b: { c: { d: { e: '3' } } } } } }], |
| 72 | + result: '{ n: { a: { b: { c: { d: { e: \'3\' } } } } } }', |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'as an object', |
| 76 | + args: ['%o', {}, { n: { a: { b: { c: '3' } } } }], |
| 77 | + result: '{} { n: { a: { b: { c: \'3\' } } } }', |
| 78 | + }, |
| 79 | + { |
| 80 | + name: 'as a full object', |
| 81 | + args: ['%O', {}, { n: { a: { b: { c: '3' } } } }], |
| 82 | + result: '{} { n: { a: { b: { c: \'3\' } } } }', |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'as a json', |
| 86 | + args: ['%j', {}, { n: { a: { b: { c: '3' } } } }], |
| 87 | + result: '{} { n: { a: { b: { c: \'3\' } } } }', |
| 88 | + }, |
| 89 | + ], |
| 90 | + )('formats objects $name (loupe doesn\'t respect depth)', ({ args, result }) => { |
| 91 | + expect(format(...args)).toBe(result) |
| 92 | + }) |
| 93 | +}) |
0 commit comments