Skip to content

Commit 150ca6b

Browse files
committed
fix DEFAULT_OPTIONS_TO_BE_DISPLAYED not aligne with global options
1 parent 4d3b41c commit 150ca6b

4 files changed

Lines changed: 54 additions & 17 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { $ } from '@wdio/globals'
2+
import { setOptions, getConfig } from 'expect-webdriverio'
3+
4+
describe('Global Options', () => {
5+
6+
before(() => {
7+
// Verify we can set the option to 1ms
8+
setOptions({ wait: 1 })
9+
expect(getConfig().wait).toBe(1)
10+
})
11+
12+
it('should allow setting and using global wait option', async () => {
13+
const start = Date.now()
14+
15+
// This should fail almost immediately because the element doesn't exist
16+
// and the wait time is set to 1ms
17+
await expect(expect($('non-existent-element-' + Date.now())).toBeDisplayed()).rejects.toThrow()
18+
const duration = Date.now() - start
19+
20+
// It should take significantly less time than the default or the 250ms set in wdio.conf
21+
// 1ms wait + overhead. 500ms should be a safe upper bound for "immediate" failure
22+
// vs a standard 3000ms timeout
23+
expect(duration).toBeLessThan(500)
24+
})
25+
26+
after(() => {
27+
// Restore the option to 250ms as inferred from wdio.conf.ts to not pollute other tests
28+
setOptions({ wait: 250 })
29+
})
30+
})

src/constants.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,16 @@ export const DEFAULT_OPTIONS: Required<ExpectWebdriverIO.DefaultOptions> = {
44
beforeAssertion: async () => {},
55
afterAssertion: async () => {},
66
}
7+
8+
export const DEFAULT_OPTIONS_TO_BE_DISPLAYED: Required<Omit<ExpectWebdriverIO.ToBeDisplayedOptions, 'message'>> = {
9+
...DEFAULT_OPTIONS,
10+
withinViewport: false,
11+
contentVisibilityAuto: true,
12+
opacityProperty: true,
13+
visibilityProperty: true
14+
}
15+
16+
export const defaultOptionsList = [
17+
DEFAULT_OPTIONS,
18+
DEFAULT_OPTIONS_TO_BE_DISPLAYED
19+
]

src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { expect as expectLib } from 'expect'
33
import type { WdioMatchersObject } from './types.js'
44
import * as wdioMatchers from './matchers.js'
5-
import { DEFAULT_OPTIONS } from './constants.js'
5+
import { DEFAULT_OPTIONS, defaultOptionsList } from './constants.js'
66
import createSoftExpect from './softExpect.js'
77
import { SoftAssertService } from './softAssert.js'
88

@@ -52,12 +52,14 @@ Object.defineProperty(expectWithSoft, 'clearSoftFailures', {
5252
export const expect = expectWithSoft
5353

5454
export const getConfig = (): ExpectWebdriverIO.DefaultOptions => DEFAULT_OPTIONS
55-
export const setDefaultOptions = (options = {}): void => {
55+
export const setDefaultOptions = (options: Partial<ExpectWebdriverIO.DefaultOptions>): void => {
5656
Object.entries(options).forEach(([key, value]) => {
57-
if (key in DEFAULT_OPTIONS) {
58-
// @ts-ignore
59-
DEFAULT_OPTIONS[key] = value
60-
}
57+
defaultOptionsList.forEach((option) => {
58+
if (key in option) {
59+
// @ts-ignore
60+
option[key] = value
61+
}
62+
})
6163
})
6264
}
6365
export const setOptions = setDefaultOptions

src/matchers/element/toBeDisplayed.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import { executeCommandBe } from '../../utils.js'
2-
import { DEFAULT_OPTIONS } from '../../constants.js'
32
import type { WdioElementMaybePromise } from '../../types.js'
4-
5-
const DEFAULT_OPTIONS_DISPLAYED: ExpectWebdriverIO.ToBeDisplayedOptions = {
6-
...DEFAULT_OPTIONS,
7-
withinViewport: false,
8-
contentVisibilityAuto: true,
9-
opacityProperty: true,
10-
visibilityProperty: true
11-
}
3+
import { DEFAULT_OPTIONS_TO_BE_DISPLAYED } from '../../constants.js'
124

135
export async function toBeDisplayed(
146
received: WdioElementMaybePromise,
15-
options: ExpectWebdriverIO.ToBeDisplayedOptions = DEFAULT_OPTIONS_DISPLAYED,
7+
options: ExpectWebdriverIO.ToBeDisplayedOptions = DEFAULT_OPTIONS_TO_BE_DISPLAYED,
168
) {
179
this.expectation = this.expectation || 'displayed'
1810

@@ -27,7 +19,7 @@ export async function toBeDisplayed(
2719
opacityProperty,
2820
visibilityProperty,
2921
...commandOptions
30-
} = { ...DEFAULT_OPTIONS_DISPLAYED, ...options }
22+
} = { ...DEFAULT_OPTIONS_TO_BE_DISPLAYED, ...options }
3123

3224
const result = await executeCommandBe.call(this, received, el => el?.isDisplayed({
3325
withinViewport,

0 commit comments

Comments
 (0)