Skip to content

Commit

Permalink
feat(expect-puppeteer): make default options configurable (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Apr 22, 2018
1 parent 72b60a9 commit 0d493c4
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 12 deletions.
10 changes: 10 additions & 0 deletions packages/expect-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ await expect(page).toUploadFile(
)
```

## Configure default options

To configure default options like `timeout`, `expect-puppeteer` exposes two methods: `getDefaultOptions` and `setDefaultOptions`. You can find available options in [Puppeteer `page.waitForFunction` documentation](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforfunctionpagefunction-options-args). Default options are set to: `{ timeout: 500 }`.

```js
import { setDefaultOptions } from 'jest-puppeteer'

setDefaultOptions({ timeout: 1000 })
```

## License

MIT
Expand Down
3 changes: 3 additions & 0 deletions packages/expect-puppeteer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import toMatch from './matchers/toMatch'
import toMatchElement from './matchers/toMatchElement'
import toSelect from './matchers/toSelect'
import toUploadFile from './matchers/toUploadFile'
import { setDefaultOptions, getDefaultOptions } from './options'

const pageMatchers = {
toClick,
Expand Down Expand Up @@ -108,3 +109,5 @@ if (typeof global.expect !== 'undefined') {
}

module.exports = expectPuppeteer
module.exports.setDefaultOptions = setDefaultOptions
module.exports.getDefaultOptions = getDefaultOptions
8 changes: 8 additions & 0 deletions packages/expect-puppeteer/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDefaultOptions, setDefaultOptions } from './'

describe('expect-puppeteer', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
Expand All @@ -13,4 +15,10 @@ describe('expect-puppeteer', () => {

expect(200).toBe(200)
})

it('should get and set default options', () => {
expect(getDefaultOptions()).toEqual({ timeout: 500 })
setDefaultOptions({ timeout: 200 })
expect(getDefaultOptions()).toEqual({ timeout: 200 })
})
})
3 changes: 2 additions & 1 deletion packages/expect-puppeteer/src/matchers/notToMatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defaultOptions, getContext, enhanceError } from '../utils'
import { getContext, enhanceError } from '../utils'
import { defaultOptions } from '../options'

async function notToMatch(instance, matcher, options) {
options = defaultOptions(options)
Expand Down
3 changes: 2 additions & 1 deletion packages/expect-puppeteer/src/matchers/notToMatchElement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defaultOptions, getContext, enhanceError } from '../utils'
import { getContext, enhanceError } from '../utils'
import { defaultOptions } from '../options'

async function notToMatchElement(
instance,
Expand Down
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/matchers/toFillForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultOptions } from '../utils'
import { defaultOptions } from '../options'
import toFill from './toFill'
import toMatchElement from './toMatchElement'

Expand Down
3 changes: 2 additions & 1 deletion packages/expect-puppeteer/src/matchers/toMatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defaultOptions, getContext, enhanceError } from '../utils'
import { getContext, enhanceError } from '../utils'
import { defaultOptions } from '../options'

async function toMatch(instance, matcher, options) {
options = defaultOptions(options)
Expand Down
3 changes: 2 additions & 1 deletion packages/expect-puppeteer/src/matchers/toMatchElement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defaultOptions, getContext, enhanceError } from '../utils'
import { getContext, enhanceError } from '../utils'
import { defaultOptions } from '../options'

async function toMatchElement(instance, selector, { text, ...options } = {}) {
options = defaultOptions(options)
Expand Down
12 changes: 12 additions & 0 deletions packages/expect-puppeteer/src/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let defaultOptionsValue = { timeout: 500 }

export const setDefaultOptions = options => {
defaultOptionsValue = options
}

export const getDefaultOptions = () => defaultOptionsValue

export const defaultOptions = options => ({
...defaultOptionsValue,
...options,
})
13 changes: 13 additions & 0 deletions packages/expect-puppeteer/src/options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getDefaultOptions, setDefaultOptions, defaultOptions } from './options'

describe('options', () => {
it('should get and set default options', () => {
expect(getDefaultOptions()).toEqual({ timeout: 500 })
setDefaultOptions({ timeout: 200 })
expect(getDefaultOptions()).toEqual({ timeout: 200 })

// Default options
expect(defaultOptions({})).toEqual({ timeout: 200 })
expect(defaultOptions({ timeout: 300 })).toEqual({ timeout: 300 })
})
})
7 changes: 0 additions & 7 deletions packages/expect-puppeteer/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
const DEFAULT_OPTIONS = { timeout: 500 }

export const defaultOptions = options => ({
...DEFAULT_OPTIONS,
...options,
})

export const getPuppeteerType = instance => {
if (
instance &&
Expand Down

0 comments on commit 0d493c4

Please sign in to comment.