Repository navigation
bun:test: throw when an equality tester or asymmetricMatch() returns a promise #48057
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -544,6 +544,74 @@ impl Pass { | |
| } | ||
| } | ||
|
|
||
| /// Must match `ExpectAnswerer` in bindings.cpp. | ||
| #[derive(Copy, Clone)] | ||
| pub(crate) enum Answerer { | ||
| EqualityTester, | ||
| AsymmetricMatch, | ||
| IsEqualNode, | ||
| SatisfyPredicate, | ||
| Contains, | ||
| } | ||
|
|
||
| impl Answerer { | ||
| const ALL: [Answerer; 5] = [ | ||
| Answerer::EqualityTester, | ||
| Answerer::AsymmetricMatch, | ||
| Answerer::IsEqualNode, | ||
| Answerer::SatisfyPredicate, | ||
| Answerer::Contains, | ||
| ]; | ||
|
|
||
| /// Calls a user function whose answer decides a comparison at once. A promise is no answer, so it throws. | ||
| #[inline] | ||
| pub(crate) fn ask( | ||
| self, | ||
| global: &JSGlobalObject, | ||
| function: JSValue, | ||
| this: JSValue, | ||
| arguments: &[JSValue], | ||
| ) -> JsResult<JSValue> { | ||
| let answer = function.call(global, this, arguments)?; | ||
| if answer.is_object() { | ||
| self.refuse_thenable(global, answer)?; | ||
| } | ||
| Ok(answer) | ||
| } | ||
|
|
||
| #[cold] | ||
| #[optimize(size)] | ||
| fn refuse_thenable(self, global: &JSGlobalObject, answer: JSValue) -> JsResult<()> { | ||
| let returned = if let Some(promise) = answer.as_any_promise() { | ||
| // The error below reports it. As an unhandled rejection it would also fail whichever test runs then. | ||
| promise.set_handled(global.vm()); | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| "a promise" | ||
| } else if answer.get(global, "then")?.is_some_and(JSValue::is_callable) { | ||
| "a thenable" | ||
| } else { | ||
| return Ok(()); | ||
| }; | ||
| let who = match self { | ||
| Answerer::EqualityTester => "An equality tester", | ||
| Answerer::AsymmetricMatch => "asymmetricMatch()", | ||
| Answerer::IsEqualNode => "isEqualNode()", | ||
| Answerer::SatisfyPredicate => "The predicate of expect.toSatisfy()", | ||
| Answerer::Contains => "contains()", | ||
| }; | ||
| Err(global.throw_type_error(format_args!( | ||
| "{who} returned {returned}. It has to return its answer synchronously" | ||
| ))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 (optional) Suites that import Why this was flaggedTrigger: a project migrating from Vitest runs under Bun with Verification: src/runtime/test_runner/expect_deferred.rs
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No change for now. The rule for the
robobun marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /// Called by C++ deep equality with an object that `who` returned. Leaves an exception pending if it is refused. | ||
| #[unsafe(no_mangle)] | ||
| pub(crate) extern "C" fn Expect__refuseThenable(global: &JSGlobalObject, answer: JSValue, who: u8) { | ||
| if let Err(error) = Self::ALL[usize::from(who)].refuse_thenable(global, answer) { | ||
| bun_jsc::host_fn::host_call_error_value(global, error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A matcher call waiting on a promise. [`Pass::wait_for`] throws it and `Expect::matcher_threw` catches it. | ||
| pub(crate) struct ExpectDeferred { | ||
| held: JsCell<Option<RunningEntry>>, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.