Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
import { expect as expectLib } from 'expect'
import type { RawMatcherFn } from './types.js'

import wdioMatchers from './matchers.js'
import * as wdioMatchers from './matchers.js'
import { DEFAULT_OPTIONS } from './constants.js'
import createSoftExpect from './softExpect.js'
import { SoftAssertService } from './softAssert.js'

export const matchers = new Map<string, RawMatcherFn>()

const filteredMatchers = {}
const extend = expectLib.extend

Object.keys(wdioMatchers).forEach(matcher => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment here about what we are filtering for?

if (typeof wdioMatchers[matcher as keyof typeof wdioMatchers] === 'function') {
filteredMatchers[matcher as keyof typeof filteredMatchers] = wdioMatchers[matcher as keyof typeof filteredMatchers]
}
})

expectLib.extend = (m) => {
if (!m || typeof m !== 'object') {
return
Expand All @@ -21,7 +28,7 @@ expectLib.extend = (m) => {

type MatchersObject = Parameters<typeof expectLib.extend>[0]

expectLib.extend(wdioMatchers as MatchersObject)
expectLib.extend(filteredMatchers as MatchersObject)

// Extend the expect object with soft assertions
const expectWithSoft = expectLib as unknown as ExpectWebdriverIO.Expect
Expand Down
106 changes: 31 additions & 75 deletions src/matchers.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,31 @@
import * as toHaveClipboardText from './matchers/browser/toHaveClipboardText.js'
import * as toHaveTitle from './matchers/browser/toHaveTitle.js'
import * as toHaveUrl from './matchers/browser/toHaveUrl.js'
import * as toBeElementsArrayOfSize from './matchers/elements/toBeElementsArrayOfSize.js'
import * as toBeClickable from './matchers/element/toBeClickable.js'
import * as toBeDisabled from './matchers/element/toBeDisabled.js'
import * as toBeDisplayed from './matchers/element/toBeDisplayed.js'
import * as toBeDisplayedInViewport from './matchers/element/toBeDisplayedInViewport.js'
import * as toBeEnabled from './matchers/element/toBeEnabled.js'
import * as toBeExisting from './matchers/element/toBeExisting.js'
import * as toBeFocused from './matchers/element/toBeFocused.js'
import * as toBeSelected from './matchers/element/toBeSelected.js'
import * as toHaveAttribute from './matchers/element/toHaveAttribute.js'
import * as toHaveChildren from './matchers/element/toHaveChildren.js'
import * as toHaveClass from './matchers/element/toHaveClass.js'
import * as toHaveHref from './matchers/element/toHaveHref.js'
import * as toHaveHTML from './matchers/element/toHaveHTML.js'
import * as toHaveId from './matchers/element/toHaveId.js'
import * as toHaveSize from './matchers/element/toHaveSize.js'
import * as toHaveElementProperty from './matchers/element/toHaveElementProperty.js'
import * as toHaveText from './matchers/element/toHaveText.js'
import * as toHaveValue from './matchers/element/toHaveValue.js'
import * as toHaveStyle from './matchers/element/toHaveStyle.js'
import * as toBeRequested from './matchers/mock/toBeRequested.js'
import * as toBeRequestedTimes from './matchers/mock/toBeRequestedTimes.js'
import * as toBeRequestedWith from './matchers/mock/toBeRequestedWith.js'
import * as snapshotMatchers from './matchers/snapshot.js'

const matchers = {
// browser
...toHaveClipboardText,
...toHaveTitle,
...toHaveUrl,

// ElementArray $$
...toBeElementsArrayOfSize,

// Element $ or ElementArray $$
...toBeClickable,
...toBeDisabled,
...toBeDisplayed,
...toBeDisplayedInViewport,
...toBeEnabled,
...toBeExisting,
...toBeFocused,
...toBeSelected,
...toHaveAttribute,
...toHaveChildren,
...toHaveClass,
...toHaveHref,
...toHaveHTML,
...toHaveId,
...toHaveSize,
...toHaveElementProperty,
...toHaveText,
...toHaveValue,
...toHaveStyle,

// Mock
...toBeRequested,
...toBeRequestedTimes,
...toBeRequestedWith,

// Snapshot
...snapshotMatchers
}

// avoid exporting internal functions
Object.keys(matchers).forEach((key: keyof typeof matchers) => {
if (key.endsWith('Fn')) {
delete matchers[key]
}
})

export default matchers
export * from './matchers/browser/toHaveClipboardText.js'
export * from './matchers/browser/toHaveTitle.js'
export * from './matchers/browser/toHaveUrl.js'
export * from './matchers/element/toBeClickable.js'
export * from './matchers/element/toBeDisabled.js'
export * from './matchers/element/toBeDisplayed.js'
export * from './matchers/element/toBeDisplayedInViewport.js'
export * from './matchers/element/toBeEnabled.js'
export * from './matchers/element/toBeExisting.js'
export * from './matchers/element/toBeFocused.js'
export * from './matchers/element/toBeSelected.js'
export * from './matchers/element/toHaveAttribute.js'
export * from './matchers/element/toHaveChildren.js'
export * from './matchers/element/toHaveClass.js'
export * from './matchers/element/toHaveComputedLabel.js'
export * from './matchers/element/toHaveComputedRole.js'
export * from './matchers/element/toHaveElementProperty.js'
export * from './matchers/element/toHaveHeight.js'
export * from './matchers/element/toHaveHref.js'
export * from './matchers/element/toHaveHTML.js'
export * from './matchers/element/toHaveId.js'
export * from './matchers/element/toHaveSize.js'
export * from './matchers/element/toHaveStyle.js'
export * from './matchers/element/toHaveText.js'
export * from './matchers/element/toHaveValue.js'
export * from './matchers/element/toHaveWidth.js'
export * from './matchers/elements/toBeElementsArrayOfSize.js'
export * from './matchers/mock/toBeRequested.js'
export * from './matchers/mock/toBeRequestedTimes.js'
export * from './matchers/mock/toBeRequestedWith.js'
export * from './matchers/snapshot.js'
4 changes: 2 additions & 2 deletions src/matchers/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function toMatchSnapshotAssert (received: unknown, message: string, inlineOption
snapshotService.client.assert({
received,
message,
filepath: snapshotService.currentFilePath,
name: snapshotService.currentTestName,
filepath: snapshotService.currentFilePath as string,
name: snapshotService.currentTestName as string,
/**
* apply inline options if needed
*/
Expand Down
14 changes: 8 additions & 6 deletions src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class SnapshotService implements Services.ServiceInstance {
async beforeTest(test: Frameworks.Test) {
this.#currentFilePath = test.file
this.#currentTestName = `${test.parent} > ${test.title}`
await this.#snapshotClient.startCurrentRun(test.file, test.fullTitle, this.#options)
await this.#snapshotClient.setup(test.file, this.#options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the intention of this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client has been updated so I got errors, to be compliant I had to change this. The reason is shared in the description of the PR where you can see the changes that ViTest made @christian-bromann

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the version in package.json then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already grabs the latest version with the current package.json but I can do so yes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

}

async beforeStep(step: Frameworks.PickleStep, scenario: Frameworks.Scenario) {
Expand All @@ -88,15 +88,17 @@ export class SnapshotService implements Services.ServiceInstance {

this.#currentFilePath = file
this.#currentTestName = testName
await this.#snapshotClient.startCurrentRun(file, testName, this.#options)
await this.#snapshotClient.setup(file, this.#options)
}

async after() {
const result = await this.#snapshotClient.finishCurrentRun()
if (!result) {
return
if (this.#currentFilePath) {
const result = await this.#snapshotClient.finish(this.#currentFilePath)
if (!result) {
return
}
this.#snapshotResults.push(result)
}
this.#snapshotResults.push(result)
}

#isEqual (received: unknown, expected: unknown) {
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { setOptions, expect as expectExport, matchers, utils } from '../src/inde
test('index', () => {
expect(setOptions.name).toBe('setDefaultOptions')
expect(expectExport).toBeDefined()
expect(matchers).toBeDefined()
expect(utils.compareText).toBeDefined()
expect(matchers.size).toEqual(41)
})
16 changes: 10 additions & 6 deletions test/matchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const ALL_MATCHERS = [
'toHaveTitle',
'toHaveUrl',

// elements
'toBeElementsArrayOfSize',

// elements
// element
'toBeClickable',
'toBeDisabled',
'toBeDisplayed',
Expand All @@ -29,15 +26,22 @@ const ALL_MATCHERS = [
'toHaveClass',
'toHaveElementClass',
'toHaveClassContaining',
'toHaveComputedLabel',
'toHaveComputedRole',
'toHaveElementProperty',
'toHaveHeight',
'toHaveHref',
'toHaveLink',
'toHaveHTML',
'toHaveId',
'toHaveSize',
'toHaveElementProperty',
'toHaveStyle',
'toHaveText',
'toHaveValue',
'toHaveStyle',
'toHaveWidth',

// elements
'toBeElementsArrayOfSize',

// mock
'toBeRequested',
Expand Down
2 changes: 1 addition & 1 deletion test/matchers/beMatchers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vi, test, describe, expect } from 'vitest'
import { $ } from '@wdio/globals'
import { getExpectMessage, getReceived, matcherNameToString } from '../__fixtures__/utils.js'
import Matchers from '../../src/matchers.js'
import * as Matchers from '../../src/matchers.js'

vi.mock('@wdio/globals')

Expand Down
2 changes: 1 addition & 1 deletion test/matchers/browserMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { vi, test, describe, expect } from 'vitest'
import { browser } from '@wdio/globals'

import { getExpectMessage, getReceived, matcherNameToString, getExpected } from '../__fixtures__/utils.js'
import Matchers from '../../src/matchers.js'
import * as Matchers from '../../src/matchers.js'

vi.mock('@wdio/globals')

Expand Down