jest
jest copied to clipboard
Hooks cannot be defined inside tests. Hook of type "beforeEach" is nested within "check for App".
Version
27.4.5
Steps to reproduce
upgrade from 26.6.3 to 27.4.5
Expected behavior
No error should be shown
Actual behavior
41 | );
42 |
> 43 | beforeEach(() => {
| ^
44 | fetch.mockClear();
45 | });
46 |
Additional context
@simen
Please include a reproduction.
@SimenB code-
describe("Test for Component", () => {
sendEvent.mockImplementation(() => {});
const store = configureStore({
getState: jest.fn()
});
it("check for App", () => {
global.fetch = jest.fn(() =>
Promise.resolve({
json: () =>
Promise.resolve({
ok: true,
status: 200,
statusText: "success",
data: {}
})
})
);
beforeEach(() => {
fetch.mockClear();
});
// const component = renderer.create(
// <Provider store={store}>
// <Router>
// <App />
// </Router>
// </Provider>
// );
// const tree = component.toJSON();
// expect(tree).toMatchSnapshot();
});
});
@SimenB what do you mean by Please include a reproduction.
?
Why would you put beforeEach in test itself?
I've been watching this thread for a few days for activity and thought I'll chip in my 2 cents.
@code-masala This isn't a bug. "test(name, fn, timeout)" as an alias for "it(name, fn, timeout)". Swap it with "test" and see what happens.
Please refer to the docs (linked to relevant section).
describe("****" , () => {
// A.K.A it
test("****", () => {
// beforeEach
// expect
}
})
Please close this issue.
I had the same problem in react-native
with some tests.
What fixed it was to:
- replace
jest-environment-jsdom
withjsdom
in jest.config.js - use fake timers in legacy mode
- call
afterEach(cleanup)
in tests
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 30 days.
This issue was closed because it has been stalled for 30 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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.