fix(browser): hide injected data-testid attributes (#9503) · vitest-dev/vitest@f89899c

File tree

2 files changed

lines changed

  • packages/pretty-format/src/plugins/lib

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -23,6 +23,16 @@ export function printProps(

2323

return keys

2424

.map((key) => {

2525

const value = props[key]

26+

// hidden injected value that should not be printed

27+

if (

28+

typeof value === 'string'

29+

&& value[0] === '_'

30+

&& value.startsWith('__vitest_')

31+

&& value.match(/__vitest_\d+__/)

32+

) {

33+

return ''

34+

}

35+
2636

let printed = printer(value, config, indentationNext, depth, refs)

2737
2838

if (typeof value !== 'string') {

Original file line numberDiff line numberDiff line change

@@ -12,3 +12,11 @@ test('file snapshot', async () => {

1212

await expect('my snapshot content')

1313

.toMatchFileSnapshot('./__snapshots__/custom/my_snapshot')

1414

})

15+
16+

test('vitest attribute is hidden', () => {

17+

const div = document.createElement('div')

18+

div.setAttribute('data-testid', '__vitest_1__')

19+

expect(div).toMatchInlineSnapshot(`

20+

<div />

21+

`)

22+

})