Skip to content

Commit

Permalink
Merge pull request #63 from cassus/component-tests
Browse files Browse the repository at this point in the history
feat: support component tests
  • Loading branch information
vitalets authored Dec 11, 2023
2 parents 051683a + 0e4b583 commit 9e80d5d
Show file tree
Hide file tree
Showing 22 changed files with 8,746 additions and 13,726 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ assignees: ''
**Then**
<!-- What did actually happen? If there is error - please post full error message with stack trace -->

**But I expected**
**But I expect**
<!-- What did you expect? -->

**Isolated demo**
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ jobs:
with:
node-version: 18
- run: npm ci
- run: npm install @playwright/test@${{ matrix.playwrightVersion }} @cucumber/cucumber@${{ matrix.cucumberVersion }}
- run: >
npm install
@playwright/test@${{ matrix.playwrightVersion }}
@playwright/experimental-ct-react@${{ matrix.playwrightVersion }}
@cucumber/cucumber@${{ matrix.cucumberVersion }}
- run: npx playwright install --with-deps chromium
- run: npm test
- uses: actions/upload-artifact@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ npm-debug.log
# my
/dist
**/.features-gen/**/*.spec.js
.cache
test-results
playwright-report
/examples/playwright-bdd*.tgz
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
.features-gen
.cache
playwright-report
**/*.md
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## dev
* support component tests, [#57](https://github.com/vitalets/playwright-bdd/issues/57)
* generate skipped tests with empty body, [#73](https://github.com/vitalets/playwright-bdd/issues/73)
* allow outline scenario name to be used as an examples template, [#67](https://github.com/vitalets/playwright-bdd/issues/67)
* fix empty step locations for esm
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ Have a look on [this section](https://vitalets.github.io/playwright-bdd/#/faq?id
#### Test locally on different Playwright versions
```
# install needed Playwright version
npm i --no-save @playwright/test@1.33
npm i --no-save @playwright/test@1.40 @playwright/[email protected]
# install corresponding chromium without clearing other versions
npx cross-env PLAYWRIGHT_SKIP_BROWSER_GC=1 playwright install chromium
npx cross-env PLAYWRIGHT_SKIP_BROWSER_GC=1 npx playwright install chromium
# run all tests
npm run test
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Writing steps](writing-steps.md)
- [Hooks](./hooks.md)
- [Decorators](decorators.md)
- [Component tests](./component-tests.md)
- [Recipes](recipes.md)
- [CLI](cli.md)
- [API](api.md)
Expand Down
78 changes: 78 additions & 0 deletions docs/component-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Component tests
Playwright-bdd supports [component tests](https://playwright.dev/docs/test-components)
since Playwright **1.39**.

Initialize components testing [per instruction](https://playwright.dev/docs/test-components#how-to-get-started):
```
npm init playwright@latest -- --ct
```

Add `playwright-bdd` configuration to `playwright-ct.config.ts`:
```ts
import { defineConfig, devices } from '@playwright/experimental-ct-react';
import { defineBddConfig } from 'playwright-bdd';

const testDir = defineBddConfig({
importTestFrom: 'fixtures.ts',
paths: ['features/*.feature'],
require: ['steps.tsx'],
});

export default defineConfig({
testDir,
// ...
});
```

Create `fixtures.ts` that exports custom `test`:
```ts
import { mergeTests } from '@playwright/test';
import { test as ctBase } from '@playwright/experimental-ct-react';
import { test as base } from 'playwright-bdd';

export const test = mergeTests(base, ctBase);
```

Define steps in `steps.tsx`:
```ts
import React from 'react';
import { expect } from '@playwright/test';
import { createBdd } from 'playwright-bdd';
import { test } from './fixtures';

const { Given, When, Then } = createBdd(test);

Given('Mounted input component', async ({ mount }) => {
await mount(<textarea data-testid="textField" />);
});

When('I type {string}', async ({ page }, arg: string) => {
await page.getByTestId('textField').fill(arg);
});

Then('input field has {string}', async ({ page }, arg: string) => {
await expect(page.getByTestId('textField')).toHaveValue(arg);
});
```

Create feature file `input.feature`:
```gherkin
Feature: input component
Scenario: Mount component and interact with it
Given Mounted input component
When I type "ABC"
Then input field has "ABC"
```

Update `test-ct` command in `package.json` to run `bddgen` before running Playwright:
```json
"scripts": {
"test-ct": "npx bddgen -c playwright-ct.config.ts && playwright test -c playwright-ct.config.ts"
},
```

Finally, run tests:
```
npm run test-ct
```
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const playwright = require('eslint-plugin-playwright');

module.exports = [
{
ignores: ['examples', 'dist', '*.config.js', 'cucumber.js'],
ignores: ['examples', 'dist', '*.config.js', 'cucumber.js', 'test/**/.cache'],
},
js.configs.recommended,
{
Expand Down
Loading

0 comments on commit 9e80d5d

Please sign in to comment.