From e3127174479ae417f151b61eed162b895e647bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Tue, 24 Apr 2018 16:31:41 +0200 Subject: [PATCH] feat(jest-environment-puppeteer): add server.launchTimeout & server.debug options Closes #44 --- .eslintignore | 1 + README.md | 3 ++ packages/jest-environment-puppeteer/README.md | 4 +++ .../jest-environment-puppeteer/package.json | 1 + .../jest-environment-puppeteer/src/global.js | 36 ++++++++++++++++++- yarn.lock | 8 +++++ 6 files changed, 52 insertions(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index c3af8579..46f10721 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ lib/ +node_modules/ \ No newline at end of file diff --git a/README.md b/README.md index 82a17861..7fd6b734 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/packages/jest-environment-puppeteer/README.md b/packages/jest-environment-puppeteer/README.md index 9ea5b500..d5efaeac 100644 --- a/packages/jest-environment-puppeteer/README.md +++ b/packages/jest-environment-puppeteer/README.md @@ -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 @@ -81,6 +83,8 @@ module.exports = { server: { command: 'node server.js', port: 4444, + launchTimeout: 10000, + debug: true, }, } ``` diff --git a/packages/jest-environment-puppeteer/package.json b/packages/jest-environment-puppeteer/package.json index 5c63a7a6..b84df0d6 100644 --- a/packages/jest-environment-puppeteer/package.json +++ b/packages/jest-environment-puppeteer/package.json @@ -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", diff --git a/packages/jest-environment-puppeteer/src/global.js b/packages/jest-environment-puppeteer/src/global.js index 3853a3b4..12feeebd 100644 --- a/packages/jest-environment-puppeteer/src/global.js +++ b/packages/jest-environment-puppeteer/src/global.js @@ -1,3 +1,4 @@ +import stream from 'stream' import fs from 'fs' import mkdirp from 'mkdirp' import rimraf from 'rimraf' @@ -5,12 +6,22 @@ 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) @@ -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) } } } diff --git a/yarn.lock b/yarn.lock index e46e13d8..15f5f456 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"