Skip to content

Commit 07cf00f

Browse files
committed
Better unit test
1 parent 8041f40 commit 07cf00f

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

test/matchers/element/toBeDisplayed.test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { $ } from '@wdio/globals'
33

44
import { toBeDisplayed } from '../../../src/matchers/element/toBeDisplayed.js'
55
import { executeCommandBe } from '../../../src/utils.js'
6-
import { DEFAULT_OPTIONS, DEFAULT_OPTIONS_TO_BE_DISPLAYED } from '../../../src/constants.js'
7-
import { setOptions } from '../../../src/index.js'
6+
import { DEFAULT_OPTIONS } from '../../../src/constants.js'
7+
import { setDefaultOptions } from '../../../src/index.js'
88
import type { ChainablePromiseElement } from 'webdriverio'
99

1010
vi.mock('@wdio/globals')
@@ -198,24 +198,19 @@ Received: "not displayed"`
198198
let el: ChainablePromiseElement
199199

200200
beforeEach(async () => {
201-
// Set global options to custom values before each test
202-
setOptions({ wait: 99, interval: 101 })
201+
setDefaultOptions({ wait: 99, interval: 101 })
203202
el = await $('sel')
204203
el.isDisplayed = vi.fn().mockResolvedValue(true)
205204

206205
})
207206

208207
afterEach(() => {
209-
// Reset options after each test to avoid side effects
210-
setOptions(defaultOptions)
211-
expect(DEFAULT_OPTIONS.wait).not.toBe(99)
208+
setDefaultOptions(defaultOptions)
212209
})
213210

214211
test('should use globally set default options with executeCommandBe', async () => {
215212
await toBeDisplayed.call({}, el)
216213

217-
expect(DEFAULT_OPTIONS.wait).toBe(99)
218-
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).toBe(99)
219214
expect(executeCommandBe).toHaveBeenCalledWith(
220215
el,
221216
expect.anything(),

test/options.test.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@ import { test, expect, describe, afterEach } from 'vitest'
22
import { setDefaultOptions, setOptions } from '../src/index.js'
33
import { DEFAULT_OPTIONS, DEFAULT_OPTIONS_TO_BE_DISPLAYED } from '../src/constants.js'
44

5-
describe('setDefaultOptions', () => {
5+
describe('Default Options', () => {
66
const defaultOptions = { ...DEFAULT_OPTIONS }
77
afterEach(() => {
8-
// Reset global options to default values after each test
9-
setOptions(defaultOptions)
8+
setDefaultOptions(defaultOptions)
109
})
1110

12-
test('setOptions should update both DEFAULT_OPTIONS_TO_BE_DISPLAYED and DEFAULT_OPTIONS', () => {
13-
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).not.toBe(1234)
14-
expect(DEFAULT_OPTIONS.wait).not.toBe(1234)
11+
describe(setOptions, () => {
12+
test('legacy setOptions should update both DEFAULT_OPTIONS_TO_BE_DISPLAYED and DEFAULT_OPTIONS', () => {
13+
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).not.toBe(98)
14+
expect(DEFAULT_OPTIONS.wait).not.toBe(98)
1515

16-
setOptions({ wait: 1234 })
16+
setOptions({ wait: 98 })
1717

18-
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).toBe(1234)
19-
expect(DEFAULT_OPTIONS.wait).toBe(1234)
18+
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).toBe(98)
19+
expect(DEFAULT_OPTIONS.wait).toBe(98)
20+
})
2021
})
2122

22-
test('setDefaultOptions should update both DEFAULT_OPTIONS_TO_BE_DISPLAYED and DEFAULT_OPTIONS', () => {
23-
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).not.toBe(1234)
24-
expect(DEFAULT_OPTIONS.wait).not.toBe(1234)
23+
describe(setDefaultOptions, () => {
2524

26-
setDefaultOptions({ wait: 1234 })
25+
test('setDefaultOptions should update both DEFAULT_OPTIONS_TO_BE_DISPLAYED and DEFAULT_OPTIONS', () => {
26+
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).not.toBe(1234)
27+
expect(DEFAULT_OPTIONS.wait).not.toBe(1234)
2728

28-
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).toBe(1234)
29-
expect(DEFAULT_OPTIONS.wait).toBe(1234)
29+
setDefaultOptions({ wait: 1234 })
30+
31+
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).toBe(1234)
32+
expect(DEFAULT_OPTIONS.wait).toBe(1234)
33+
})
3034
})
35+
3136
})
3237

0 commit comments

Comments
 (0)