Skip to content

Commit 67ce461

Browse files
jrolfsZache
andcommitted
feat: add support for @playwright/test via fixture
Going forward, two packages will be published: - **playwright-testing-library** to support the standalone queries with the **playwright** package (no longer supports extending the Playwright prototypes via `/extend`) - **@playwright-testing-library/test** to support both standalone queries with **@playwright/test** _as well as_ a Playwright Test [fixture](https://playwright.dev/docs/test-fixtures) (available at `@playwright-testing-library/test/fixture`) BREAKING CHANGE: Removes support for extending Playwright prototypes (`/extend` entrypoint) BREAKING CHANGE: Only Playwright 1.12+ is officially supported going forward Co-authored-by: Zacharias Björngren <[email protected]>
1 parent 200d255 commit 67ce461

21 files changed

+321
-292
lines changed

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ module.exports = {
1212
'no-restricted-syntax': 'off',
1313
'no-underscore-dangle': ['error', {allow: ['__regex', '__flags']}],
1414
},
15+
overrides: [
16+
{
17+
files: ['playwright-test/*.+(js|ts)'],
18+
rules: {
19+
'import/no-extraneous-dependencies': ['error', {devDependencies: true}],
20+
},
21+
},
22+
],
1523
}

.github/workflows/build.yml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
strategy:
3131
matrix:
3232
node: ['12', '14', '16']
33-
playwright: ['1.1.0', 'latest']
33+
playwright: ['1.12.0', 'latest']
3434

3535
steps:
3636
- name: Checkout
@@ -40,9 +40,9 @@ jobs:
4040
uses: actions/setup-node@master
4141
with:
4242
node-version: ${{ matrix.node }}
43-
43+
4444
- name: Update to npm 7
45-
run: npm i -g npm@7 --registry=https://registry.npmjs.org
45+
run: npm i -g npm@7 --registry=https://registry.npmjs.org
4646

4747
- uses: actions/cache@v2
4848
with:
@@ -54,27 +54,31 @@ jobs:
5454
- name: Install dependencies
5555
run: npm install
5656

57-
# Old versions of Playwright (<= 1.1.0) fail on Node 16
58-
5957
- name: Install specific Playwright version
60-
if: ${{ !(matrix.node == '16' && matrix.playwright == '1.1.0') }}
6158
run: |
6259
npm install playwright@${{ matrix.playwright }}
63-
npm why playwright
60+
npm install @playwright/test@${{ matrix.playwright }}
6461
65-
- name: Build
66-
if: ${{ !(matrix.node == '16' && matrix.playwright == '1.1.0') }}
67-
run: npm run rebuild
68-
69-
- name: Run lint + tests
70-
if: ${{ !(matrix.node == '16' && matrix.playwright == '1.1.0') }}
71-
run: npm run validate
62+
- name: Check types, run lint + tests
63+
run: |
64+
npm why playwright
65+
npm why @playwright/test
66+
npm run validate
7267
7368
# Only release on Node 14
7469

75-
- name: Upload Coverage / Release
70+
- name: Upload Coverage / Release / Playwright
7671
run: npm run ci-after-success
77-
if: ${{ matrix.node == '14' }}
72+
if: ${{ matrix.node == '14' && matrix.playwright == 'latest' }}
73+
env:
74+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
75+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76+
77+
- name: Upload Coverage / Release / Playwright Test
78+
run: |
79+
npm run prepare:playwright-test
80+
npm run ci-after-success
81+
if: ${{ matrix.node == '14' && matrix.playwright == 'latest' }}
7882
env:
7983
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
8084
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dist/
55
yarn-error.log
66

77
dom-testing-library.js
8-
extend.js
9-
extend.d.ts
8+
fixture.js
9+
fixture.d.ts
1010

1111
.vscode/

.husky/commit-msg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
yarn hover-scripts commit-msg
4+
npm run commit-msg

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
yarn hover-scripts pre-commit --no-toc
4+
npm run pre-commit

README.md

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
3030
All of your favorite user-centric querying functions from **@testing-library/react** and **@testing-library/dom** available from Playwright!
3131

32-
- Standalone queries **`playwright-testing-library`** or...
33-
- Playwright extensions**`playwright-testing-library/extend`**
32+
- Playwright Test [fixture](https://playwright.dev/docs/test-fixtures) **`@playwright-testing-library/test/fixture`** or...
33+
- Standalone queries**`playwright-testing-library`**/**`@playwright-testing-library/test`**
3434
- Asynchronous assertion helper (via **[wait-for-expect](https://github.com/TheBrainFamily/wait-for-expect)**)
3535

3636
## 🌱 Getting Started
@@ -47,53 +47,66 @@ or
4747
yarn add --dev playwright-testing-library
4848
```
4949

50-
### 2a. Use _standalone queries_
50+
### 2a. Use _Playwright Test [fixture](https://playwright.dev/docs/test-fixtures)_
5151

52-
```js
53-
const {webkit} = require('playwright') // or 'firefox' or 'chromium'
54-
const {getDocument, queries} = require('playwright-testing-library')
52+
```ts
53+
import {test as baseTest} from '@playwright/test'
54+
import {fixtures, TestingLibraryFixtures} from '@playwright-testing-library/test/fixture'
5555

56-
const {getByTestId, getByLabelText} = queries
56+
// As only fixture
57+
const test = baseTest.extend<TestingLibraryFixtures>(fixtures)
5758

58-
const browser = await webkit.launch()
59-
const page = await browser.newPage()
59+
// Alternatively, with other fixtures
60+
interface Fixtures extends TestingLibraryFixtures {
61+
// ... additional fixture types
62+
}
6063

61-
// Grab ElementHandle for document
62-
const $document = await getDocument(page)
64+
const test = baseTest.extend<Fixtures>({
65+
...fixtures,
66+
// ... additional fixtures
67+
})
6368

64-
// Your favorite query methods are available
65-
const $form = await getByTestId($document, 'my-form')
69+
const {expect} = test
6670

67-
// Returned elements are ElementHandles too!
68-
const $email = await getByLabelText($form, 'Email')
71+
// Query methods are available in `test` blocks
72+
test('my form', ({queries: {getByTestId}}) => {
73+
const $form = await getByTestId('my-form')
6974

70-
// Interact with playwright like usual
71-
await $email.type('[email protected]')
75+
// Scope queries with `getQueriesForElement`
76+
const {getByLabelText} = $form.getQueriesForElement()
7277

73-
// ...
78+
const $email = await getByLabelText('Email')
79+
80+
// Interact with Playwright like usual
81+
await $email.type('[email protected]')
82+
83+
expect($email).toHaveValue('[email protected]')
84+
85+
// ...
86+
})
7487
```
7588

76-
### 2b. Use _extensions_
89+
### 2b. Use _standalone queries_
7790

7891
```js
7992
const {webkit} = require('playwright') // or 'firefox' or 'chromium'
80-
require('playwright-testing-library/extend')
93+
const {getDocument, queries} = require('playwright-testing-library')
94+
95+
const {getByTestId, getByLabelText} = queries
8196

8297
const browser = await webkit.launch()
8398
const page = await browser.newPage()
8499

85-
// Grab document with `getDocument`, which is added to the prototype of `Paqe`
86-
const $document = await page.getDocument()
87-
88-
// Query methods are added directly to prototype of `ElementHandle`
89-
const $form = await $document.getByTestId('my-form')
100+
// Grab ElementHandle for document
101+
const $document = await getDocument(page)
90102

91-
// Scope queries with `getQueriesForElement`
92-
const {getByLabelText} = $form.getQueriesForElement()
103+
// Your favorite query methods are available
104+
const $form = await getByTestId($document, 'my-form')
93105

94-
const $email = await getByLabelText('Email')
106+
// Returned elements are ElementHandles too!
107+
const $email = await getByLabelText($form, 'Email')
95108

96-
// Interact with Playwright like usual
109+
// Interact with playwright like usual
97110
await $email.type('[email protected]')
98111

99112
// ...
File renamed without changes.

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
collectCoverageFrom: ['**/*.ts', '!**/*.d.ts'],
66
coverageThreshold: {},
77
setupFilesAfterEnv: ['./jest.setup.ts'],
8-
testTimeout: 15000,
8+
testMatch: ['<rootDir>/test/standalone/*.+(test.js|test.jsx|test.ts|test.tsx)'],
9+
testTimeout: 30000,
910
}

lib/extend.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/fixture.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type {PlaywrightTestArgs, TestFixture} from '@playwright/test'
2+
import type {IScopedQueryUtils as Queries} from './typedefs'
3+
import {getDocument, getQueriesForElement} from '.'
4+
5+
interface TestingLibraryFixtures {
6+
queries: Queries
7+
}
8+
9+
const fixture: TestFixture<Queries, PlaywrightTestArgs> = async ({page}, use) => {
10+
const document = await getDocument(page)
11+
12+
const queries = getQueriesForElement(document)
13+
14+
await use(queries)
15+
}
16+
17+
const fixtures = {queries: fixture}
18+
19+
export {fixture, fixtures}
20+
export type {Queries, TestingLibraryFixtures}

package.json

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
"description": "playwright + dom-testing-library",
55
"main": "./dist/index.js",
66
"scripts": {
7-
"build": "npm run build:types && npm run build:rollup",
8-
"build:rollup": "rollup -c rollup.config.js",
9-
"build:types": "tsc",
7+
"build": "run-s clean build:testing-library build:source",
8+
"build:source": "tsc",
9+
"build:testing-library": "rollup -c",
1010
"ci-after-success": "hover-scripts ci-after-success",
11-
"clean": "rm -fR dist/",
11+
"clean": "rimraf dist/*",
1212
"commit-msg": "hover-scripts commit-msg",
1313
"lint": "hover-scripts lint",
1414
"pre-commit": "hover-scripts pre-commit --no-toc",
1515
"prepare": "husky install",
16-
"prepublishOnly": "npm run rebuild && generate-export-aliases",
17-
"rebuild": "npm run clean && npm run build",
18-
"test": "hover-scripts test",
19-
"validate": "hover-scripts validate 'lint,test --coverage --runInBand --verbose'"
16+
"prepare:playwright-test": "run-s prepare:playwright-test:package prepare:playwright-test:imports build:source prepare:playwright-test:entries",
17+
"prepare:playwright-test:entries": "generate-export-aliases",
18+
"prepare:playwright-test:imports": "./playwright-test/prepare-package.js",
19+
"prepare:playwright-test:package": "jscodeshift -t ./playwright-test/rename-imports.ts --extensions=ts --parser=ts ./lib",
20+
"prepublishOnly": "npm run build",
21+
"test": "run-s build:testing-library test:*",
22+
"test:fixture": "playwright test",
23+
"test:standalone": "hover-scripts test --no-watch",
24+
"test:types": "tsc --noEmit",
25+
"validate": "run-s test"
2026
},
2127
"repository": {
2228
"type": "git",
@@ -37,7 +43,7 @@
3743
],
3844
"config": {
3945
"exportAliases": {
40-
"extend": "./dist/extend"
46+
"fixture": "./dist/fixture"
4147
}
4248
},
4349
"dependencies": {
@@ -51,16 +57,20 @@
5157
"@rollup/plugin-node-resolve": "^13.0.5",
5258
"@rollup/plugin-replace": "^3.0.0",
5359
"@types/jest": "^27.0.2",
60+
"@types/jscodeshift": "^0.11.2",
5461
"generate-export-aliases": "^1.1.0",
5562
"husky": "^7.0.2",
63+
"jscodeshift": "^0.13.0",
64+
"npm-run-all": "^4.1.5",
5665
"playwright": "^1.15.1",
66+
"rimraf": "^3.0.2",
5767
"rollup": "^2.58.0",
5868
"setimmediate": "^1.0.5",
5969
"typescript": "^4.4.3"
6070
},
6171
"peerDependencies": {
62-
"playwright": "^1.1.0",
63-
"@playwright/test": "^1.12.0"
72+
"@playwright/test": "^1.12.0",
73+
"playwright": "^1.12.0"
6474
},
6575
"peerDependenciesMeta": {
6676
"playwright": {

playwright-test/prepare-package.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
4+
const fs = require('fs/promises')
5+
const path = require('path')
6+
7+
const PACKAGE = path.join(__dirname, '..', 'package.json')
8+
9+
const run = async () => {
10+
const package = JSON.parse(await fs.readFile(PACKAGE, {encoding: 'utf-8'}))
11+
12+
const modifiedPackage = {
13+
...package,
14+
name: '@playwright-testing-library/test',
15+
}
16+
17+
await fs.writeFile(PACKAGE, JSON.stringify(modifiedPackage, undefined, 2))
18+
}
19+
20+
run()
21+
.then(() => process.exit(0))
22+
.catch(error => {
23+
console.error(error)
24+
process.exit(1)
25+
})

playwright-test/rename-imports.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {API, FileInfo} from 'jscodeshift'
2+
3+
const transformer = (file: FileInfo, {jscodeshift: j}: API) =>
4+
j(file.source)
5+
.find(j.ImportDeclaration)
6+
.find(j.StringLiteral)
7+
.filter(path => path.value.value === 'playwright')
8+
.replaceWith(() => j.stringLiteral('@playwright/test'))
9+
.toSource()
10+
11+
export default transformer

0 commit comments

Comments
 (0)