Skip to content

Commit

Permalink
feat(jest-environment-puppeteer): support custom config
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
gregberge committed Apr 5, 2018
1 parent 5babe93 commit 44a64e2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,22 @@ For this use case, `jest-environment-puppeteer` exposes two methods: `setup` and

```js
// global-setup.js
const { setup: setupPuppeteer } = require('jest-environment-puppeteer');
const { setup: setupPuppeteer } = require('jest-environment-puppeteer')

module.exports = async function globalSetup() {
await setupPuppeteer();
await setupPuppeteer()
// Your global setup
};
}
```

```js
// global-teardown.js
const { teardown: teardownPuppeteer } = require('jest-environment-puppeteer');
const { teardown: teardownPuppeteer } = require('jest-environment-puppeteer')

module.exports = async function globalTeardown() {
// Your global teardown
await teardownPuppeteer();
};
await teardownPuppeteer()
}
```

Then assigning your js file paths to the [`globalSetup`](https://facebook.github.io/jest/docs/en/configuration.html#globalsetup-string) and [`globalTeardown`](https://facebook.github.io/jest/docs/en/configuration.html#globalteardown-string) property in your Jest configuration.
Expand Down Expand Up @@ -223,7 +223,7 @@ await expect(page).toMatch('A text in the page')

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

You can specify a `jest-puppeteer.config.js` at the root of the project.
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.

* `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
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ it('should fill an input', async () => {

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

You can specify a `jest-puppeteer.config.js` at the root of the project.
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.

* `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.
* `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.
* `server` <[Object]> Server options
* `command` <[string]> Command to start server
* `port` <[number]> If specified, it will wait port to be listened
Expand Down
19 changes: 16 additions & 3 deletions packages/jest-environment-puppeteer/src/readConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { merge } from 'lodash'

const exists = promisify(fs.exists)

const CONFIG_PATH = path.join(cwd(), 'jest-puppeteer.config.js')
const DEFAULT_CONFIG = {
launch: {},
}
Expand All @@ -20,10 +19,24 @@ async function readConfig() {
const defaultConfig =
process.env.CI === 'true' ? DEFAULT_CONFIG_CI : DEFAULT_CONFIG

if (!await exists(CONFIG_PATH)) return defaultConfig
const hasCustomConfigPath = !!process.env.JEST_PUPPETEER_CONFIG
const configPath =
process.env.JEST_PUPPETEER_CONFIG || 'jest-puppeteer.config.js'
const absConfigPath = path.resolve(cwd(), configPath)
const configExists = await exists(absConfigPath)

if (hasCustomConfigPath && !configExists) {
throw new Error(
`Error: Can't find a root directory while resolving a config file path.\nProvided path to resolve: ${configPath}`,
)
}

if (!hasCustomConfigPath && !configExists) {
return defaultConfig
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
server: {
command: 'yarn babel-node src/server/main.js',
},
}
61 changes: 61 additions & 0 deletions packages/jest-environment-puppeteer/tests/readConfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from 'fs'
import path from 'path'
import readConfig from '../src/readConfig'

jest.mock('fs')

function mockExists(value) {
fs.exists.mockImplementation((path, callback) => callback(null, value))
}

describe('readConfig', () => {
describe('with custom config path', () => {
beforeEach(() => {
process.env.JEST_PUPPETEER_CONFIG = 'nop.js'
})

it('should return an error if not found', async () => {
process.env.JEST_PUPPETEER_CONFIG = 'nop.js'
expect.assertions(1)
mockExists(false)
try {
await readConfig()
} catch (error) {
expect(error.message).toBe(
"Error: Can't find a root directory while resolving a config file path.\nProvided path to resolve: nop.js",
)
}
})

it('should return custom config', async () => {
process.env.JEST_PUPPETEER_CONFIG = path.resolve(
__dirname,
'__fixtures__/customConfig.js',
)
mockExists(true)
const config = await readConfig()
expect(config).toEqual({
launch: {},
server: { command: 'yarn babel-node src/server/main.js' },
})
})
})

describe('with default config path', () => {
beforeEach(() => {
process.env.JEST_PUPPETEER_CONFIG = ''
})

it('should return custom config', async () => {
mockExists(true)
const config = await readConfig()
expect(config.server).toBeDefined()
})

it('should return default config if not found', async () => {
mockExists(false)
const config = await readConfig()
expect(config).toEqual({ launch: {} })
})
})
})

0 comments on commit 44a64e2

Please sign in to comment.