Skip to content

Commit

Permalink
feta: simplify API
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

- `mainPage` is renamed into `page`
- "/globalSetup" => "/setup"
- "/globalTeardown" => "/teardown"
- "/testEnvironment" => ""
  • Loading branch information
gregberge committed Mar 3, 2018
1 parent 958db95 commit 895d1b7
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 23 deletions.
5 changes: 2 additions & 3 deletions packages/jest-environment-puppeteer/.npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
!/lib/*.js
!index.js
!globalSetup.js
!globalTeardown.js
!testEnvironment.js
!setup.js
!teardown.js
34 changes: 27 additions & 7 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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()
}
```

Expand All @@ -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).

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment-puppeteer/index.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions packages/jest-environment-puppeteer/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
globalSetup: './globalSetup.js',
globalTeardown: './globalTeardown.js',
testEnvironment: './testEnvironment.js',
globalSetup: './setup',
globalTeardown: './teardown',
testEnvironment: './',
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/jest-environment-puppeteer/testEnvironment.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/jest-environment-puppeteer/tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
browser: true,
},
globals: {
mainPage: true,
page: true,
browser: true,
},
}
4 changes: 2 additions & 2 deletions packages/jest-environment-puppeteer/tests/google.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 895d1b7

Please sign in to comment.