Skip to content

Support async functions as callback in pm.test #919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/sandbox/pmapi-setup-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ module.exports = function (pm, onAssertionComplete) {
}
// if the assertion function does not expect a callback, we synchronously execute the same
else {
try { assert(); }
try {
Promise.resolve(assert())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vast majority of tests aren't going to be async functions, and we'll likely hit a small performance penalty on running all test functions through Promise resolution. Wondering if we could do something like...

try {
  const maybePromise = assert();
  if (maybePromise instanceof Promise) {
    Promise.resolve(maybePromise)
      .catch(e => markAssertionAsFailure(assertionData, e));
  }
} catch (e) {
  markAssertionAsFailure(assertionData, e);
}

onAssertionComplete(assertionData);

Alternatively, we could also check if the function's constructor name is AsyncFunction and split logic based on that without having to execute the function first.

.catch(e => markAssertionAsFailure(assertionData, e))
.finally(() => onAssertionComplete(assertionData));
}
catch (e) {
markAssertionAsFailure(assertionData, e);
onAssertionComplete(assertionData);
}

onAssertionComplete(assertionData);
}

return pm; // make it chainable
Expand Down