Skip to content

Commit 6f67a7a

Browse files
fix: expected getBy query errors being logged
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.
1 parent 38da772 commit 6f67a7a

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function executeQuery(
7171
container: HTMLElement,
7272
...args: any[]
7373
) {
74-
const done = args.pop() as (result: any) => void;
74+
const done = args.pop() as (result: any) => void
7575

7676
// @ts-ignore
7777
function deserializeObject(object) {
@@ -96,16 +96,20 @@ function executeQuery(
9696

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

99-
Promise.resolve(
100-
window.TestingLibraryDom[query](
101-
container,
102-
matcher,
103-
options,
104-
waitForOptions,
105-
),
106-
)
107-
.then(done)
108-
.catch((e) => done(e.message))
99+
try {
100+
Promise.resolve(
101+
window.TestingLibraryDom[query](
102+
container,
103+
matcher,
104+
options,
105+
waitForOptions,
106+
),
107+
)
108+
.then(done)
109+
.catch((e) => done(e.message))
110+
} catch (e) {
111+
done(e.message)
112+
}
109113
}
110114

111115
/*

test/async/configure.e2e.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('configure', () => {
4747
await expect(() =>
4848
getByTestId('button-that-should-not-use-testid'),
4949
).rejects.toThrowError(
50-
'TestingLibraryElementError: A better query is available',
50+
'A better query is available',
5151
)
5252
})
5353

wdio.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ exports.config = {
7777
// Define all options that are relevant for the WebdriverIO instance here
7878
//
7979
// Level of logging verbosity: trace | debug | info | warn | error | silent
80-
logLevel: 'silent',
80+
logLevel: 'warn',
8181
//
8282
// Set specific log levels per logger
8383
// loggers:

0 commit comments

Comments
 (0)