Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-environment-puppeteer): support custom config #28

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 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,9 +223,9 @@ 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.
* `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
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',
},
}
58 changes: 58 additions & 0 deletions packages/jest-environment-puppeteer/tests/readConfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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.server).toBeDefined()
})
})

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.server).not.toBeDefined()
})
})
})