expect-webdriverio registers WebdriverIO custom matchers out of the box for a seamless experience.
To use WebdriverIO custom matchers (except asymmetric matchers) directly in:
- Jest: Register matchers manually with
expect.extend. - Jasmine: Using
@wdio/jasmine-frameworkprovides an out-of-the-box experience.- Else, register matchers manually with
jasmine.addAsyncMatchers, then they will be available onexpectAsync.
- Else, register matchers manually with
- Types: Type augmentation for custom matchers is provided. See Types.md for details.
Similar to how expect-webdriverio provide custom matchers it's possible to add your own custom matchers.
- Jasmine see custom matchers doc
- Everyone else see Jest's expect.extend
Custom matchers should be added in wdio before hook
// wdio.conf.js
{
async before () {
const { addCustomMatchers } = await import('./myMatchers')
addCustomMatchers()
}
}// myMatchers.js - Jest example
export function addCustomMatchers () {
expect.extend({
myMatcher (actual, expected) {
return { pass: actual === expected, message: () => 'some message' }
}
})
}Add the types of your matchers to the ExpectWebdriverIO namespace. It works with every framework: the types of expect-webdriverio/jest and of expect-webdriverio/jasmine (and @wdio/jasmine-framework) extend it, also for expect.soft().
// my-matchers.d.ts, or any .ts file of your project
declare global {
namespace ExpectWebdriverIO {
interface Matchers<R, T> {
// A matcher for any value
toBeWithinRange(floor: number, ceiling: number): R
// An async matcher for elements only: `never` blocks it on other values
toHaveDataState: T extends ChainablePromiseElement | WebdriverIO.Element
? (state: string | ExpectWebdriverIO.PartialMatcher<string>, options?: ExpectWebdriverIO.CommandOptions) => Promise<R>
: never
}
interface AsymmetricMatchers {
// The asymmetric form: `expect.toBeWithinRange(1, 10)`
toBeWithinRange(floor: number, ceiling: number): ExpectWebdriverIO.PartialMatcher<number>
}
}
}
export {}Matcherstakes both type parameters,<R, T>, as inexpect-webdriverio:Ris the return type of the assertion, andTis the type of the value given toexpect(). WithMatchers<R>only, TypeScript fails:All declarations of 'Matchers' must have identical type parameters.- An async matcher, which waits for the element, returns
Promise<R>. A matcher that compares a value at once returnsR. ExpectWebdriverIO.PartialMatchertakes the type of the value, e.g.PartialMatcher<string>.- In a file with an
importor anexport(a module), put the namespace indeclare global { … }, and end withexport {}when the file has no other export. In a.d.tsfile with noimportand noexport, writedeclare namespace ExpectWebdriverIO { … }directly. - Use the public types:
ExpectWebdriverIO.*(also as named exports ofexpect-webdriverio),WebdriverIO.Element, and the 2 global namesChainablePromiseElementandChainablePromiseArray(the types of a not-awaited$()and$$(), fromwebdriverio). The other helper types ofexpect-webdriverioare not global. - With Jest, you can also add the types to the
expectmodule (declare module 'expect' { interface Matchers<R, T> { … } }), or to thejestnamespace. Thejestnamespace does not typeexpect.soft().