Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@types/react-dom": "^19.0.0",
"@vitejs/plugin-react": "^4.3.3",
"@vitest/browser-playwright": "^4.0.0-beta.15",
"@vitest/utils": "^4.0.17",
"bumpp": "^9.4.2",
"changelogithub": "^0.13.9",
"eslint": "^9.8.0",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/pure.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { Locator, LocatorSelectors, PrettyDOMOptions } from 'vitest/browser'
import { page, utils } from 'vitest/browser'
import { page, server, utils } from 'vitest/browser'
import React from 'react'
import type { Container } from 'react-dom/client'
import ReactDOMClient from 'react-dom/client'
import { nanoid } from '@vitest/utils/helpers'

const { debug, getElementLocatorSelectors } = utils

function getTestIdAttribute() {
return server.config.browser.locators.testIdAttribute
}

let activeActs = 0

function setActEnvironment(env: boolean | undefined): void {
Expand Down Expand Up @@ -74,10 +79,14 @@ export async function render(
// default to document.body instead of documentElement to avoid output of potentially-large
// head elements (such as JSS style blocks) in debug output
baseElement = document.body
if (!document.body.hasAttribute(getTestIdAttribute())) {
document.body.setAttribute(getTestIdAttribute(), nanoid())
}
}

if (!container) {
container = baseElement.appendChild(document.createElement('div'))
container.setAttribute(getTestIdAttribute(), nanoid())
}

let root: ReactRoot
Expand Down
4 changes: 3 additions & 1 deletion test/__snapshots__/render.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`renders simple component 1`] = `
<div>
<div
data-testid="stable-snapshot"
>
<div>
Hello World
</div>
Expand Down
26 changes: 26 additions & 0 deletions test/render-selector.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, test } from 'vitest'
import { render } from 'vitest-browser-react'
import { page, server } from 'vitest/browser'

test('should apply and use a unique testid as the root selector when it does not exists', async () => {
const screen = await render(<div>Render</div>)

const selector = page.elementLocator(screen.baseElement).selector

expect(selector).toMatch(/^internal:testid=\[[^\]]*\]$/)
})

test('should apply and use a unique testid as the locator selector when using default container', async () => {
const screen = await render(<div>Render</div>)

expect(screen.locator.selector).toMatch(/^internal:testid=\[[^\]]*\]$/)
})

test('should not override testid attribute if already set', async () => {
document.body.setAttribute(server.config.browser.locators.testIdAttribute, 'custom-id')

const screen = await render(<div>Render</div>)
const selector = page.elementLocator(screen.baseElement).selector

expect(selector).toBe(`internal:testid=[${server.config.browser.locators.testIdAttribute}="custom-id"s]`)
})
4 changes: 3 additions & 1 deletion test/render.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test, vi } from 'vitest'
import { page, userEvent } from 'vitest/browser'
import { page, server, userEvent } from 'vitest/browser'
import { Button } from 'react-aria-components'
import { Suspense } from 'react'
import { render } from 'vitest-browser-react'
Expand All @@ -10,6 +10,8 @@ import { SuspendedHelloWorld } from './fixtures/SuspendedHelloWorld'
test('renders simple component', async () => {
const screen = await render(<HelloWorld />)
await expect.element(page.getByText('Hello World')).toBeVisible()

screen.container.setAttribute(server.config.browser.locators.testIdAttribute, 'stable-snapshot')
expect(screen.container).toMatchSnapshot()
})

Expand Down
4 changes: 4 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export default defineConfig({
test: { name: 'dev' },
resolve: { conditions: ['vdev'] },
},
{
extends: true,
test: { name: 'selector-custom-attr', include: ['test/render-selector.test.tsx'], browser: { locators: { testIdAttribute: 'data-custom-test-id' } } },
},
Comment thread
sheremet-va marked this conversation as resolved.
],
printConsoleTrace: true,
browser: {
Expand Down