Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
chore: add Prettier (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton authored May 12, 2021
1 parent bd931ff commit db7a938
Show file tree
Hide file tree
Showing 29 changed files with 438 additions and 249 deletions.
43 changes: 22 additions & 21 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
name: Node.js CI
on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: |
sudo apt update
# chromium dependencies
sudo apt-get install libgbm1
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x
- run: npm ci
- name: Lint
run: |
yarn run tsc --noEmit
- run: npm run build
- run: npm test
- run: npx codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- uses: actions/checkout@v2
- name: Install dependencies
run: |
sudo apt update
# chromium dependencies
npx playwright-chromium install-deps chromium
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x
- run: npm ci
- name: Lint
run: |
npx tsc --noEmit
npx prettier --check .
- run: npm run build
- run: npm test
- run: npx codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage/
lib/
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"semi": false
}
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To activate it in your Jest environment you have to include it in your configura

```json
{
"setupFilesAfterEnv": ["expect-playwright"]
"setupFilesAfterEnv": ["expect-playwright"]
}
```

Expand All @@ -39,7 +39,7 @@ Example which should wait and compare the text content of a paragraph on the pag
```javascript
// before
await page.waitForSelector("#foo")
const textContent = await page.$eval("#foo", el => el.textContent)
const textContent = await page.$eval("#foo", (el) => el.textContent)
expect(textContent).stringContaining("my text")

// after by using expect-playwright
Expand Down Expand Up @@ -68,7 +68,7 @@ This function waits as a maximum as the timeout exceeds for a given selector onc
await expect(page).toHaveSelector("#foobar")
// or via not, useful to only wait 1 second instead of for the default timeout by Playwright which is 30 seconds.
await expect(page).not.toHaveSelector("#foobar", {
timeout: 1 * 1000
timeout: 1 * 1000,
})
```

Expand All @@ -82,7 +82,7 @@ This function checks if the given selector has focus.
await expect(page).toHaveFocus("#foobar")
// or via not, useful to only wait 1 second instead of for the default timeout by Playwright which is 30 seconds.
await expect(page).not.toHaveFocus("#foobar", {
timeout: 1 * 1000
timeout: 1 * 1000,
})
```

Expand Down Expand Up @@ -131,7 +131,7 @@ Or by passing a Playwright [ElementHandle]:
**expect(element: [ElementHandle]).toHaveText(value: string)**

```javascript
const element = await page.$('#my-element');
const element = await page.$("#my-element")
await expect(element).toHaveText("Playwright")
```

Expand Down Expand Up @@ -160,7 +160,7 @@ Or by passing a Playwright [ElementHandle]:
**expect(element: [ElementHandle]).toEqualText(value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))**

```javascript
const element = await page.$('#my-element');
const element = await page.$("#my-element")
await expect(element).toEqualText("Playwright")
```

Expand All @@ -181,14 +181,14 @@ Or by passing a Playwright [ElementHandle]:
**expect(element: [ElementHandle]).toEqualValue(value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))**

```javascript
const element = await page.$('#my-element');
const element = await page.$("#my-element")
await expect(element).toEqualValue("Playwright")
```

## Examples

```typescript
import playwright from 'playwright-chromium'
import playwright from "playwright-chromium"

describe("GitHub Playwright project", () => {
it("should should have Playwright in the README heading", async () => {
Expand All @@ -197,7 +197,9 @@ describe("GitHub Playwright project", () => {
await page.goto("https://github.com/microsoft/playwright")
await expect(page).toHaveText("#readme h1", "Playwright")
// or also all of them via the not property
await expect(page).not.toHaveText("this-is-no-anywhere", { timeout: 1 * 1000 })
await expect(page).not.toHaveText("this-is-no-anywhere", {
timeout: 1 * 1000,
})
await browser.close()
})
})
Expand All @@ -217,6 +219,6 @@ at the top of your test file or include it globally in your `tsconfig.json`.

- [expect-puppeteer](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/expect-puppeteer)

[ElementHandle]: https://github.com/microsoft/playwright/blob/master/docs/api.md#class-elementhandle
[Page]: https://github.com/microsoft/playwright/blob/master/docs/api.md#class-page
[Playwright]: https://github.com/microsoft/Playwright
[elementhandle]: https://github.com/microsoft/playwright/blob/master/docs/api.md#class-elementhandle
[page]: https://github.com/microsoft/playwright/blob/master/docs/api.md#class-page
[playwright]: https://github.com/microsoft/Playwright
80 changes: 51 additions & 29 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface PageWaitForSelectorOptions {
* - `'hidden'` - wait for element to be either detached from DOM, or have an empty bounding box or `visibility:hidden`.
* This is opposite to the `'visible'` option.
*/
state?: "attached" | "detached" | "visible" | "hidden";
state?: "attached" | "detached" | "visible" | "hidden"

/**
* Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by
Expand All @@ -18,55 +18,77 @@ interface PageWaitForSelectorOptions {
* or [page.setDefaultTimeout(…)](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagesetdefaulttimeout)
* methods.
*/
timeout?: number;
timeout?: number
}

export interface PlaywrightMatchers<R> {
/**
* Will check if the element's textContent on the page determined by the selector includes the given text.
*/
toHaveText(selector: string, value: string, options?: PageWaitForSelectorOptions): Promise<R>;
* Will check if the element's textContent on the page determined by the selector includes the given text.
*/
toHaveText(
selector: string,
value: string,
options?: PageWaitForSelectorOptions
): Promise<R>
/**
* Will check if the element's value includes the given text.
*/
toHaveText(value: string): Promise<R>;
* Will check if the element's value includes the given text.
*/
toHaveText(value: string): Promise<R>
/**
* Will compare the element's textContent on the page determined by the selector with the given text.
*/
toEqualText(selector: string, value: string, options?: PageWaitForSelectorOptions): Promise<R>;
* Will compare the element's textContent on the page determined by the selector with the given text.
*/
toEqualText(
selector: string,
value: string,
options?: PageWaitForSelectorOptions
): Promise<R>
/**
* Will compare the element's textContent by the given text.
*/
toEqualText(value: string, options?: PageWaitForSelectorOptions): Promise<R>;
* Will compare the element's textContent by the given text.
*/
toEqualText(value: string, options?: PageWaitForSelectorOptions): Promise<R>
/**
* Will ensure that the element is on the page.
*/
toHaveSelector(selector: string, options?: PageWaitForSelectorOptions): Promise<R>;
* Will ensure that the element is on the page.
*/
toHaveSelector(
selector: string,
options?: PageWaitForSelectorOptions
): Promise<R>
/**
* Will ensure that the element has focus.
*/
toHaveFocus(selector: string, options?: PageWaitForSelectorOptions): Promise<R>;
toHaveFocus(
selector: string,
options?: PageWaitForSelectorOptions
): Promise<R>
/**
* Will assert that N elements with the given selector are on the page and wait for it by default.
* If its 0 elements, then it will throw since the element can't be found.
*/
toHaveSelectorCount(selector: string, count: number, options?: PageWaitForSelectorOptions): Promise<R>;
toHaveSelectorCount(
selector: string,
count: number,
options?: PageWaitForSelectorOptions
): Promise<R>
/**
* Will compare the element's value on the page determined by the selector with the given value.
*/
toEqualValue(selector: string, value: string, options?: PageWaitForSelectorOptions): Promise<R>;
* Will compare the element's value on the page determined by the selector with the given value.
*/
toEqualValue(
selector: string,
value: string,
options?: PageWaitForSelectorOptions
): Promise<R>
/**
* Will compare element's value with the given value.
*/
toEqualValue(value: string, options?: PageWaitForSelectorOptions): Promise<R>;
* Will compare element's value with the given value.
*/
toEqualValue(value: string, options?: PageWaitForSelectorOptions): Promise<R>
/**
* Will assert the given URL with the page's URL
*/
toEqualUrl(value: string): Promise<R>;
* Will assert the given URL with the page's URL
*/
toEqualUrl(value: string): Promise<R>
}

declare global {
namespace jest {
interface Matchers<R> extends PlaywrightMatchers<R> { }
interface Matchers<R> extends PlaywrightMatchers<R> {}
}
}
10 changes: 5 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
preset: 'jest-playwright-preset',
testMatch: ['**/src/**/*.test.ts'],
preset: "jest-playwright-preset",
testMatch: ["**/src/**/*.test.ts"],
collectCoverage: true,
collectCoverageFrom: ["**/src/**/*.ts", "!**/tests/*"],
transform: {
"^.+\\.ts$": "ts-jest"
"^.+\\.ts$": "ts-jest",
},
setupFilesAfterEnv: ["<rootDir>/lib/index.js"]
};
setupFilesAfterEnv: ["<rootDir>/lib/index.js"],
}
19 changes: 19 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"jest-playwright-preset": "^1.5.1",
"playwright-chromium": "^1.11.0",
"playwright-core": "^1.11.0",
"prettier": "^2.3.0",
"ts-jest": "^26.5.6",
"typescript": "^4.2.4"
}
Expand Down
24 changes: 15 additions & 9 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import matchers from './matchers';
import matchers from "./matchers"

import expectPlaywright from "."

describe("expect-playwright", () => {
afterEach(async () => {
await page.setContent('')
await page.setContent("")
})
it("should apply the functions", () => {
for (let matcher in matchers) {
Expand All @@ -24,7 +24,7 @@ describe("expect-playwright", () => {

describe("expectPlaywright", () => {
afterEach(async () => {
await page.setContent('')
await page.setContent("")
})
describe("should be able to handle positive cases", () => {
it("return right result for page and 2 arguments", async () => {
Expand All @@ -33,24 +33,30 @@ describe("expectPlaywright", () => {
})
it("return right result for page and 3 arguments", async () => {
await page.setContent(`<div id="bar">zzzBarzzz</div>`)
expect(await expectPlaywright(page).toHaveText("#bar", "zzzBarzzz")).toBe(true)
expect(await expectPlaywright(page).toHaveText("#bar", "zzzBarzzz")).toBe(
true
)
})
it("return right result for element and 2 arguments", async () => {
await page.setContent(`<div id="foo">zzzFoozzz</div>`)
const elem = await page.$('#foo')
const elem = await page.$("#foo")
expect(await expectPlaywright(elem!).toHaveText("zzzFoozzz")).toBe(true)
})
})
describe("should be able to handle negative cases", () => {
it("return right result for page and 2 arguments", async () => {
await page.setContent(`<div id="foobar">zzzzz</div>`)
await expect(expectPlaywright(page).toHaveText("zzzBarzzz")).rejects.toThrowErrorMatchingSnapshot()
await expect(
expectPlaywright(page).toHaveText("zzzBarzzz")
).rejects.toThrowErrorMatchingSnapshot()
})
it("return right result for page and 4 arguments", async () => {
await page.setContent(`<div id="foo">zzzBarzzz</div>`)
await expect(expectPlaywright(page).toHaveText("#bar", "zzzBarzzz", {
timeout: 1* 1000
})).rejects.toThrowErrorMatchingSnapshot()
await expect(
expectPlaywright(page).toHaveText("#bar", "zzzBarzzz", {
timeout: 1 * 1000,
})
).rejects.toThrowErrorMatchingSnapshot()
})
})
})
Loading

0 comments on commit db7a938

Please sign in to comment.