Skip to content

Commit

Permalink
fix(utils): fix objDisplay default truncate option for test.each
Browse files Browse the repository at this point in the history
…title (#4917)
  • Loading branch information
hi-ogawa authored Jan 12, 2024
1 parent 21513f0 commit 9ae9dac
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/utils/src/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '')
Expand Down
22 changes: 22 additions & 0 deletions test/config/fixtures/chai-config/test-each-title.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {});
3 changes: 3 additions & 0 deletions test/config/fixtures/chai-config/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
81 changes: 81 additions & 0 deletions test/config/test/chai-config.test.ts
Original file line number Diff line number Diff line change
@@ -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, '')
}

0 comments on commit 9ae9dac

Please sign in to comment.