Skip to content

Commit

Permalink
Add tests and polish some bits
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlened committed Sep 24, 2018
1 parent 1c1d3dd commit dcbb08e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ Other options are documented in [jest-dev-server](https://github.com/smooth-code

### Configure Puppeteer

Jest Puppeteer automatically detects the best config to start Puppeteer but sometimes you may need to specify custom options. All Puppeteer [launch](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) or [connect](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) options can be specified in `jest-puppeteer.config.js` at the root of the project. Since it is JavaScript, you can use all the stuff that you need, including environment.
Jest Puppeteer automatically detects the best config to start Puppeteer but sometimes you may need to specify custom options. All Puppeteer [launch](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) or [connect](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) options can be specified in `jest-puppeteer.config.js` at the root of the project. Since it is JavaScript, you can use all the stuff you need, including environment.

The browser context can be also specified - `default` or `incognito` - if you want more isolation between running instances. More information available in [jest-puppeteer-environment readme](https://github.com/smooth-code/jest-puppeteer/blob/master/packages/jest-environment-puppeteer/README.md)
The browser context can be also specified. By default, the browser context is shared. `incognito` is available if you want more isolation between running instances. More information available in [jest-puppeteer-environment readme](https://github.com/smooth-code/jest-puppeteer/blob/master/packages/jest-environment-puppeteer/README.md)

```js
// jest-puppeteer.config.js
Expand Down Expand Up @@ -234,6 +234,10 @@ it('should fill an input', async () => {
})
```

### `global.context`

Give access to a [browser context](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-browsercontext) that is instantiated when the browser is launched. You can control whether each test has its own isolated browser context using the `browserContext` option in your `jest-puppeteer.config.js`.

### `global.expect(page)`

Helper to make Puppeteer assertions, [see documentation](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/expect-puppeteer/README.md#api).
Expand Down
2 changes: 1 addition & 1 deletion jest-puppeteer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
launch: {
headless: process.env.CI === 'true',
},
browserContext: 'default',
browserContext: process.env.INCOGNITO ? 'incognito' : 'default',
server: {
command: `PORT=${port} node server`,
port,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
],
"scripts": {
"build": "lerna run build",
"ci": "yarn build && yarn lint && yarn test --ci",
"ci": "yarn build && yarn lint && yarn test --ci && yarn test:incognito --ci",
"dev": "lerna run build --parallel -- --watch",
"format": "prettier --write \"packages/**/*.{js,json,md}\" \"*.{js,json,md}\"",
"lint": "eslint .",
"release": "lerna publish --conventional-commits && conventional-github-releaser --preset angular",
"test": "jest --runInBand"
"test": "jest --runInBand",
"test:incognito": "INCOGNITO=true jest --runInBand"
},
"devDependencies": {
"@babel/cli": "^7.1.0",
Expand Down
1 change: 0 additions & 1 deletion packages/expect-puppeteer/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const getDefaultOptions = () => {
(global.puppeteerConfig.launch && global.puppeteerConfig.launch.slowMo) ||
(global.puppeteerConfig.connect && global.puppeteerConfig.connect.slowMo)
) &&
global.puppeteerConfig.browserContext &&
defaultOptionsValue &&
defaultOptionsValue.timeout
) {
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ it('should fill an input', async () => {

### `global.context`

Give access to a [Browser context](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-browsercontext) that is instanciated when the browser is launched.

It is possible to set the browser context inside the config `jest-puppeteer-config.js` in the root of this project. The context will always be exposed via the global `context` object the same way `page` and `browser` are exposed.
Give access to a [browser context](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-browsercontext) that is instantiated when the browser is launched. You can control whether each test has its own isolated browser context using the `browserContext` option in your `jest-puppeteer.config.js`.

### `global.jestPuppeteer.debug()`

Expand All @@ -85,9 +83,9 @@ You can specify a `jest-puppeteer.config.js` at the root of the project or defin

- `launch` <[object]> [All Puppeteer launch options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.
- `connect` <[object]> [All Puppeteer connect options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) can be specified in config. This is an alternative to `launch` config, allowing you to connect to an already running instance of Chrome.
- `browserContext` <[string]>. Defaults to `default` but can have the following values (any other will throw an error on test suite setup):
- `default` Default valDefault behavior, the browser will have one instance where all tabs share the same context.
- `incognito` Each instance to have a separate, isolated context. Useful when running tests that could interfere with one another. (*Example: testing multiple users on the same app at once with login, transactions, etc.*)
- `browserContext` <[string]>. By default, the browser context (cookies, localStorage, etc) is shared between all tests. The following options are available for `browserContext`:
- `default` Each test starts a tab, so all tests share the same context.
- `incognito` Each tests starts an incognito window, so all tests have a separate, isolated context. Useful when running tests that could interfere with one another. (*Example: testing multiple users on the same app at once with login, transactions, etc.*)
- `exitOnPageError` <[boolean]> Exits page on any global error message thrown. Defaults to `true`.
- `server` <[Object]> Server options allowed by [jest-dev-server](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class PuppeteerEnvironment extends NodeEnvironment {
browserWSEndpoint: wsEndpoint
})

if(config.browserContext === 'incognito') {
if (config.browserContext === 'incognito') {
// Using this, pages will be created in a pristine context.
this.global.context = await this.global.browser.createIncognitoBrowserContext()
} else if(config.browserContext === 'default') {
} else if (config.browserContext === 'default' || !config.browserContext) {
/**
* Since this is a new browser, browserContexts() will return only one instance
* https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserbrowsercontexts
Expand Down Expand Up @@ -105,7 +105,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
const config = await readConfig()
this.global.page.removeListener('pageerror', handleError)

if(config.browserContext === 'incognito') {
if (config.browserContext === 'incognito') {
await this.global.context.close()
} else {
await this.global.page.close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('browserContext', () => {
const test = process.env.INCOGNITO ? it : it.skip
test('incognito should isolate cookies (part 1)', async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
expect(await page.cookies()).toEqual([])
await page.setCookie({ name: 'isolation-test', value: 'dummy' })
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('browserContext', () => {
const test = process.env.INCOGNITO ? it : it.skip
test('incognito should isolate cookies (part 2)', async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
expect(await page.cookies()).toEqual([])
await page.setCookie({ name: 'isolation-test', value: 'dummy' })
})
})

0 comments on commit dcbb08e

Please sign in to comment.