{"id":18408923,"url":"https://github.com/vitest-dev/vitest-browser-react","last_synced_at":"2025-05-15T21:04:33.147Z","repository":{"id":252402578,"uuid":"839015238","full_name":"vitest-dev/vitest-browser-react","owner":"vitest-dev","description":"Render React components in Vitest Browser Mode","archived":false,"fork":false,"pushed_at":"2025-02-11T14:58:46.000Z","size":114,"stargazers_count":159,"open_issues_count":8,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-12T05:16:54.197Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/vitest-browser-react","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vitest-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-06T19:48:54.000Z","updated_at":"2025-05-08T07:29:07.000Z","dependencies_parsed_at":"2024-12-27T23:36:01.792Z","dependency_job_id":"82102ddb-815a-45e2-baad-70b99a36b86e","html_url":"https://github.com/vitest-dev/vitest-browser-react","commit_stats":null,"previous_names":["vitest-dev/vitest-browser-react"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitest-dev%2Fvitest-browser-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitest-dev%2Fvitest-browser-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitest-dev%2Fvitest-browser-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitest-dev%2Fvitest-browser-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vitest-dev","download_url":"https://codeload.github.com/vitest-dev/vitest-browser-react/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254422754,"owners_count":22068678,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T03:22:28.768Z","updated_at":"2025-05-15T21:04:33.126Z","avatar_url":"https://github.com/vitest-dev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vitest-browser-react\n\nRender React components in Vitest Browser Mode. This library follows `testing-library` principles and exposes only [locators](https://vitest.dev/guide/browser/locators) and utilities that encourage you to write tests that closely resemble how your React components are used.\n\n`vitest-browser-react` aims to deliver a good developer experience in Vitest Browser Mode by incorporating the [locators API](https://vitest.dev/guide/browser/locators.html) and [retry-ability](https://vitest.dev/guide/browser/assertion-api.html) mechanism directly into the `render` result. This allows you to call user methods without needing to verify the element's existence or wait for external events (like API calls) to render the element.\n\nRequires `vitest` and `@vitest/browser` 2.1.0 or higher.\n\n```tsx\nimport { render } from 'vitest-browser-react'\nimport { expect, test } from 'vitest'\n\ntest('counter button increments the count', async () =\u003e {\n  const screen = render(\u003cComponent count={1} /\u003e)\n\n  await screen.getByRole('button', { name: 'Increment' }).click()\n\n  await expect.element(screen.getByText('Count is 2')).toBeVisible()\n})\n```\n\n\u003e 💡 This library doesn't expose React's `act` and uses it only to flush operations happening as part of `useEffect` during initial rendering and unmouting.\nOther use cases are handled by CDP and `expect.element` which both have built-in [retry-ability mechanism](https://vitest.dev/guide/browser/assertion-api).\n\n`vitest-browser-react` also exposes `renderHook` helper to test React hooks.\n\n```tsx\nimport { renderHook } from 'vitest-browser-react'\nimport { expect, test } from 'vitest'\nimport { act } from 'react'\n\ntest('should increment counter', async () =\u003e {\n  const { result } = renderHook(() =\u003e useCounter())\n\n  act(() =\u003e {\n    result.current.increment()\n  })\n\n  expect(result.current.count).toBe(1)\n})\n```\n\n`vitest-browser-react` also automatically injects `render` method on the `page`. Example:\n\n```ts\n// vitest.config.ts\nimport { defineConfig } from 'vitest/config'\n\nexport default defineConfig({\n  test: {\n    setupFiles: ['./setup-file.ts'],\n    browser: {\n      name: 'chromium',\n      enabled: true,\n    },\n  },\n})\n\n// ./setup-file.ts\n// add an import at the top of your setup file so TypeScript can pick up types\nimport 'vitest-browser-react'\n```\n\n```tsx\nimport { page } from '@vitest/browser/context'\n\ntest('counter button increments the count', async () =\u003e {\n  const screen = page.render(\u003cComponent count={1} /\u003e)\n\n  screen.cleanup()\n})\n```\n\nUnlike `@testing-library/react`, `vitest-browser-react` performs cleanup of the component before the test begins, allowing you to see the rendered result in your UI. If you prefer to disable auto-cleanup, you can import the `render` function from `vitest-browser-react/pure`.\n\n## Configuration\n\nYou can configure if the component should be rendered in Strict Mode with `configure` method from `vitest-browser-react/pure`:\n\n```ts\nimport { configure } from 'vitest-browser-react/pure'\n\nconfigure({\n  // disabled by default\n  reactStrictMode: true,\n})\n```\n\n## Special thanks\n\n- Inspired by [`@testing-library/react`](https://github.com/testing-library/react-testing-library)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitest-dev%2Fvitest-browser-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvitest-dev%2Fvitest-browser-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitest-dev%2Fvitest-browser-react/lists"}