Skip to content

Commit 14cd90f

Browse files
refactor: catch getBy/findBy errors the same way
getBy queries throw an error whereas findBy queries reject a Promise. This means we try-catch the query and chain a catch onto the promise chain in order to handle both cases. Instead we can use an async IIFE and try-catch the result of the query. This handles both cases at once and allows us to flatten as an added benefit.
1 parent b1af2ac commit 14cd90f

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/index.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,29 @@ function executeQuery(
106106

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

109-
try {
110-
Promise.resolve(
111-
window.TestingLibraryDom[query](
109+
;(async () => {
110+
let result: undefined | null | HTMLElement | HTMLElement[]
111+
try {
112+
result = await window.TestingLibraryDom[query](
112113
container,
113114
matcher,
114115
options,
115116
waitForOptions,
116-
),
117-
)
118-
.then((result) => {
119-
if (!result) {
120-
return done(null)
121-
}
122-
if (Array.isArray(result)) {
123-
return done(
124-
result.map((element) => ({selector: window.Simmer(element)})),
125-
)
126-
}
127-
return done({selector: window.Simmer(result)})
128-
})
129-
.catch((e) => done(e.message))
130-
} catch (e) {
131-
done(e.message)
132-
}
117+
)
118+
} catch (e) {
119+
done(e.message)
120+
}
121+
122+
if (!result) {
123+
return done(null)
124+
}
125+
126+
if (Array.isArray(result)) {
127+
return done(result.map((element) => ({selector: window.Simmer(element)})))
128+
}
129+
130+
return done({selector: window.Simmer(result)})
131+
})()
133132
}
134133

135134
function createQuery(element: ElementBase, queryName: string) {

0 commit comments

Comments
 (0)