jest icon indicating copy to clipboard operation
jest copied to clipboard

Hooks cannot be defined inside tests. Hook of type "beforeEach" is nested within "check for App".

Open code-masala opened this issue 3 years ago • 7 comments

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

code-masala avatar Dec 21 '21 06:12 code-masala

Please include a reproduction.

SimenB avatar Dec 21 '21 07:12 SimenB

@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();
	});
});

code-masala avatar Dec 21 '21 08:12 code-masala

@SimenB what do you mean by Please include a reproduction.?

code-masala avatar Dec 21 '21 08:12 code-masala

Why would you put beforeEach in test itself?

Smrtnyk avatar Dec 21 '21 09:12 Smrtnyk

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.

davidtaing avatar Jan 08 '22 10:01 davidtaing

I had the same problem in react-native with some tests. What fixed it was to:

  • replace jest-environment-jsdom with jsdom in jest.config.js
  • use fake timers in legacy mode
  • call afterEach(cleanup) in tests

rpopovici avatar Feb 03 '23 11:02 rpopovici

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.

github-actions[bot] avatar Feb 03 '24 12:02 github-actions[bot]

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.

github-actions[bot] avatar Mar 04 '24 12:03 github-actions[bot]

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.

github-actions[bot] avatar Apr 04 '24 00:04 github-actions[bot]