Skip to content

Commit

Permalink
fix: expected getBy query errors being logged
Browse files Browse the repository at this point in the history
When getBy queries throw the resulting errors are not caught in the
executeAsync call, WebdriverIO thinks this error is not expected and so
logs it as an error. This is a problem for tests that expect an error to
be produced.

Catch errors thrown by getBy queries and pass their messages back like
we do for findBy promise rejections.

Change the logLevel in wdio.conf.js to 'warn' to see these logs in
future.

Fix the configure test 'supports setting throwSuggestions' to assert
using the new error message produced.

BREAKING CHANGE: The errors thrown by getBy queries will have slightly
different messages as they are no longer being processed directly by
WebdriverIO. This may cause tests that assert against the error message
to fail after this fix.
  • Loading branch information
olivierwilkinson committed Jun 11, 2021
1 parent 38da772 commit 6f67a7a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
26 changes: 15 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function executeQuery(
container: HTMLElement,
...args: any[]
) {
const done = args.pop() as (result: any) => void;
const done = args.pop() as (result: any) => void

// @ts-ignore
function deserializeObject(object) {
Expand All @@ -96,16 +96,20 @@ function executeQuery(

const [matcher, options, waitForOptions] = args.map(deserializeArg)

Promise.resolve(
window.TestingLibraryDom[query](
container,
matcher,
options,
waitForOptions,
),
)
.then(done)
.catch((e) => done(e.message))
try {
Promise.resolve(
window.TestingLibraryDom[query](
container,
matcher,
options,
waitForOptions,
),
)
.then(done)
.catch((e) => done(e.message))
} catch (e) {
done(e.message)
}
}

/*
Expand Down
2 changes: 1 addition & 1 deletion test/async/configure.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('configure', () => {
await expect(() =>
getByTestId('button-that-should-not-use-testid'),
).rejects.toThrowError(
'TestingLibraryElementError: A better query is available',
'A better query is available',
)
})

Expand Down
2 changes: 1 addition & 1 deletion wdio.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ exports.config = {
// Define all options that are relevant for the WebdriverIO instance here
//
// Level of logging verbosity: trace | debug | info | warn | error | silent
logLevel: 'silent',
logLevel: 'warn',
//
// Set specific log levels per logger
// loggers:
Expand Down

0 comments on commit 6f67a7a

Please sign in to comment.