Skip to content

Commit

Permalink
feat: integrate server launch
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Puppeteer launch options are now specified under `launch` object
  • Loading branch information
gregberge committed Mar 5, 2018
1 parent e04a172 commit dbea571
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 55 deletions.
5 changes: 3 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"presets": [
[
"env",
"babel-preset-env",
{
"targets": {
"node": "8"
},
"loose": true
}
]
]
],
"plugins": ["transform-object-rest-spread"]
}
62 changes: 55 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Google', () => {

Writing integration test can be done using [Puppeteer API](<(https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md)>) but it can be complicated and hard because API is not designed for testing.

To make it simpler, an `expectPage()` is automatically installed and available, it provides a lot of convenient methods, all documented in [expect-puppeteer API](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/expect-puppeteer#api).
To make it simpler, an `expectPage()` is automatically installed and available, it provides a lot of convenient methods, all documented in [expect-puppeteer API](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/expect-puppeteer/README.md#api).

Some examples:

Expand Down Expand Up @@ -68,17 +68,33 @@ await expectPage().toFillForm('form[name="myForm"]', {
})
```

### Configure Puppeteer
### Start a server

Jest Puppeteer integrates a functionality to run start a server when your test suite is started. It automatically close the server when tests are done.

To use it, specify a server section in your `jest-puppeteer.config.js`.

```js
// jest-puppeteer.config.js
module.exports = {
server: {
command: 'node server.js',
port: 4444,
},
}
```

Jest Puppeteer automatically detect the best config to start Puppeteer but sometimes you may need to specify custom options.
### Configure Puppeteer

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

```js
// jest-puppeteer.config.js
module.exports = {
dumpio: true,
headless: process.env.HEADLESS !== 'false',
launch: {
dumpio: true,
headless: process.env.HEADLESS !== 'false',
},
}
```

Expand Down Expand Up @@ -168,13 +184,37 @@ it('should fill an input', async () => {

### `global.expectPage`

Helper to make Puppeteer assertions, [see documentation](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/expect-puppeteer#api).
Helper to make Puppeteer assertions, [see documentation](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/expect-puppeteer/README.md#api).

```js
await expectPage().toMatch('A text in the page')
// ...
```

### `jest-puppeteer.config.js`

You can specify a `jest-puppeteer.config.js` at the root of the project.

* `launch` <[object]> [All Puppeteer launch options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) can be specified in `jest-puppeteer.config.js` at the root of the project. Since it is JavaScript, you can use all stuff you need, including environment.
* `server` <[Object]> Server options
* `command` <[string]> Command to start server
* `port` <[number]> If specified, it will wait port to be listened
* `options` <[Object]> Optional options for [spawnd](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/spawnd/README.md)

```js
// jest-puppeteer.config.js
module.exports = {
launch: {
dumpio: true,
headless: process.env.HEADLESS !== 'false',
},
server: {
command: 'node server.js',
port: 4444,
},
}
```

## Inspiration

Thanks to Fumihiro Xue for his great [Jest example](https://github.com/xfumihiro/jest-puppeteer-example).
Expand All @@ -189,3 +229,11 @@ MIT
[package]: https://www.npmjs.com/package/jest-environment-puppeteer
[license-badge]: https://img.shields.io/npm/l/jest-environment-puppeteer.svg?style=flat-square
[license]: https://github.com/smooth-code/jest-puppeteer/blob/master/LICENSE
[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 'Array'
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type 'Boolean'
[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function 'Function'
[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type 'Number'
[object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 'Object'
[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 'Promise'
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type 'String'
[error]: https://nodejs.org/api/errors.html#errors_class_error 'Error'
8 changes: 7 additions & 1 deletion jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module.exports = {
headless: process.env.CI === 'true',
launch: {
headless: process.env.CI === 'true',
},
server: {
command: 'node server',
port: 4444,
},
}
4 changes: 1 addition & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = {
preset: './jest-puppeteer-preset',
globalSetup: './jestConfig/globalSetup',
globalTeardown: './jestConfig/globalTeardown',
preset: '<rootDir>/packages/jest-puppeteer-preset',
}
15 changes: 0 additions & 15 deletions jestConfig/globalSetup.js

This file was deleted.

8 changes: 0 additions & 8 deletions jestConfig/globalTeardown.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"conventional-github-releaser": "^2.0.0",
"eslint": "^4.18.2",
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-environment-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
},
"dependencies": {
"cwd": "^0.10.0",
"lodash": "^4.17.5",
"mkdirp": "^0.5.1",
"rimraf": "^2.6.2"
"rimraf": "^2.6.2",
"spawnd": "^1.1.1",
"wait-port": "^0.2.2"
}
}
20 changes: 19 additions & 1 deletion packages/jest-environment-puppeteer/src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@ import fs from 'fs'
import mkdirp from 'mkdirp'
import rimraf from 'rimraf'
import puppeteer from 'puppeteer'
import spawnd from 'spawnd'
import cwd from 'cwd'
import waitPort from 'wait-port'
import readConfig from './readConfig'
import { DIR, WS_ENDPOINT_PATH } from './constants'

let browser
let server

export async function setup() {
const config = await readConfig()
browser = await puppeteer.launch(config)
browser = await puppeteer.launch(config.launch)
mkdirp.sync(DIR)
fs.writeFileSync(WS_ENDPOINT_PATH, browser.wsEndpoint())

if (config.server) {
server = spawnd(config.server.command, {
shell: true,
env: process.env,
cwd: cwd(),
...config.server.options,
})

if (config.server.port) {
await waitPort({ port: config.server.port, output: 'silent' })
}
}
}

export async function teardown() {
if (server) await server.destroy()
await browser.close()
rimraf.sync(DIR)
}
11 changes: 8 additions & 3 deletions packages/jest-environment-puppeteer/src/readConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import cwd from 'cwd'
import { merge } from 'lodash'

const exists = promisify(fs.exists)

const CONFIG_PATH = path.join(cwd(), 'jest-puppeteer.config.js')
const DEFAULT_CONFIG = {}
const DEFAULT_CONFIG = {
launch: {},
}
const DEFAULT_CONFIG_CI = {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
launch: {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
},
}

async function readConfig() {
Expand All @@ -19,7 +24,7 @@ async function readConfig() {

// eslint-disable-next-line global-require, import/no-dynamic-require
const localConfig = require(CONFIG_PATH)
return Object.assign({}, defaultConfig, localConfig)
return merge({}, defaultConfig, localConfig)
}

export default readConfig
10 changes: 10 additions & 0 deletions packages/jest-environment-puppeteer/tests/basic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
describe('Basic', () => {
beforeAll(async () => {
await page.goto('http://localhost:4444')
})

it('should display "This is home!" text on page', async () => {
const text = await page.evaluate(() => document.body.textContent)
expect(text).toContain('This is home!')
})
})
10 changes: 0 additions & 10 deletions packages/jest-environment-puppeteer/tests/google.test.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/spawnd/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function spawnd(command, options) {
cleanExit(typeof code === 'number' ? code : 1)
})

proc.destroy = () => {
proc.destroy = async () => {
removeExitHandler()
proc.removeAllListeners('exit')
proc.removeAllListeners('error')
pterminate(proc.pid).catch(() => {
return pterminate(proc.pid).catch(() => {
/* ignore error */
})
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 9 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"

babel-plugin-syntax-object-rest-spread@^6.13.0:
babel-plugin-syntax-object-rest-spread@^6.13.0, babel-plugin-syntax-object-rest-spread@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"

Expand Down Expand Up @@ -710,6 +710,13 @@ babel-plugin-transform-exponentiation-operator@^6.22.0:
babel-plugin-syntax-exponentiation-operator "^6.8.0"
babel-runtime "^6.22.0"

babel-plugin-transform-object-rest-spread@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06"
dependencies:
babel-plugin-syntax-object-rest-spread "^6.8.0"
babel-runtime "^6.26.0"

babel-plugin-transform-regenerator@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz#e0703696fbde27f0a3efcacf8b4dca2f7b3a8f2f"
Expand Down Expand Up @@ -3362,7 +3369,7 @@ lodash.templatesettings@^4.0.0:
dependencies:
lodash._reinterpolate "~3.0.0"

lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"

Expand Down

0 comments on commit dbea571

Please sign in to comment.