Skip to content

Commit

Permalink
expect-webdriverio: revert breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinheitzman committed Feb 12, 2024
1 parent 3a54956 commit 87a4b0a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 87 deletions.
15 changes: 2 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
/// <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'

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

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: 0 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
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: 58 additions & 68 deletions test/matchers.test.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,64 @@
import { test, expect, vi } from 'vitest'
import { matchers, expect as expectLib } from '../src/index.js'
import { test, expect } from 'vitest'
import Matchers from '../src/matchers.js'

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',
test('matchers', () => {
expect(Object.keys(Matchers)).toEqual([
// browser
'toHaveClipboardText',
'toHaveClipboardTextContaining',
'toHaveTitle',
'toHaveTitleContaining',
'toHaveUrl',
'toHaveUrlContaining',

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

// snapshot
'toMatchSnapshot',
'toMatchInlineSnapshot'
]
// 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',

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

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')
// snapshot
'toMatchSnapshot',
'toMatchInlineSnapshot'
])
})

0 comments on commit 87a4b0a

Please sign in to comment.