Skip to content

Commit 0d493c4

Browse files
authored
feat(expect-puppeteer): make default options configurable (#46)
1 parent 72b60a9 commit 0d493c4

File tree

11 files changed

+55
-12
lines changed

11 files changed

+55
-12
lines changed

packages/expect-puppeteer/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ await expect(page).toUploadFile(
219219
)
220220
```
221221

222+
## Configure default options
223+
224+
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 }`.
225+
226+
```js
227+
import { setDefaultOptions } from 'jest-puppeteer'
228+
229+
setDefaultOptions({ timeout: 1000 })
230+
```
231+
222232
## License
223233

224234
MIT

packages/expect-puppeteer/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import toMatch from './matchers/toMatch'
1010
import toMatchElement from './matchers/toMatchElement'
1111
import toSelect from './matchers/toSelect'
1212
import toUploadFile from './matchers/toUploadFile'
13+
import { setDefaultOptions, getDefaultOptions } from './options'
1314

1415
const pageMatchers = {
1516
toClick,
@@ -108,3 +109,5 @@ if (typeof global.expect !== 'undefined') {
108109
}
109110

110111
module.exports = expectPuppeteer
112+
module.exports.setDefaultOptions = setDefaultOptions
113+
module.exports.getDefaultOptions = getDefaultOptions

packages/expect-puppeteer/src/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getDefaultOptions, setDefaultOptions } from './'
2+
13
describe('expect-puppeteer', () => {
24
beforeEach(async () => {
35
await page.goto('http://localhost:4444')
@@ -13,4 +15,10 @@ describe('expect-puppeteer', () => {
1315

1416
expect(200).toBe(200)
1517
})
18+
19+
it('should get and set default options', () => {
20+
expect(getDefaultOptions()).toEqual({ timeout: 500 })
21+
setDefaultOptions({ timeout: 200 })
22+
expect(getDefaultOptions()).toEqual({ timeout: 200 })
23+
})
1624
})

packages/expect-puppeteer/src/matchers/notToMatch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { defaultOptions, getContext, enhanceError } from '../utils'
1+
import { getContext, enhanceError } from '../utils'
2+
import { defaultOptions } from '../options'
23

34
async function notToMatch(instance, matcher, options) {
45
options = defaultOptions(options)

packages/expect-puppeteer/src/matchers/notToMatchElement.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { defaultOptions, getContext, enhanceError } from '../utils'
1+
import { getContext, enhanceError } from '../utils'
2+
import { defaultOptions } from '../options'
23

34
async function notToMatchElement(
45
instance,

packages/expect-puppeteer/src/matchers/toFillForm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaultOptions } from '../utils'
1+
import { defaultOptions } from '../options'
22
import toFill from './toFill'
33
import toMatchElement from './toMatchElement'
44

packages/expect-puppeteer/src/matchers/toMatch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { defaultOptions, getContext, enhanceError } from '../utils'
1+
import { getContext, enhanceError } from '../utils'
2+
import { defaultOptions } from '../options'
23

34
async function toMatch(instance, matcher, options) {
45
options = defaultOptions(options)

packages/expect-puppeteer/src/matchers/toMatchElement.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { defaultOptions, getContext, enhanceError } from '../utils'
1+
import { getContext, enhanceError } from '../utils'
2+
import { defaultOptions } from '../options'
23

34
async function toMatchElement(instance, selector, { text, ...options } = {}) {
45
options = defaultOptions(options)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let defaultOptionsValue = { timeout: 500 }
2+
3+
export const setDefaultOptions = options => {
4+
defaultOptionsValue = options
5+
}
6+
7+
export const getDefaultOptions = () => defaultOptionsValue
8+
9+
export const defaultOptions = options => ({
10+
...defaultOptionsValue,
11+
...options,
12+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getDefaultOptions, setDefaultOptions, defaultOptions } from './options'
2+
3+
describe('options', () => {
4+
it('should get and set default options', () => {
5+
expect(getDefaultOptions()).toEqual({ timeout: 500 })
6+
setDefaultOptions({ timeout: 200 })
7+
expect(getDefaultOptions()).toEqual({ timeout: 200 })
8+
9+
// Default options
10+
expect(defaultOptions({})).toEqual({ timeout: 200 })
11+
expect(defaultOptions({ timeout: 300 })).toEqual({ timeout: 300 })
12+
})
13+
})

0 commit comments

Comments
 (0)