Skip to content

Commit

Permalink
Revert "expect-webdriverio: revert breaking change"
Browse files Browse the repository at this point in the history
This reverts commit 87a4b0a.
  • Loading branch information
christian-bromann committed Feb 12, 2024
1 parent 1315a55 commit 28c2ed7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 60 deletions.
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
/// <reference types="../types/standalone.d.ts" />
import { expect as expectLib } from 'expect'
import type { RawMatcherFn } from './types.js'

import wdioMatchers from './matchers.js'
import { DEFAULT_OPTIONS } from './constants.js'

expectLib.extend({ ...wdioMatchers })
export const matchers = new Map<string, RawMatcherFn>()

const extend = expectLib.extend
expectLib.extend = (m) => {
if (!m || typeof m !== 'object') {
return
}

Object.entries(m).forEach(([name, matcher]) => matchers.set(name, matcher))
return extend(m)
}

expectLib.extend(wdioMatchers)
export const expect = expectLib as unknown as ExpectWebdriverIO.Expect
export const matchers = wdioMatchers
export const getConfig = (): any => DEFAULT_OPTIONS
export const setDefaultOptions = (options = {}): void => {
Object.entries(options).forEach(([key, value]) => {
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import type { ExpectationResult, MatcherContext } from 'expect'

export type WdioElementMaybePromise =
WebdriverIO.Element | WebdriverIO.ElementArray |
Promise<WebdriverIO.Element> | Promise<WebdriverIO.ElementArray>

export type RawMatcherFn<Context extends MatcherContext = MatcherContext> = {
(this: Context, actual: any, ...expected: Array<any>): ExpectationResult;
}
126 changes: 68 additions & 58 deletions test/matchers.test.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,74 @@
import { test, expect } from 'vitest'
import Matchers from '../src/matchers.js'
import { test, expect, vi } from 'vitest'
import { matchers, expect as expectLib } from '../src/index.js'

test('matchers', () => {
expect(Object.keys(Matchers)).toEqual([
// browser
'toHaveClipboardText',
'toHaveClipboardTextContaining',
'toHaveTitle',
'toHaveTitleContaining',
'toHaveUrl',
'toHaveUrlContaining',
const ALL_MATCHERS = [
// browser
'toHaveClipboardText',
'toHaveClipboardTextContaining',
'toHaveTitle',
'toHaveTitleContaining',
'toHaveUrl',
'toHaveUrlContaining',

// elements
'toBeElementsArrayOfSize',

// elements
'toBeClickable',
'toBeDisabled',
'toBeDisplayed',
'toBeDisplayedInViewport',
'toBeEnabled',
'toExist',
'toBeExisting',
'toBePresent',
'toBeFocused',
'toBeSelected',
'toBeChecked',
'toHaveAttributeAndValue',
'toHaveAttribute',
'toHaveAttributeContaining',
'toHaveAttrContaining',
'toHaveAttr',
'toHaveChildren',
'toHaveClass',
'toHaveElementClass',
'toHaveElementClassContaining',
'toHaveClassContaining',
'toHaveHref',
'toHaveLink',
'toHaveHrefContaining',
'toHaveLinkContaining',
'toHaveHTML',
'toHaveHTMLContaining',
'toHaveId',
'toHaveSize',
'toHaveElementProperty',
'toHaveText',
'toHaveTextContaining',
'toHaveValue',
'toHaveValueContaining',
'toHaveStyle',

// elements
'toBeElementsArrayOfSize',
// mock
'toBeRequested',
'toBeRequestedTimes',
'toBeRequestedWith',
'toBeRequestedWithResponse',

// elements
'toBeClickable',
'toBeDisabled',
'toBeDisplayed',
'toBeDisplayedInViewport',
'toBeEnabled',
'toExist',
'toBeExisting',
'toBePresent',
'toBeFocused',
'toBeSelected',
'toBeChecked',
'toHaveAttributeAndValue',
'toHaveAttribute',
'toHaveAttributeContaining',
'toHaveAttrContaining',
'toHaveAttr',
'toHaveChildren',
'toHaveClass',
'toHaveElementClass',
'toHaveElementClassContaining',
'toHaveClassContaining',
'toHaveHref',
'toHaveLink',
'toHaveHrefContaining',
'toHaveLinkContaining',
'toHaveHTML',
'toHaveHTMLContaining',
'toHaveId',
'toHaveSize',
'toHaveElementProperty',
'toHaveText',
'toHaveTextContaining',
'toHaveValue',
'toHaveValueContaining',
'toHaveStyle',
// snapshot
'toMatchSnapshot',
'toMatchInlineSnapshot'
]

// mock
'toBeRequested',
'toBeRequestedTimes',
'toBeRequestedWith',
'toBeRequestedWithResponse',
test('matchers', () => {
expect([...matchers.keys()]).toEqual(ALL_MATCHERS)
})

// snapshot
'toMatchSnapshot',
'toMatchInlineSnapshot'
])
test('allows to add matcher', () => {
const matcher: any = vi.fn((actual: any, expected: any) => ({ pass: actual === expected }))
expectLib.extend({ toBeCustom: matcher })
// @ts-expect-error not in types
expectLib('foo').toBeCustom('foo')
expect(matchers.keys()).toContain('toBeCustom')
})

0 comments on commit 28c2ed7

Please sign in to comment.