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
globalThis
is not the same as self
when using jsdom
#1146
Comments
I had a similar issue - it seemed the only way I could get the warning to go away was putting After much trial & error, here's what worked for me:
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
test: {
environment: "jsdom",
setupFiles: "./testSetup.ts",
},
});
globalThis.IS_REACT_ACT_ENVIRONMENT = true; |
I think this is a bug in testing-library. Our current approach doesn't allow If they are putting a property on a global object that is read somewhere else, then the function to get the |
What does "properly" mean here? The way we're doing it, does match the |
I wrote “property”. What I mean is it should write the same way it is read inside react (I haven’t check how it is read). Also, MDN’s polyfill doesn't check for existing “globalThis”, assuming it is always undefined, which is in my opinion incorrect behaviour. If you a polyfilling something, you should first check if you need to. |
React just reads it with We can check for |
version: Setting globalThis.IS_REACT_ACT_ENVIRONMENT = true in the jest.setup.js file does not work. On the contrary, the test runs continuously after the setting, repeating the problem of act() |
This is a separate problem. See #1242 |
Describe the bug
Observation
When testing react-components, you have to make sure that component-state changes are wrapped in an
act()
function call, otherwise the warningWarning: An update to App inside a test was not wrapped in act(...).
will be displayed.In the normal
create-react-app
setup, this warning is disabled when the code is currently running inside thewaitFor
function of@testing-library/react
.The same test, run with
vitest
shows the warning again.Expectation
My expectation is that no warning is shown.
Analysis
I figured out that
@testing-library/react
sets self.IS_REACT_ACT_ENVIRONMENT tofalse
, while react accessesIS_REACT_ACT_ENVIRONMENT
directly to determine, whether to the warning or not.In
jsdom
-environment ofvitest
,self
andglobalThis
seem to be different object, so settingself.IS_REACT_ACT_ENVIRONMENT
does not disable the warnings.When I run the same test with
react-scripts test
(i.e. in jest), they are the sameExpectation
self
andglobalThis
should be the same object.Reproduction
I created the repository https://github.com/nknapp/vitest-react-18-testing-library-missing-act-workaround to reproduce the error.
Setup dependencies
Run the test with
jest
, no warning will appearRun the test with
vitest
, there will be a warningOpen src/setupVitest.js and remove the comments from the workaround-code.
The run
yarn vitest
again. No warning will appearThe workaround defines
IS_REACT_ACT_ENVIRONMENT
as property that proxies its value toself.IS_REACT_ACT_ENVIRONMENT
, so that the deactivation works correctly.System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: