-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Return cleanup functions from beforeAll and beforeEach #10453
Comments
I agree that the hooks API could use a redesign (in fact I talked about this over a year ago). I'm not quite sure how that would happen - perhaps as additional functions under a different name. I'm not sure we have another ticket open for this (#7823 is quite different), so we can leave this open for tracking I guess. I would also like to mention that this is not the only way to improve hooks by using return values, so there would probably need to be some debate around it. beforeEach(() => {
const originalConsoleLog = console.log;
console.log = jest.fn();
return () => {
console.log = originalConsoleLog;
}
}) But the following is good for when you have before and need something in the module scope, but do not need after (e.g. with react-testing-library which does it for you): const fixtureRef = beforeEach(() => {
return render(<Component />);
})
test('t', () => {
fixtureRef.current.getByText('hello world');
}) A second parameter if after is needed could also be a consideration: const fixtureRef = aroundEach(() => {
return render(<Component />);
}, (fixture) => {
fixture.unmount();
})
test('t', () => {
fixtureRef.current.getByText('hello world');
}) |
I have given the @jeysal’s some thought. I like this as well, but I don’t like the idea of having to use Of course one could still the following, even if the hook would return a fixture ref.
I do like the idea of using a new name for the hook, i.e. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
It would be a nice addition if the
beforeAll()
andbeforeEach()
function could return cleanup functions. This is based on the cleanup function ofReact.useEffect()
.Because one setup may depend on another, the cleanup functions should run in the reverse order of their definition after the
afterAll()
andafterEach()
blocks respectively. I.e. the following test:will log:
Motivation
Currently one needs to store the variables on a higher scope, so the cleanup can be done in a
afterAll()
orafterEach()
block. This works fine, but it’s more work. One needs to declare the variables and possibly explicitly add type definitions. Also returning such cleanup functions keeps related setup and cleanup close together.Example
Currently:
With returned cleanup functions.
Variables can still be assigned for use in other setup blocks or tests
Pitch
This changes the behaviour of
beforeAll()
andbeforeEach()
.The text was updated successfully, but these errors were encountered: