From 87a4b0a9dd24a127534e05e0fa3dcd3a09a5b214 Mon Sep 17 00:00:00 2001 From: Erwin Heitzman <15839059+erwinheitzman@users.noreply.github.com> Date: Mon, 12 Feb 2024 15:41:07 +0100 Subject: [PATCH] expect-webdriverio: revert breaking change --- src/index.ts | 15 +---- src/types.ts | 6 -- test/matchers.test.ts | 126 +++++++++++++++++++----------------------- 3 files changed, 60 insertions(+), 87 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0c5c4a59..f14689b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,24 +1,13 @@ /// 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() +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]) => { diff --git a/src/types.ts b/src/types.ts index 40541d86..82445221 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,9 +1,3 @@ -import type { ExpectationResult, MatcherContext } from 'expect' - export type WdioElementMaybePromise = WebdriverIO.Element | WebdriverIO.ElementArray | Promise | Promise - -export type RawMatcherFn = { - (this: Context, actual: any, ...expected: Array): ExpectationResult; -} diff --git a/test/matchers.test.ts b/test/matchers.test.ts index 49a21c8d..9d075b73 100644 --- a/test/matchers.test.ts +++ b/test/matchers.test.ts @@ -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' + ]) })