diff --git a/docs/src/test-components-js.md b/docs/src/test-components-js.md index df649403d5..6738918d62 100644 --- a/docs/src/test-components-js.md +++ b/docs/src/test-components-js.md @@ -5,13 +5,10 @@ title: "Experimental: components" Playwright Test can now test your components. - -
- ## Example Here is what a typical component test looks like: @@ -42,16 +39,31 @@ Adding Playwright Test to an existing React, Vue or Svelte project is easy. Belo ### Step 1: Install Playwright Test for components for your respective framework -```sh + + + +```bash npm init playwright@latest -- --ct ``` -or with Yarn: + -```sh + + +```bash yarn create playwright --ct ``` + + + + This step creates several files in your workspace: #### `playwright/index.html` @@ -72,13 +84,23 @@ also link the script called `playwright/index.[tj]s`. #### `playwright/index.ts` You can include stylesheets, apply theme and inject code into the page where -component is mounted using this script. It can be either `.js` or `.ts` file. +component is mounted using this script. It can be either a `.js` or `.ts` file. ```js // Apply theme here, add anything your component needs at runtime here. ``` -### Step 2. Create a test file `src/App.spec.tsx` +### Step 2. Create a test file `src/App.spec.{ts,tsx}` + + + ```js import { test, expect } from '@playwright/experimental-ct-react'; @@ -92,8 +114,53 @@ test('should work', async ({ mount }) => { }); ``` + + + + +```js +import { test, expect } from '@playwright/experimental-ct-vue'; +import App from './App.vue'; + +test.use({ viewport: { width: 500, height: 500 } }); + +test('should work', async ({ mount }) => { + const component = await mount(); + await expect(component).toContainText('Vite + Vue'); +}); +``` + +If using TypeScript and Vue make sure to add a `vue.d.ts` file to your project: + +```ts +declare module '*.vue'; +``` + + + + + +```js +import { test, expect } from '@playwright/experimental-ct-svelte'; +import App from './App.svelte'; + +test.use({ viewport: { width: 500, height: 500 } }); + +test('should work', async ({ mount }) => { + const component = await mount(App); + await expect(component).toContainText('Vite + Svelte'); +}); +``` + + + + + + ### Step 3. Run the tests +You can run tests using the [VS Code extension](./getting-started-vscode.md) or the command line. + ```sh npm run test-ct ```