From 895d1b7f0f02b3811a4cc114dfe4c89f9bb04aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Sat, 3 Mar 2018 10:31:40 +0100 Subject: [PATCH] feta: simplify API BREAKING CHANGE: - `mainPage` is renamed into `page` - "/globalSetup" => "/setup" - "/globalTeardown" => "/teardown" - "/testEnvironment" => "" --- .../jest-environment-puppeteer/.npmignore | 5 ++- packages/jest-environment-puppeteer/README.md | 34 +++++++++++++++---- packages/jest-environment-puppeteer/index.js | 4 +-- .../jest-environment-puppeteer/jest.config.js | 6 ++-- .../{globalSetup.js => setup.js} | 0 .../src/PuppeteerEnvironment.js | 8 ++--- .../{globalTeardown.js => teardown.js} | 0 .../testEnvironment.js | 1 - .../tests/.eslintrc.js | 2 +- .../tests/google.test.js | 4 +-- 10 files changed, 41 insertions(+), 23 deletions(-) rename packages/jest-environment-puppeteer/{globalSetup.js => setup.js} (100%) rename packages/jest-environment-puppeteer/{globalTeardown.js => teardown.js} (100%) delete mode 100644 packages/jest-environment-puppeteer/testEnvironment.js diff --git a/packages/jest-environment-puppeteer/.npmignore b/packages/jest-environment-puppeteer/.npmignore index d820b72e..4f576a3a 100644 --- a/packages/jest-environment-puppeteer/.npmignore +++ b/packages/jest-environment-puppeteer/.npmignore @@ -1,6 +1,5 @@ /* !/lib/*.js !index.js -!globalSetup.js -!globalTeardown.js -!testEnvironment.js +!setup.js +!teardown.js diff --git a/packages/jest-environment-puppeteer/README.md b/packages/jest-environment-puppeteer/README.md index e681d88c..19cf7808 100644 --- a/packages/jest-environment-puppeteer/README.md +++ b/packages/jest-environment-puppeteer/README.md @@ -16,9 +16,9 @@ Update your Jest configuration: ```json { - "globalSetup": "jest-environment-puppeteer/globalSetup", - "globalTeardown": "jest-environment-puppeteer/globalTeardown", - "testEnvironment": "jest-environment-puppeteer/testEnvironment" + "globalSetup": "jest-environment-puppeteer/setup", + "globalTeardown": "jest-environment-puppeteer/teardown", + "testEnvironment": "jest-environment-puppeteer" } ``` @@ -57,6 +57,23 @@ module.exports = { } ``` +### Configure ESLint + +Jest Environment Puppeteer exposes two new globals: `browser` and `page`. If you want to avoid errors, you can add them to your `.eslintrc.js`: + +```js +// .eslintrc.js +module.exports = { + env: { + jest: true, + }, + globals: { + page: true, + browser: true, + }, +} +``` + ### Extend PuppeteerEnvironment Sometimes you want to use your own environment, to do that you can extend `PuppeteerEnvironment`. @@ -84,16 +101,19 @@ module.exports = CustomEnvironment It is possible to access `globalSetup` or `globalTeardown` in your scripts. ```js -import { globalSetup, globalTeardown } from 'jest-environment-puppeteer' +const { + setup: setupPuppeteer, + teardown: teardownPuppeteer, +} = require('jest-environment-puppeteer') async function setup() { - await globalSetup() + await setupPuppeteer() // ... } async function teardown() { // ... - await globalTeardown() + await teardownPuppeteer() } ``` @@ -110,7 +130,7 @@ it('should open a new page', async () => { }) ``` -### `global.mainPage` +### `global.page` Give access to a [Puppeteer Page](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-page) opened at start (you will use it most of time). diff --git a/packages/jest-environment-puppeteer/index.js b/packages/jest-environment-puppeteer/index.js index 769ea52f..42740689 100644 --- a/packages/jest-environment-puppeteer/index.js +++ b/packages/jest-environment-puppeteer/index.js @@ -1,3 +1,3 @@ module.exports = require('./lib/PuppeteerEnvironment').default -module.exports.globalSetup = require('./lib/global').setup -module.exports.globalTeardown = require('./lib/global').teardown +module.exports.setup = require('./lib/global').setup +module.exports.teardown = require('./lib/global').teardown diff --git a/packages/jest-environment-puppeteer/jest.config.js b/packages/jest-environment-puppeteer/jest.config.js index ba96533e..5a6305a8 100644 --- a/packages/jest-environment-puppeteer/jest.config.js +++ b/packages/jest-environment-puppeteer/jest.config.js @@ -1,5 +1,5 @@ module.exports = { - globalSetup: './globalSetup.js', - globalTeardown: './globalTeardown.js', - testEnvironment: './testEnvironment.js', + globalSetup: './setup', + globalTeardown: './teardown', + testEnvironment: './', } diff --git a/packages/jest-environment-puppeteer/globalSetup.js b/packages/jest-environment-puppeteer/setup.js similarity index 100% rename from packages/jest-environment-puppeteer/globalSetup.js rename to packages/jest-environment-puppeteer/setup.js diff --git a/packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js b/packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js index 9f2589f8..bbf0e513 100644 --- a/packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js +++ b/packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js @@ -16,13 +16,13 @@ class PuppeteerEnvironment extends NodeEnvironment { this.global.browser = await puppeteer.connect({ browserWSEndpoint: wsEndpoint, }) - this.global.mainPage = await this.global.browser.newPage() - this.global.mainPage.addListener('pageerror', handleError) + this.global.page = await this.global.browser.newPage() + this.global.page.addListener('pageerror', handleError) } async teardown() { - this.global.mainPage.removeListener('pageerror', handleError) - await this.global.mainPage.close() + this.global.page.removeListener('pageerror', handleError) + await this.global.page.close() } } diff --git a/packages/jest-environment-puppeteer/globalTeardown.js b/packages/jest-environment-puppeteer/teardown.js similarity index 100% rename from packages/jest-environment-puppeteer/globalTeardown.js rename to packages/jest-environment-puppeteer/teardown.js diff --git a/packages/jest-environment-puppeteer/testEnvironment.js b/packages/jest-environment-puppeteer/testEnvironment.js deleted file mode 100644 index cf36aad6..00000000 --- a/packages/jest-environment-puppeteer/testEnvironment.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./lib/PuppeteerEnvironment').default diff --git a/packages/jest-environment-puppeteer/tests/.eslintrc.js b/packages/jest-environment-puppeteer/tests/.eslintrc.js index ed08f7bc..81503bf7 100644 --- a/packages/jest-environment-puppeteer/tests/.eslintrc.js +++ b/packages/jest-environment-puppeteer/tests/.eslintrc.js @@ -4,7 +4,7 @@ module.exports = { browser: true, }, globals: { - mainPage: true, + page: true, browser: true, }, } diff --git a/packages/jest-environment-puppeteer/tests/google.test.js b/packages/jest-environment-puppeteer/tests/google.test.js index 8bc53a92..6992bea0 100644 --- a/packages/jest-environment-puppeteer/tests/google.test.js +++ b/packages/jest-environment-puppeteer/tests/google.test.js @@ -1,10 +1,10 @@ describe('Google', () => { beforeAll(async () => { - await mainPage.goto('https://google.com') + await page.goto('https://google.com') }) it('should display "google" text on page', async () => { - const text = await mainPage.evaluate(() => document.body.textContent) + const text = await page.evaluate(() => document.body.textContent) expect(text).toContain('google') }) })