-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Test with rejected Promise fails in Node, not in browser #468
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
Comments
My guess is this is a codesandbox bug. Is there a real world scenario that you can point us to for how this is issue impacting you? |
Also, this seems unrelated to react testing library. Could you verify that it's an issue in RTL? |
It was introduced in v8.0.6. My use case is React Async, which keeps a |
That's pretty strange. Would you mind doing a little digging to see if you can figure out why this is happening? |
You should know this whole thing was actually triggered by the Anyway, I'll see what I can do to further pin down the issue. |
Interestingly, I was able to bring down import {act} from 'react-dom/test-utils'
act(() => ({
then: () => {},
}))
export const asyncAct = act
export default act Turns out this little gem was removed in the 8.0.6 release. Obviously this completely breaks the actual act compatibility, but it shows the gist of it. |
As expected, the test passes once I call import React from "react"
import { render } from "@testing-library/react"
import { act } from "react-dom/test-utils"
act(() => ({ then: () => {} })) // this fixes the test
.then(() => {}) // this suppresses the "act without await" warning
const Hello = () => {
const promise = Promise.reject("Oh no!")
console.log(promise)
return <div>Hello RTL</div>
}
test("HelloRTL", async () => {
const { findByText } = render(<Hello />)
await findByText("Hello RTL")
})
|
The problem is that we need to support 16.8 for the foreseeable future. That's why act-compat exists 😬 |
Yes, but I'm sure we can just put that little snippet back in at the right place to fix this issue, while keeping the whole compat thing. I only brought it down to the bare minimum for debugging purposes. |
By the way relative-deps has been indispensable to get my local fork of |
I've pushed the workaround to the demo repo and confirmed that this fixes the test. |
I'm experiencing a similar issue I guess, but I'm pretty sure it has something to do with |
This may sound naive but isn't Could you check if the behavior is different without using |
@gnbaron Thanks, I will give that a try. @eps1lon I've tested this with plain Jest, following the testing guide on the React website. The problem does not appear there. Indeed this is an uncaught exception in Node. Perhaps I should change from a Promise to a Thenable to avoid that oddity. In my mind a rejected promise is just a value like any other, but this might not be true in certain environments. My goal is to expose this promise as a render prop in React Async, so it may or may not be used by library users. This works fine but blows up in RTL. |
@gnbaron So downgrading to 16.8.0 makes the test pass, but RTL will tell you that you need to upgrade to 16.9 because you need an async version of act. This also works by the way (with 16.9 and RTL): import React from "react"
import { render } from "@testing-library/react"
const Hello = () => {
const promise = Promise.reject("Oh no!")
promise.then(m => console.log(m), e => console.log(e)) // catch the exception
return <div>Hello RTL</div>
}
test("HelloRTL", async () => {
const { findByText } = render(<Hello />)
await findByText("Hello RTL")
}) So it seems @eps1lon is correct. |
Just tested it with plain I don't think this is an issue rtl should solve. The environment should decide what it does with unhandled rejections. diff --git a/src/hello.spec.js b/src/hello.spec.js
index 9aa3707..4f00861 100644
--- a/src/hello.spec.js
+++ b/src/hello.spec.js
@@ -1,5 +1,5 @@
import React from "react";
-import { render } from "@testing-library/react";
+import { render } from "react-dom";
const Hello = () => {
const promise = Promise.reject("Oh no!");
@@ -7,7 +7,17 @@ const Hello = () => {
return <div>Hello RTL</div>;
};
-test("HelloRTL", async () => {
- const { findByText } = render(<Hello />);
- await findByText("Hello RTL");
+let container;
+beforeEach(() => {
+ container = document.createElement("div");
+});
+
+afterEach(() => {
+ container = null;
+});
+
+test("HelloRTL", done => {
+ render(<Hello />, container);
+
+ setTimeout(() => done(), 100);
});
|
Considering this is unrelated to RTL specifically, I think we'll close this one. Thanks everyone! |
Uh oh!
There was an error while loading. Please reload this page.
Relevant code
The problem
Ran from the command line with
yarn test --watchAll
, this fails with the following output:Reproduction
Expected outcome
I would expect the test to pass in both environments. There is nothing wrong with assigning a rejected Promise to a variable, as long as it doesn't get rendered. The React component works fine.
The text was updated successfully, but these errors were encountered: