From db7a93870a2d832b39dac17f6370132daccbb029 Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Wed, 12 May 2021 09:50:24 -0500 Subject: [PATCH] chore: add Prettier (#72) --- .github/workflows/nodejs.yml | 43 +++++----- .prettierignore | 2 + .prettierrc | 3 + README.md | 26 +++--- global.d.ts | 80 ++++++++++++------- jest.config.js | 10 +-- package-lock.json | 19 +++++ package.json | 1 + src/index.test.ts | 24 +++--- src/index.ts | 39 +++++---- src/matchers/index.ts | 14 ++-- src/matchers/tests/utils.ts | 4 +- src/matchers/toEqualText/index.test.ts | 30 ++++--- src/matchers/toEqualText/index.ts | 28 ++++--- src/matchers/toEqualUrl/index.test.ts | 14 ++-- src/matchers/toEqualUrl/index.ts | 18 +++-- src/matchers/toEqualValue/index.test.ts | 24 ++++-- src/matchers/toEqualValue/index.ts | 28 ++++--- src/matchers/toHaveFocus/index.test.ts | 34 +++++--- src/matchers/toHaveFocus/index.ts | 25 +++--- src/matchers/toHaveSelector/index.test.ts | 36 ++++++--- src/matchers/toHaveSelector/index.ts | 20 +++-- .../toHaveSelectorCount/index.test.ts | 22 +++-- src/matchers/toHaveSelectorCount/index.ts | 29 ++++--- src/matchers/toHaveText/index.test.ts | 40 +++++++--- src/matchers/toHaveText/index.ts | 26 ++++-- src/matchers/utils.test.ts | 12 +-- src/matchers/utils.ts | 30 ++++--- tsconfig.json | 6 +- 29 files changed, 438 insertions(+), 249 deletions(-) create mode 100644 .prettierignore create mode 100644 .prettierrc diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 64f64f6..93c0db3 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -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 }} diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..64be67e --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +coverage/ +lib/ diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..cce9d3c --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "semi": false +} diff --git a/README.md b/README.md index 65d4c09..c1f0b8d 100644 --- a/README.md +++ b/README.md @@ -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"] } ``` @@ -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 @@ -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, }) ``` @@ -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, }) ``` @@ -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") ``` @@ -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") ``` @@ -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 () => { @@ -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() }) }) @@ -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 diff --git a/global.d.ts b/global.d.ts index ad64e9b..5cc744d 100644 --- a/global.d.ts +++ b/global.d.ts @@ -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 @@ -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 { /** - * 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; + * 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 /** - * Will check if the element's value includes the given text. - */ - toHaveText(value: string): Promise; + * Will check if the element's value includes the given text. + */ + toHaveText(value: string): Promise /** - * 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; + * 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 /** - * Will compare the element's textContent by the given text. - */ - toEqualText(value: string, options?: PageWaitForSelectorOptions): Promise; + * Will compare the element's textContent by the given text. + */ + toEqualText(value: string, options?: PageWaitForSelectorOptions): Promise /** - * Will ensure that the element is on the page. - */ - toHaveSelector(selector: string, options?: PageWaitForSelectorOptions): Promise; + * Will ensure that the element is on the page. + */ + toHaveSelector( + selector: string, + options?: PageWaitForSelectorOptions + ): Promise /** * Will ensure that the element has focus. */ - toHaveFocus(selector: string, options?: PageWaitForSelectorOptions): Promise; + toHaveFocus( + selector: string, + options?: PageWaitForSelectorOptions + ): Promise /** * 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; + toHaveSelectorCount( + selector: string, + count: number, + options?: PageWaitForSelectorOptions + ): Promise /** - * 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; + * 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 /** - * Will compare element's value with the given value. - */ - toEqualValue(value: string, options?: PageWaitForSelectorOptions): Promise; + * Will compare element's value with the given value. + */ + toEqualValue(value: string, options?: PageWaitForSelectorOptions): Promise /** - * Will assert the given URL with the page's URL - */ - toEqualUrl(value: string): Promise; + * Will assert the given URL with the page's URL + */ + toEqualUrl(value: string): Promise } declare global { namespace jest { - interface Matchers extends PlaywrightMatchers { } + interface Matchers extends PlaywrightMatchers {} } } diff --git a/jest.config.js b/jest.config.js index 5d1d51d..7e0d8d3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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: ["/lib/index.js"] -}; \ No newline at end of file + setupFilesAfterEnv: ["/lib/index.js"], +} diff --git a/package-lock.json b/package-lock.json index edfa0bd..2ccca62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,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" }, @@ -5138,6 +5139,18 @@ "node": ">= 0.8.0" } }, + "node_modules/prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/pretty-format": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-26.6.2.tgz", @@ -11215,6 +11228,12 @@ "integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=", "dev": true }, + "prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true + }, "pretty-format": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-26.6.2.tgz", diff --git a/package.json b/package.json index 7533bbf..94bead0 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/index.test.ts b/src/index.test.ts index 7db94fb..94fe597 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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) { @@ -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 () => { @@ -33,24 +33,30 @@ describe("expectPlaywright", () => { }) it("return right result for page and 3 arguments", async () => { await page.setContent(`
zzzBarzzz
`) - 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(`
zzzFoozzz
`) - 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(`
zzzzz
`) - 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(`
zzzBarzzz
`) - await expect(expectPlaywright(page).toHaveText("#bar", "zzzBarzzz", { - timeout: 1* 1000 - })).rejects.toThrowErrorMatchingSnapshot() + await expect( + expectPlaywright(page).toHaveText("#bar", "zzzBarzzz", { + timeout: 1 * 1000, + }) + ).rejects.toThrowErrorMatchingSnapshot() }) }) }) diff --git a/src/index.ts b/src/index.ts index 8d3f075..f94109d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,24 +1,29 @@ -import matchers from './matchers'; -import type { Page, ElementHandle } from 'playwright-core'; -import type { PlaywrightMatchers } from '../global' +import matchers from "./matchers" +import type { Page, ElementHandle } from "playwright-core" +import type { PlaywrightMatchers } from "../global" // @ts-ignore -if (typeof global.expect !== 'undefined') { +if (typeof global.expect !== "undefined") { // @ts-ignore - global.expect.extend(matchers); + global.expect.extend(matchers) } -const expectWrapper = (pageOrElement: Page | ElementHandle): PlaywrightMatchers => - Object.entries(matchers).reduce((acc, [name, matcher]) => ({ - ...acc, - [name]: async (...args: any[]) => { - // @ts-ignore - const result = await matcher(pageOrElement, ...args) - if (!result.pass) { - throw new Error(result.message()) - } - return true - } - }), {} as PlaywrightMatchers) +const expectWrapper = ( + pageOrElement: Page | ElementHandle +): PlaywrightMatchers => + Object.entries(matchers).reduce( + (acc, [name, matcher]) => ({ + ...acc, + [name]: async (...args: any[]) => { + // @ts-ignore + const result = await matcher(pageOrElement, ...args) + if (!result.pass) { + throw new Error(result.message()) + } + return true + }, + }), + {} as PlaywrightMatchers + ) export = expectWrapper diff --git a/src/matchers/index.ts b/src/matchers/index.ts index 61da8ed..7a2e13c 100644 --- a/src/matchers/index.ts +++ b/src/matchers/index.ts @@ -1,10 +1,10 @@ -import toHaveText from './toHaveText' -import toEqualText from './toEqualText' -import toHaveSelector from './toHaveSelector' -import toEqualValue from './toEqualValue' -import toHaveSelectorCount from './toHaveSelectorCount' -import toEqualUrl from './toEqualUrl' -import toHaveFocus from './toHaveFocus' +import toHaveText from "./toHaveText" +import toEqualText from "./toEqualText" +import toHaveSelector from "./toHaveSelector" +import toEqualValue from "./toEqualValue" +import toHaveSelectorCount from "./toHaveSelectorCount" +import toEqualUrl from "./toEqualUrl" +import toHaveFocus from "./toHaveFocus" export default { toHaveText, diff --git a/src/matchers/tests/utils.ts b/src/matchers/tests/utils.ts index d01ba33..57d3762 100644 --- a/src/matchers/tests/utils.ts +++ b/src/matchers/tests/utils.ts @@ -1,4 +1,4 @@ -import { SyncExpectationResult } from 'expect/build/types' +import { SyncExpectationResult } from "expect/build/types" export const testWrapper = (result: SyncExpectationResult) => { if (result.pass) { @@ -7,4 +7,4 @@ export const testWrapper = (result: SyncExpectationResult) => { return () => { throw new Error(result.message()) } -} \ No newline at end of file +} diff --git a/src/matchers/toEqualText/index.test.ts b/src/matchers/toEqualText/index.test.ts index d75f7e2..edca387 100644 --- a/src/matchers/toEqualText/index.test.ts +++ b/src/matchers/toEqualText/index.test.ts @@ -1,10 +1,10 @@ import { testWrapper } from "../tests/utils" -import toEqualText from '.' +import toEqualText from "." describe("toEqualText", () => { afterEach(async () => { - await page.setContent('') + await page.setContent("") }) describe("selector", () => { it("positive frame", async () => { @@ -22,20 +22,28 @@ describe("toEqualText", () => { }) it("negative", async () => { await page.setContent(`
zzzBarzzz
`) - expect(testWrapper(await toEqualText(page, "#foobar", "not-existing"))).toThrowErrorMatchingSnapshot() + expect( + testWrapper(await toEqualText(page, "#foobar", "not-existing")) + ).toThrowErrorMatchingSnapshot() }) describe("timeout", () => { it("positive: should be able to use a custom timeout", async () => { setTimeout(async () => { await page.setContent(`
Bar
`) }, 500) - expect(testWrapper(await toEqualText(page, "#foobar", "Bar"))).toBe(true) + expect(testWrapper(await toEqualText(page, "#foobar", "Bar"))).toBe( + true + ) }) it("should throw an error after the timeout exceeds", async () => { const start = new Date().getTime() - expect(testWrapper(await toEqualText(page, "#foobar", "Bar", { - timeout: 1 * 1000 - }))).toThrowErrorMatchingSnapshot() + expect( + testWrapper( + await toEqualText(page, "#foobar", "Bar", { + timeout: 1 * 1000, + }) + ) + ).toThrowErrorMatchingSnapshot() const duration = new Date().getTime() - start expect(duration).toBeLessThan(1500) }) @@ -52,7 +60,9 @@ describe("toEqualText", () => { await page.setContent(`
zzzBarzzz
`) const element = await page.$("#foobar") expect(element).not.toBe(null) - expect(testWrapper(await toEqualText(element!, "not-existing"))).toThrowError() + expect( + testWrapper(await toEqualText(element!, "not-existing")) + ).toThrowError() }) }) describe("page", () => { @@ -62,7 +72,9 @@ describe("toEqualText", () => { }) it("negative", async () => { await page.setContent(`
zzzBarzzz
`) - expect(testWrapper(await toEqualText(page, "not-existing"))).toThrowError() + expect( + testWrapper(await toEqualText(page, "not-existing")) + ).toThrowError() }) }) }) diff --git a/src/matchers/toEqualText/index.ts b/src/matchers/toEqualText/index.ts index c537fab..318ff79 100644 --- a/src/matchers/toEqualText/index.ts +++ b/src/matchers/toEqualText/index.ts @@ -1,27 +1,37 @@ -import { SyncExpectationResult } from 'expect/build/types' -import { getElementText, quote, InputArguments } from '../utils' +import { SyncExpectationResult } from "expect/build/types" +import { getElementText, quote, InputArguments } from "../utils" -const toEqualText = async (...args: InputArguments): Promise => { +const toEqualText = async ( + ...args: InputArguments +): Promise => { try { - const { elementHandle, selector, expectedValue } = await getElementText(...args) + const { elementHandle, selector, expectedValue } = await getElementText( + ...args + ) /* istanbul ignore next */ - const actualTextContent = await elementHandle.evaluate((el) => el.textContent) + const actualTextContent = await elementHandle.evaluate( + (el) => el.textContent + ) if (actualTextContent === expectedValue) { return { pass: true, - message: () => `${quote(expectedValue)} does equal ${quote(actualTextContent)}.` + message: () => + `${quote(expectedValue)} does equal ${quote(actualTextContent)}.`, } } return { pass: false, - message: () => `${quote(expectedValue)} does not equal ${quote(actualTextContent)}${selector ? ' of ' + quote(selector) + "." : '.'}` + message: () => + `${quote(expectedValue)} does not equal ${quote(actualTextContent)}${ + selector ? " of " + quote(selector) + "." : "." + }`, } } catch (err) { return { pass: false, - message: () => err.toString() + message: () => err.toString(), } } } -export default toEqualText \ No newline at end of file +export default toEqualText diff --git a/src/matchers/toEqualUrl/index.test.ts b/src/matchers/toEqualUrl/index.test.ts index 650064a..023210b 100644 --- a/src/matchers/toEqualUrl/index.test.ts +++ b/src/matchers/toEqualUrl/index.test.ts @@ -1,13 +1,13 @@ -import toEqualUrl from '.' +import toEqualUrl from "." describe("toEqualUrl", () => { it("should return true if it matches the Url", async () => { - await page.route("**/1.html", route => { + await page.route("**/1.html", (route) => { route.fulfill({ body: "123", headers: { - "Content-Type": "text/html" - } + "Content-Type": "text/html", + }, }) }) const myUrl = "http://i-do-not-exist.com/1.html" @@ -17,12 +17,12 @@ describe("toEqualUrl", () => { expect(result.message()).toMatchSnapshot() }) it("should return false if it does not match the Url", async () => { - await page.route("**/1.html", route => { + await page.route("**/1.html", (route) => { route.fulfill({ body: "123", headers: { - "Content-Type": "text/html" - } + "Content-Type": "text/html", + }, }) }) await page.goto("http://i-do-not-exist.com/1.html") diff --git a/src/matchers/toEqualUrl/index.ts b/src/matchers/toEqualUrl/index.ts index 5c79cbb..de807b7 100644 --- a/src/matchers/toEqualUrl/index.ts +++ b/src/matchers/toEqualUrl/index.ts @@ -1,19 +1,23 @@ -import { SyncExpectationResult } from 'expect/build/types' -import { Page } from 'playwright-core' -import { getElementText, quote } from '../utils' +import { SyncExpectationResult } from "expect/build/types" +import { Page } from "playwright-core" +import { getElementText, quote } from "../utils" -const toEqualUrl = async (page: Page, expectedUrl: string): Promise => { +const toEqualUrl = async ( + page: Page, + expectedUrl: string +): Promise => { const actualUrl = page.url() if (actualUrl === expectedUrl) { return { pass: true, - message: () => `${quote(expectedUrl)} matches the given Url.` + message: () => `${quote(expectedUrl)} matches the given Url.`, } } return { pass: false, - message: () => `${quote(expectedUrl)} expects not the current Url: ${quote(actualUrl)}` + message: () => + `${quote(expectedUrl)} expects not the current Url: ${quote(actualUrl)}`, } } -export default toEqualUrl \ No newline at end of file +export default toEqualUrl diff --git a/src/matchers/toEqualValue/index.test.ts b/src/matchers/toEqualValue/index.test.ts index 3ad21a1..b1d1e54 100644 --- a/src/matchers/toEqualValue/index.test.ts +++ b/src/matchers/toEqualValue/index.test.ts @@ -1,10 +1,10 @@ import { testWrapper } from "../tests/utils" -import toEqualValue from '.' +import toEqualValue from "." describe("toEqualValue", () => { afterEach(async () => { - await page.setContent('') + await page.setContent("") }) describe("selector", () => { it("positive", async () => { @@ -15,7 +15,9 @@ describe("toEqualValue", () => { }) it("negative", async () => { await page.setContent(``) - expect(testWrapper(await toEqualValue(page, "#foobar", "not-existing"))).toThrowErrorMatchingSnapshot() + expect( + testWrapper(await toEqualValue(page, "#foobar", "not-existing")) + ).toThrowErrorMatchingSnapshot() }) }) describe("element", () => { @@ -29,17 +31,23 @@ describe("toEqualValue", () => { await page.setContent(``) const element = await page.$("#foobar") expect(element).not.toBe(null) - expect(testWrapper(await toEqualValue(element!, "not-existing"))).toThrowErrorMatchingSnapshot() + expect( + testWrapper(await toEqualValue(element!, "not-existing")) + ).toThrowErrorMatchingSnapshot() }) }) describe("timeout", () => { it("should throw an error after the timeout exceeds", async () => { const start = new Date().getTime() - expect(testWrapper(await toEqualValue(page, "#foobar", "bar", { - timeout: 1 * 1000 - }))).toThrowErrorMatchingSnapshot() + expect( + testWrapper( + await toEqualValue(page, "#foobar", "bar", { + timeout: 1 * 1000, + }) + ) + ).toThrowErrorMatchingSnapshot() const duration = new Date().getTime() - start expect(duration).toBeLessThan(1500) }) }) -}) \ No newline at end of file +}) diff --git a/src/matchers/toEqualValue/index.ts b/src/matchers/toEqualValue/index.ts index c8e8fb4..7b2c934 100644 --- a/src/matchers/toEqualValue/index.ts +++ b/src/matchers/toEqualValue/index.ts @@ -1,27 +1,37 @@ -import { SyncExpectationResult } from 'expect/build/types' -import { getElementText, quote, InputArguments } from '../utils' +import { SyncExpectationResult } from "expect/build/types" +import { getElementText, quote, InputArguments } from "../utils" -const toEqualValue = async (...args: InputArguments): Promise => { +const toEqualValue = async ( + ...args: InputArguments +): Promise => { try { - const { elementHandle, selector, expectedValue } = await getElementText(...args) + const { elementHandle, selector, expectedValue } = await getElementText( + ...args + ) /* istanbul ignore next */ - const actualTextContent = await elementHandle.evaluate((el) => (el as HTMLInputElement).value) + const actualTextContent = await elementHandle.evaluate( + (el) => (el as HTMLInputElement).value + ) if (actualTextContent?.includes(expectedValue)) { return { pass: true, - message: () => `${quote(expectedValue)} does equal ${quote(actualTextContent)}.` + message: () => + `${quote(expectedValue)} does equal ${quote(actualTextContent)}.`, } } return { pass: false, - message: () => `${quote(expectedValue)} does not equal ${quote(actualTextContent)}${selector ? ' of ' + quote(selector) + "." : '.'}` + message: () => + `${quote(expectedValue)} does not equal ${quote(actualTextContent)}${ + selector ? " of " + quote(selector) + "." : "." + }`, } } catch (err) { return { pass: false, - message: () => err.toString() + message: () => err.toString(), } } } -export default toEqualValue \ No newline at end of file +export default toEqualValue diff --git a/src/matchers/toHaveFocus/index.test.ts b/src/matchers/toHaveFocus/index.test.ts index 3479c81..783d214 100644 --- a/src/matchers/toHaveFocus/index.test.ts +++ b/src/matchers/toHaveFocus/index.test.ts @@ -1,29 +1,29 @@ import { testWrapper } from "../tests/utils" -import toHaveFocus from '.' +import toHaveFocus from "." describe("toHaveFocus", () => { afterEach(async () => { - await page.setContent('') + await page.setContent("") }) it("positive", async () => { await page.setContent(``) - await page.keyboard.press('Tab') + await page.keyboard.press("Tab") const result = await toHaveFocus(page, "#foobar") expect(result.pass).toBe(true) expect(result.message()).toMatchSnapshot() }) it("negative: target element don't have focus", async () => { await page.setContent(``) - await page.keyboard.press('Tab') + await page.keyboard.press("Tab") const result = await toHaveFocus(page, "#bar") expect(testWrapper(result)).toThrowErrorMatchingSnapshot() }) it("negative: target element not found", async () => { await page.setContent(``) - await page.keyboard.press('Tab') + await page.keyboard.press("Tab") const result = await toHaveFocus(page, "#bar", { - timeout: 1 * 1000 + timeout: 1 * 1000, }) expect(testWrapper(result)).toThrowErrorMatchingSnapshot() }) @@ -31,17 +31,25 @@ describe("toHaveFocus", () => { it("positive: should be able to use a custom timeout", async () => { setTimeout(async () => { await page.setContent(``) - await page.keyboard.press('Tab'); + await page.keyboard.press("Tab") }, 500) - expect(testWrapper(await toHaveFocus(page, "#foobar", { - timeout: 1 * 1000 - }))).toBe(true) + expect( + testWrapper( + await toHaveFocus(page, "#foobar", { + timeout: 1 * 1000, + }) + ) + ).toBe(true) }) it("should throw an error after the timeout exceeds", async () => { const start = new Date().getTime() - expect(testWrapper(await toHaveFocus(page, "#foobar", { - timeout: 1 * 1000 - }))).toThrowError() + expect( + testWrapper( + await toHaveFocus(page, "#foobar", { + timeout: 1 * 1000, + }) + ) + ).toThrowError() const duration = new Date().getTime() - start expect(duration).toBeLessThan(1500) }) diff --git a/src/matchers/toHaveFocus/index.ts b/src/matchers/toHaveFocus/index.ts index b1b9af7..7236c1f 100644 --- a/src/matchers/toHaveFocus/index.ts +++ b/src/matchers/toHaveFocus/index.ts @@ -1,27 +1,34 @@ -import { SyncExpectationResult } from 'expect/build/types' -import { quote } from '../utils' -import { Page } from 'playwright-core' -import { PageWaitForSelectorOptions } from '../../../global' +import { SyncExpectationResult } from "expect/build/types" +import { quote } from "../utils" +import { Page } from "playwright-core" +import { PageWaitForSelectorOptions } from "../../../global" -const toHaveFocus = async (page: Page, selector: string, options: PageWaitForSelectorOptions = {}): Promise => { +const toHaveFocus = async ( + page: Page, + selector: string, + options: PageWaitForSelectorOptions = {} +): Promise => { try { await page.waitForSelector(selector, options) /* istanbul ignore next */ - const isFocused = await page.$eval(selector, (el) => el === document.activeElement) + const isFocused = await page.$eval( + selector, + (el) => el === document.activeElement + ) if (isFocused) { return { pass: true, - message: () => `${quote(selector)} has focus on it.` + message: () => `${quote(selector)} has focus on it.`, } } return { pass: false, - message: () => `${quote(selector)} has no focus on it.` + message: () => `${quote(selector)} has no focus on it.`, } } catch (err) { return { pass: false, - message: () => `${quote(selector)} could not be found on the page.` + message: () => `${quote(selector)} could not be found on the page.`, } } } diff --git a/src/matchers/toHaveSelector/index.test.ts b/src/matchers/toHaveSelector/index.test.ts index 2debd20..36a5ab2 100644 --- a/src/matchers/toHaveSelector/index.test.ts +++ b/src/matchers/toHaveSelector/index.test.ts @@ -1,10 +1,10 @@ import { testWrapper } from "../tests/utils" -import toHaveSelector from '.' +import toHaveSelector from "." describe("toHaveSelector", () => { afterEach(async () => { - await page.setContent('') + await page.setContent("") }) it("positive", async () => { await page.setContent(`
Bar
`) @@ -13,26 +13,38 @@ describe("toHaveSelector", () => { expect(result.message()).toMatchSnapshot() }) it("negative", async () => { - expect(testWrapper(await toHaveSelector(page, "#foobar", { - timeout: 1 * 1000 - }))).toThrowError() + expect( + testWrapper( + await toHaveSelector(page, "#foobar", { + timeout: 1 * 1000, + }) + ) + ).toThrowError() }) describe("timeout", () => { it("positive: should be able to use a custom timeout", async () => { setTimeout(async () => { await page.setContent(`
Bar
`) }, 500) - expect(testWrapper(await toHaveSelector(page, "#foobar", { - timeout: 1 * 1000 - }))).toBe(true) + expect( + testWrapper( + await toHaveSelector(page, "#foobar", { + timeout: 1 * 1000, + }) + ) + ).toBe(true) }) it("should throw an error after the timeout exceeds", async () => { const start = new Date().getTime() - expect(testWrapper(await toHaveSelector(page, "#foobar", { - timeout: 1 * 1000 - }))).toThrowError() + expect( + testWrapper( + await toHaveSelector(page, "#foobar", { + timeout: 1 * 1000, + }) + ) + ).toThrowError() const duration = new Date().getTime() - start expect(duration).toBeLessThan(1500) }) }) -}) \ No newline at end of file +}) diff --git a/src/matchers/toHaveSelector/index.ts b/src/matchers/toHaveSelector/index.ts index 2ac4069..05654e4 100644 --- a/src/matchers/toHaveSelector/index.ts +++ b/src/matchers/toHaveSelector/index.ts @@ -1,21 +1,25 @@ -import { SyncExpectationResult } from 'expect/build/types' -import { quote } from '../utils' -import { Page } from 'playwright-core' -import { PageWaitForSelectorOptions } from '../../../global' +import { SyncExpectationResult } from "expect/build/types" +import { quote } from "../utils" +import { Page } from "playwright-core" +import { PageWaitForSelectorOptions } from "../../../global" -const toHaveSelector = async (page: Page, selector: string, options: PageWaitForSelectorOptions = {}): Promise => { +const toHaveSelector = async ( + page: Page, + selector: string, + options: PageWaitForSelectorOptions = {} +): Promise => { try { await page.waitForSelector(selector, options) return { pass: true, - message: () => `${quote(selector)} was found on the page.` + message: () => `${quote(selector)} was found on the page.`, } } catch (err) { return { pass: false, - message: () => `${quote(selector)} could not be found on the page.` + message: () => `${quote(selector)} could not be found on the page.`, } } } -export default toHaveSelector \ No newline at end of file +export default toHaveSelector diff --git a/src/matchers/toHaveSelectorCount/index.test.ts b/src/matchers/toHaveSelectorCount/index.test.ts index 3374153..57324bf 100644 --- a/src/matchers/toHaveSelectorCount/index.test.ts +++ b/src/matchers/toHaveSelectorCount/index.test.ts @@ -1,29 +1,37 @@ import { testWrapper } from "../tests/utils" -import toHaveSelectorCount from '.' +import toHaveSelectorCount from "." describe("toHaveSelectorCount", () => { afterEach(async () => { - await page.setContent('') + await page.setContent("") }) describe("selector", () => { it("positive", async () => { - await page.setContent(`
Bar
`) + await page.setContent( + `
Bar
` + ) const result = await toHaveSelectorCount(page, ".foobar", 2) expect(result.pass).toBe(true) expect(result.message()).toMatchSnapshot() }) it("negative", async () => { await page.setContent(`
Bar
`) - expect(testWrapper(await toHaveSelectorCount(page, ".foobar", 2))).toThrowErrorMatchingSnapshot() + expect( + testWrapper(await toHaveSelectorCount(page, ".foobar", 2)) + ).toThrowErrorMatchingSnapshot() }) }) describe("timeout", () => { it("should throw an error after the timeout exceeds", async () => { const start = new Date().getTime() - expect(testWrapper(await toHaveSelectorCount(page, ".foobar", 1, { - timeout: 1 * 1000 - }))).toThrowErrorMatchingSnapshot() + expect( + testWrapper( + await toHaveSelectorCount(page, ".foobar", 1, { + timeout: 1 * 1000, + }) + ) + ).toThrowErrorMatchingSnapshot() const duration = new Date().getTime() - start expect(duration).toBeLessThan(1500) }) diff --git a/src/matchers/toHaveSelectorCount/index.ts b/src/matchers/toHaveSelectorCount/index.ts index f035ea9..7bf62ac 100644 --- a/src/matchers/toHaveSelectorCount/index.ts +++ b/src/matchers/toHaveSelectorCount/index.ts @@ -1,27 +1,36 @@ -import { SyncExpectationResult } from 'expect/build/types' -import { quote } from '../utils' -import { Page } from 'playwright-core' -import {PageWaitForSelectorOptions} from "../../../global"; +import { SyncExpectationResult } from "expect/build/types" +import { quote } from "../utils" +import { Page } from "playwright-core" +import { PageWaitForSelectorOptions } from "../../../global" -const toHaveSelectorCount = async (page: Page, selector: string, expectedValue: number, options: PageWaitForSelectorOptions = {}): Promise => { +const toHaveSelectorCount = async ( + page: Page, + selector: string, + expectedValue: number, + options: PageWaitForSelectorOptions = {} +): Promise => { try { - await page.waitForSelector(selector, {state: 'attached', ...options}) + await page.waitForSelector(selector, { state: "attached", ...options }) /* istanbul ignore next */ - const actualCount = await page.$$eval(selector, el => el.length) + const actualCount = await page.$$eval(selector, (el) => el.length) if (actualCount === expectedValue) { return { pass: true, - message: () => `${quote(`${expectedValue}`)} does equal ${quote(`${actualCount}`)}.` + message: () => + `${quote(`${expectedValue}`)} does equal ${quote(`${actualCount}`)}.`, } } return { pass: false, - message: () => `${quote(`${expectedValue}`)} does not equal ${quote(`${actualCount}`)}${selector ? ' of ' + quote(selector) + "." : '.'}` + message: () => + `${quote(`${expectedValue}`)} does not equal ${quote( + `${actualCount}` + )}${selector ? " of " + quote(selector) + "." : "."}`, } } catch (err) { return { pass: false, - message: () => `${quote(selector)} could not be found on the page.` + message: () => `${quote(selector)} could not be found on the page.`, } } } diff --git a/src/matchers/toHaveText/index.test.ts b/src/matchers/toHaveText/index.test.ts index 967f55f..3f92ff3 100644 --- a/src/matchers/toHaveText/index.test.ts +++ b/src/matchers/toHaveText/index.test.ts @@ -1,10 +1,10 @@ import { testWrapper } from "../tests/utils" -import toHaveText from '.' +import toHaveText from "." describe("toHaveText", () => { afterEach(async () => { - await page.setContent('') + await page.setContent("") }) describe("selector", () => { it("positive frame", async () => { @@ -16,7 +16,9 @@ describe("toHaveText", () => { }) it("empty positive with page element", async () => { await page.setContent(`
`) - const result = await toHaveText(page, "#foobar", "", { state: 'attached' }) + const result = await toHaveText(page, "#foobar", "", { + state: "attached", + }) expect(result.pass).toBe(true) expect(result.message()).toMatchSnapshot() }) @@ -36,7 +38,9 @@ describe("toHaveText", () => { }) it("negative", async () => { await page.setContent(`
zzzBarzzz
`) - expect(testWrapper(await toHaveText(page, "#foobar", "not-existing"))).toThrowErrorMatchingSnapshot() + expect( + testWrapper(await toHaveText(page, "#foobar", "not-existing")) + ).toThrowErrorMatchingSnapshot() }) }) describe("element", () => { @@ -50,7 +54,9 @@ describe("toHaveText", () => { await page.setContent(`
zzzBarzzz
`) const element = await page.$("#foobar") expect(element).not.toBe(null) - expect(testWrapper(await toHaveText(element!, "not-existing"))).toThrowErrorMatchingSnapshot() + expect( + testWrapper(await toHaveText(element!, "not-existing")) + ).toThrowErrorMatchingSnapshot() }) }) describe("page", () => { @@ -60,14 +66,20 @@ describe("toHaveText", () => { }) it("negative", async () => { await page.setContent(`
zzzBarzzz
`) - expect(testWrapper(await toHaveText(page, "not-existing"))).toThrowErrorMatchingSnapshot() + expect( + testWrapper(await toHaveText(page, "not-existing")) + ).toThrowErrorMatchingSnapshot() }) it("should be able to overwrite timeout", async () => { await page.setContent(`
zzzBarzzz
`) const start = new Date().getTime() - expect(testWrapper(await toHaveText(page, "not-existing", { - timeout: 2000 - }))).toThrowErrorMatchingSnapshot() + expect( + testWrapper( + await toHaveText(page, "not-existing", { + timeout: 2000, + }) + ) + ).toThrowErrorMatchingSnapshot() const duration = new Date().getTime() - start expect(duration).toBeGreaterThan(2000) }) @@ -75,9 +87,13 @@ describe("toHaveText", () => { describe("timeout", () => { it("should throw an error after the timeout exceeds", async () => { const start = new Date().getTime() - expect(testWrapper(await toHaveText(page, "#foobar", "bar", { - timeout: 1 * 1000 - }))).toThrowErrorMatchingSnapshot() + expect( + testWrapper( + await toHaveText(page, "#foobar", "bar", { + timeout: 1 * 1000, + }) + ) + ).toThrowErrorMatchingSnapshot() const duration = new Date().getTime() - start expect(duration).toBeLessThan(1500) }) diff --git a/src/matchers/toHaveText/index.ts b/src/matchers/toHaveText/index.ts index e3c11b4..57a3138 100644 --- a/src/matchers/toHaveText/index.ts +++ b/src/matchers/toHaveText/index.ts @@ -1,25 +1,35 @@ -import { SyncExpectationResult } from 'expect/build/types' -import { getElementText, quote, InputArguments } from '../utils' +import { SyncExpectationResult } from "expect/build/types" +import { getElementText, quote, InputArguments } from "../utils" -const toHaveText = async (...args: InputArguments): Promise => { +const toHaveText = async ( + ...args: InputArguments +): Promise => { try { - const { elementHandle, selector, expectedValue } = await getElementText(...args) + const { elementHandle, selector, expectedValue } = await getElementText( + ...args + ) /* istanbul ignore next */ - const actualTextContent = await elementHandle.evaluate((el) => el.textContent) + const actualTextContent = await elementHandle.evaluate( + (el) => el.textContent + ) if (actualTextContent?.includes(expectedValue)) { return { pass: true, - message: () => `${quote(expectedValue)} is included in ${quote(actualTextContent)}.` + message: () => + `${quote(expectedValue)} is included in ${quote(actualTextContent)}.`, } } return { pass: false, - message: () => `${quote(expectedValue)} is not included in ${quote(actualTextContent)}${selector ? ' of ' + quote(selector) + "." : '.'}` + message: () => + `${quote(expectedValue)} is not included in ${quote( + actualTextContent + )}${selector ? " of " + quote(selector) + "." : "."}`, } } catch (err) { return { pass: false, - message: () => err.toString() + message: () => err.toString(), } } } diff --git a/src/matchers/utils.test.ts b/src/matchers/utils.test.ts index 929e928..1251b44 100644 --- a/src/matchers/utils.test.ts +++ b/src/matchers/utils.test.ts @@ -1,13 +1,15 @@ -import { getElementText } from './utils' +import { getElementText } from "./utils" describe("utils.getElementText", () => { it("should throw an error if the specified expect element was not recognized", async () => { - class Test123 { } - // @ts-ignore - await expect(getElementText(new Test123(), "")).rejects.toThrowErrorMatchingSnapshot() + class Test123 {} + await expect( + // @ts-ignore + getElementText(new Test123(), "") + ).rejects.toThrowErrorMatchingSnapshot() }) it("should throw an error if the input length was not in range", async () => { // @ts-ignore await expect(getElementText()).rejects.toThrowErrorMatchingSnapshot() }) -}) \ No newline at end of file +}) diff --git a/src/matchers/utils.ts b/src/matchers/utils.ts index 9903751..9a774a3 100644 --- a/src/matchers/utils.ts +++ b/src/matchers/utils.ts @@ -26,9 +26,17 @@ interface getElementTextReturn { expectedValue: string } -export type InputArguments = [Page | ElementHandle, string?, (string | PageWaitForSelectorOptions)?, PageWaitForSelectorOptions?] +export type InputArguments = [ + Page | ElementHandle, + string?, + (string | PageWaitForSelectorOptions)?, + PageWaitForSelectorOptions? +] -const lastElementHasType = (args: InputArguments, type: "string" | "object"): boolean => typeof args[args.length - 1] === type +const lastElementHasType = ( + args: InputArguments, + type: "string" | "object" +): boolean => typeof args[args.length - 1] === type const getSelectorOptions = (args: InputArguments) => { let selectorOptions: PageWaitForSelectorOptions | undefined = undefined @@ -41,7 +49,9 @@ const getSelectorOptions = (args: InputArguments) => { return selectorOptions } -export const getElementText = async (...args: InputArguments): Promise => { +export const getElementText = async ( + ...args: InputArguments +): Promise => { if (args.length > 1) { const type = detectExpectType(args[0]) /** @@ -56,13 +66,13 @@ export const getElementText = async (...args: InputArguments): Promise