Skip to content

Commit

Permalink
feat(jest-environment-puppeteer): accept a promise as config (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartholomae authored and gregberge committed Jan 1, 2019
1 parent 8ffd49f commit 40bc3a2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
33 changes: 20 additions & 13 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ it('should put test in debug mode', async () => {

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

You can specify a `jest-puppeteer.config.js` at the root of the project or define a custom path using `JEST_PUPPETEER_CONFIG` environment variable.
You can specify a `jest-puppeteer.config.js` at the root of the project or define a custom path using `JEST_PUPPETEER_CONFIG` environment variable. It should export a config object or a Promise for a config object.

- `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.
Expand Down Expand Up @@ -113,19 +113,26 @@ This example uses an already running instance of Chrome by passing the active we

```js
// jest-puppeteer.config.js
const wsEndpoint = fs.readFileSync(endpointPath, 'utf8')

module.exports = {
connect: {
browserWSEndpoint: wsEndpoint,
},
server: {
command: 'node server.js',
port: 4444,
launchTimeout: 10000,
debug: true,
},
const fetch = require('node-fetch')
const dockerHost = 'http://localhost:9222'

async function getConfig () {
const response = await fetch(`${dockerHost}/json/version`)
const browserWSEndpoint = (await response.json()).webSocketDebuggerUrl
return {
connect: {
browserWSEndpoint
},
server: {
command: 'node server.js',
port: 3000,
launchTimeout: 10000,
debug: true,
}
}
}

module.exports = getConfig()
```

## Inspiration
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-environment-puppeteer/src/readConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function readConfig() {
}

// eslint-disable-next-line global-require, import/no-dynamic-require
const localConfig = require(absConfigPath)
const localConfig = await require(absConfigPath)
return merge({}, defaultConfig, localConfig)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = Promise.resolve({
server: {
command: 'yarn babel-node src/server/main.js',
},
})
12 changes: 12 additions & 0 deletions packages/jest-environment-puppeteer/tests/readConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@ describe('readConfig', () => {
])
})
})

describe('with Promise returning config', () => {
it('should return async config', async () => {
process.env.JEST_PUPPETEER_CONFIG = path.resolve(
__dirname,
'__fixtures__/promiseConfig.js',
)
mockExists(true)
const config = await readConfig()
expect(config.server).toBeDefined()
})
})
})

0 comments on commit 40bc3a2

Please sign in to comment.