-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Incorrect behavior after set injectGlobals: false in jest config #1240
Comments
if |
it has same behavior in Bun test |
I encountered the same problem with vitest ( This seems to solve it in vitest - please check if something similar works in Jest / Bun test: // vitest.config.ts
export default defineConfig({
// ...
setupFiles: ['src/testing/setupTests.tsx')]
}); // setupTests.tsx
import { cleanup } from '@testing-library/react';
import { afterEach } from 'vitest';
afterEach(() => cleanup()); Jest has setupFiles and setupFilesAfterEnv, I'm not sure which one you would need. Bun has The reason this happens: with |
@leonadler thank you for workaround i will try to use this solution but i think that it is need to be fixed in testing library |
It can't really be, is what I'm saying - You might be using jest, or Bun (so you yourself are already using 2 test runners!), I am using vitest, someone else might be using mocha, ava, ... They could add a section about it in the readme, I would agree - but they can't detect your testing framework if you basically configure it to |
Hi @krutoo, thanks for opening this.
Since there's no I'm closing this one because as already written here by @leonadler, we can't have dedicated code for each test runner nor we wan't to inject an |
@MatanBobi Sorry if this entirely the wrong place to ask, but next to the code that does // This matches the behavior of React < 18.
let previousIsReactActEnvironment = getIsReactActEnvironment()
beforeAll(() => {
previousIsReactActEnvironment = getIsReactActEnvironment()
setReactActEnvironment(true)
})
afterAll(() => {
setReactActEnvironment(previousIsReactActEnvironment)
}) Do I understand correctly that with React >= 18 this is not necessary anymore? Or do consumers with |
@leonadler AFAIR, |
Ok, thanks - just FYI, with TypeScript this gets a bit unwieldly and awkward, so maybe the module could export // my-project/external-modules.d.ts
declare module '@testing-library/react/dist/act-compat' {
export function getIsReactActEnvironment(): unknown;
export function setReactActEnvironment(env: unknown): void;
} // my-project/setup-tests.ts
import { cleanup } from '@testing-library/react';
import { getIsReactActEnvironment, setReactActEnvironment } from '@testing-library/react/dist/act-compat';
// ^ meh 🙁
import { afterAll, afterEach, beforeAll, beforeEach, vitest } from 'vitest';
let previousIsReactActEnvironment = getIsReactActEnvironment();
beforeAll(() => {
previousIsReactActEnvironment = getIsReactActEnvironment();
setReactActEnvironment(true);
});
afterEach(() => cleanup());
afterAll(() => {
setReactActEnvironment(previousIsReactActEnvironment);
}); |
I understand. |
can package log some warning message about inability to apply automatic cleanup when afterEach is undefined? |
@krutoo I've created a PR to add a warning in that case. |
@krutoo - This PR was reverted because it created too much problems for our users. We'll re-circle it to see if there's a different approach to do it. |
@MatanBobi Is there a new issue to track this? |
@MattyBalaam - I just opened one: #1257 |
Relevant code or config:
I'm trying to switch from injected globals to import needed functions from
@jest/globals
i just add
insectGlobals: false
injest.config.ts
What you did:
I added needed imports to test file and tests fails with errors like:
What happened:
errors are reproduced only if there are two or more tests
it looks like the render result is not reset and they are rendered into one document
Reproduction:
Here an repo with example of incorrect behavior
https://github.com/krutoo/testing-library-react-jest-no-inject-globals
The text was updated successfully, but these errors were encountered: