Skip to content

Commit

Permalink
fix: assertion failure in new Elem API leads to unhandledRejection. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 authored Oct 21, 2024
1 parent 785d52a commit a3f3181
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
25 changes: 21 additions & 4 deletions lib/api/web-element/assert/element-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@ class ScopedElementAssertions {
this.nightwatchInstance = nightwatchInstance;
}

async assert(callback) {
const assert = this.nightwatchInstance.api.assert;
assert(callback) {
const assertPromise = new Promise((resolve, reject) => {
const assert = this.nightwatchInstance.api.assert;

await callback(this.negated ? assert.not : assert, this.scopedElement.webElement);
const callbackResult = callback(this.negated ? assert.not : assert, this.scopedElement.webElement);
if (callbackResult instanceof Promise) {
callbackResult
.then(() => {
resolve(this.scopedElement);
})
.catch(err => {
reject(err);
});
} else {
resolve(this.scopedElement);
}
});

return this.scopedElement;
// prevent unhandledRejection errors, while also making sure
// that the exception passes to the actual test case.
assertPromise.catch(() => {});

return assertPromise;
}

async executeScript(scriptFn, args) {
Expand Down
25 changes: 21 additions & 4 deletions lib/api/web-element/assert/value-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@ module.exports.create = function createAssertions(scopedValue, {negated, nightwa
this.scopedValue = scopedValue;
}

async _assert(callback) {
const assert = nightwatchInstance.api.assert;
await callback(this.negated ? assert.not : assert, this.scopedValue.value);
_assert(callback) {
const assertPromise = new Promise((resolve, reject) => {
const assert = nightwatchInstance.api.assert;
const callbackResult = callback(this.negated ? assert.not : assert, this.scopedValue.value);
if (callbackResult instanceof Promise) {
callbackResult
.then(() => {
resolve(this.scopedValue);
})
.catch(err => {
reject(err);
});
} else {
resolve(this.scopedValue);
}
});

// prevent unhandledRejection errors, while also making sure
// that the exception passes to the actual test case.
assertPromise.catch((err) => {});

return this.scopedValue;
return assertPromise;
}

get not() {
Expand Down

0 comments on commit a3f3181

Please sign in to comment.