Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): keep track of registered matchers #1451

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +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')
})
Loading