Skip to content

Commit

Permalink
feat(jest-environment-puppeteer): add server.launchTimeout & server.d…
Browse files Browse the repository at this point in the history
…ebug options

Closes #44
  • Loading branch information
gregberge committed Apr 24, 2018
1 parent 0d493c4 commit e312717
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib/
node_modules/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Run your tests using Jest & Puppeteer 🎪✨
```
npm install --save-dev jest-puppeteer puppeteer
```

> TypeScript users should additionally install `@types/puppeteer` and `@types/jest-environment-puppeteer`
## Usage
Expand Down Expand Up @@ -85,6 +86,8 @@ module.exports = {
}
```

Other options are documented in [Jest Environment Puppeteer readme](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-environment-puppeteer#jest-puppeteerconfigjs).

### Configure Puppeteer

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.
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ You can specify a `jest-puppeteer.config.js` at the root of the project or defin
* `server` <[Object]> Server options
* `command` <[string]> Command to start server
* `port` <[number]> If specified, it will wait port to be listened
* `launchTimeout` <[number]> Maximum time in milliseconds to wait before the port is listened. Defaults to `5000`.
* `options` <[Object]> Optional options for [spawnd](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/spawnd/README.md)
* `debug` <[boolean]> Output server logs

```js
// jest-puppeteer.config.js
Expand All @@ -81,6 +83,8 @@ module.exports = {
server: {
command: 'node server.js',
port: 4444,
launchTimeout: 10000,
debug: true,
},
}
```
Expand Down
1 change: 1 addition & 0 deletions packages/jest-environment-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"puppeteer": "^1.0.0"
},
"dependencies": {
"chalk": "^2.4.0",
"cwd": "^0.10.0",
"lodash": "^4.17.5",
"mkdirp": "^0.5.1",
Expand Down
36 changes: 35 additions & 1 deletion packages/jest-environment-puppeteer/src/global.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import stream from 'stream'
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 chalk from 'chalk'
import readConfig from './readConfig'
import { DIR, WS_ENDPOINT_PATH } from './constants'

let browser
let server

const serverLogPrefixer = new stream.Transform({
transform(chunk, encoding, callback) {
this.push(
chalk.magentaBright(`[Jest Puppeteer server] ${chunk.toString()}`),
)
callback()
},
})

export async function setup() {
const config = await readConfig()
browser = await puppeteer.launch(config.launch)
Expand All @@ -25,8 +36,31 @@ export async function setup() {
...config.server.options,
})

if (config.server.debug) {
console.log(chalk.magentaBright('\nJest Puppeteer server output:'))
server.stdout.pipe(serverLogPrefixer).pipe(process.stdout)
}

if (config.server.port) {
await waitPort({ port: config.server.port, output: 'silent' })
const launchTimeout = config.server.launchTimeout || 5000
const timeout = setTimeout(() => {
console.error(
chalk.red(
`\nJest Puppeteer Error: Server has taken more than ${launchTimeout}ms to start.`,
),
)
console.error(
chalk.blue(
`You can set "server.launchTimeout" in jest-puppeteer.config.js`,
),
)
process.exit(1)
}, launchTimeout)
await waitPort({
port: config.server.port,
output: 'silent',
})
clearTimeout(timeout)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,14 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.0.tgz#a060a297a6b57e15b61ca63ce84995daa0fe6e52"
dependencies:
ansi-styles "^3.2.1"
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chardet@^0.4.0:
version "0.4.2"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2"
Expand Down

0 comments on commit e312717

Please sign in to comment.