From 9ae9dac955a299a6bcfbf17cf2a72771a158ed32 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 12 Jan 2024 18:59:02 +0900 Subject: [PATCH] fix(utils): fix `objDisplay` default truncate option for `test.each` title (#4917) --- packages/utils/src/display.ts | 7 +- .../chai-config/test-each-title.test.ts | 22 +++++ .../fixtures/chai-config/vitest.config.ts | 3 + test/config/test/chai-config.test.ts | 81 +++++++++++++++++++ 4 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 test/config/fixtures/chai-config/test-each-title.test.ts create mode 100644 test/config/fixtures/chai-config/vitest.config.ts create mode 100644 test/config/test/chai-config.test.ts diff --git a/packages/utils/src/display.ts b/packages/utils/src/display.ts index 0a861c185869..3ba7454cebb7 100644 --- a/packages/utils/src/display.ts +++ b/packages/utils/src/display.ts @@ -98,18 +98,19 @@ export function format(...args: unknown[]) { return str } -export function inspect(obj: unknown, options: LoupeOptions = {}) { +export function inspect(obj: unknown, options: LoupeOptions = {}): string { if (options.truncate === 0) options.truncate = Number.POSITIVE_INFINITY return loupe(obj, options) } export function objDisplay(obj: unknown, options: LoupeOptions = {}): string { - const truncateThreshold = options.truncate ?? 40 + if (typeof options.truncate === 'undefined') + options.truncate = 40 const str = inspect(obj, options) const type = Object.prototype.toString.call(obj) - if (truncateThreshold && str.length >= truncateThreshold) { + if (options.truncate && str.length >= options.truncate) { if (type === '[object Function]') { const fn = obj as () => void return (!fn.name || fn.name === '') diff --git a/test/config/fixtures/chai-config/test-each-title.test.ts b/test/config/fixtures/chai-config/test-each-title.test.ts new file mode 100644 index 000000000000..42750f67c1df --- /dev/null +++ b/test/config/fixtures/chai-config/test-each-title.test.ts @@ -0,0 +1,22 @@ +import { expect, test } from 'vitest' + +test.each` + length | param + ${30} | ${'0123456789'.repeat(3)} + ${40} | ${'0123456789'.repeat(4)} + ${50} | ${'0123456789'.repeat(5)} +`('$param (length = $length)', () => {}); + +test.each` + param + ${['one', 'two', 'three']} + ${['one', 'two', 'three', 'four']} + ${['one', 'two', 'three', 'four', 'five']} +`('$param', () => {}); + +test.each` + param + ${{ one: 1, two: 2, three: 3 }} + ${{ one: 1, two: 2, three: 3, four: 4 }} + ${{ one: 1, two: 2, three: 3, four: 4, five: 5 }} +`('$param', () => {}); diff --git a/test/config/fixtures/chai-config/vitest.config.ts b/test/config/fixtures/chai-config/vitest.config.ts new file mode 100644 index 000000000000..abed6b2116e1 --- /dev/null +++ b/test/config/fixtures/chai-config/vitest.config.ts @@ -0,0 +1,3 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({}) diff --git a/test/config/test/chai-config.test.ts b/test/config/test/chai-config.test.ts new file mode 100644 index 000000000000..9d99ba26eb5b --- /dev/null +++ b/test/config/test/chai-config.test.ts @@ -0,0 +1,81 @@ +import { describe, expect, it } from 'vitest' +import { runVitest } from '../../test-utils' + +describe('truncateThreshold', () => { + it('default', async () => { + const result = await runVitest({ + root: 'fixtures/chai-config', + reporters: ['tap-flat'], + }) + expect(cleanOutput(result.stdout)).toMatchInlineSnapshot(` + "TAP version 13 + 1..9 + ok 1 - test-each-title.test.ts > '012345678901234567890123456789' (length = 30) + ok 2 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 40) + ok 3 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 50) + ok 4 - test-each-title.test.ts > [ 'one', 'two', 'three' ] + ok 5 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four' ] + ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ] + ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 } + ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 } + ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) } + " + `) + expect(result.exitCode).toBe(0) + }) + + it('40', async () => { + const result = await runVitest({ + root: 'fixtures/chai-config', + reporters: ['tap-flat'], + chaiConfig: { + truncateThreshold: 40, + }, + }) + expect(cleanOutput(result.stdout)).toMatchInlineSnapshot(` + "TAP version 13 + 1..9 + ok 1 - test-each-title.test.ts > '012345678901234567890123456789' (length = 30) + ok 2 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 40) + ok 3 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 50) + ok 4 - test-each-title.test.ts > [ 'one', 'two', 'three' ] + ok 5 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four' ] + ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ] + ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 } + ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 } + ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) } + " + `) + expect(result.exitCode).toBe(0) + }) + + it('0', async () => { + const result = await runVitest({ + root: 'fixtures/chai-config', + reporters: ['tap-flat'], + chaiConfig: { + truncateThreshold: 0, + }, + }) + expect(cleanOutput(result.stdout)).toMatchInlineSnapshot(` + "TAP version 13 + 1..9 + ok 1 - test-each-title.test.ts > '012345678901234567890123456789' (length = 30) + ok 2 - test-each-title.test.ts > '0123456789012345678901234567890123456789' (length = 40) + ok 3 - test-each-title.test.ts > '01234567890123456789012345678901234567890123456789' (length = 50) + ok 4 - test-each-title.test.ts > [ 'one', 'two', 'three' ] + ok 5 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four' ] + ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', 'five' ] + ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 } + ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 } + ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4, five: 5 } + " + `) + expect(result.exitCode).toBe(0) + }) +}) + +function cleanOutput(output: string) { + // remove non-deterministic output + return output.replaceAll(/\s*# time=.*/g, '') +}