'Jest test passes but .. has console message You are trying to access a property or method of the Jest environment after it has been torn down

My test passes but has a console refrenceError:

You are trying to access a property or method of the Jest environment after it has been torn down.

Test:

import {render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react';
import {create, act} from 'react-test-renderer';
import Comp1 from './Comp1';
import ReactDOM from 'react-dom';
import "@testing-library/jest-dom";
import { QueryClient, QueryClientProvider } from 'react-query';

const queryClient = new QueryClient();

it('Should wait for response', () => {
    act(() => {
        waitFor(() => {
        render(<QueryClientProvider client={queryClient}><Comp1 /></QueryClientProvider>);
        expect(screen.getByTestId('loading', {}, { timeout: 2000 })).toBeInTheDocument();
        });
    });
});

Any Idea's? this checks the response of a async/await api call.



Solution 1:[1]

It's an async issue, you need to add async, wait:

it('Should wait for response', async () => {
  render(
    <QueryClientProvider client={queryClient}>
      <Comp1 />
    </QueryClientProvider>
  );

  await waitFor(() => {
        expect(screen.getByTestId('loading', {})).toBeInTheDocument();
   });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manu Artero