referenceerror: you are trying to `import` a file after the jest environment has been torn down.

In case you are using react-native-testing-library, you can try to make the callback of the test case async as follows:

it('your test case', async () => {...})

instead of

it('your test case', () => {...})

In the general case, this problem can be solved by adding “jest.useFakeTimers(); before each test manually or with a setup function such as beforeEach. For example (Source: https://jestjs.io/docs/timer-mocks)

// __tests__/timerGame-test.js
'use strict';

jest.useFakeTimers();

test('waits 1 second before ending the game', () => {
  const timerGame = require('../timerGame');
  timerGame();

  expect(setTimeout).toHaveBeenCalledTimes(1);
  expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000);
});

From Jest’s documentation, you should take their following note

This mocks out setTimeout and other timer functions with mock functions. If running multiple tests inside of one file or describe block, jest.useFakeTimers(); can be called before each test manually or with a setup function such as beforeEach. Not doing so will result in the internal usage counter not being reset.


More Articles

How to start a new Github repo from an existing project

Assume that you have a project and you want to track it with Github. Navigate to the project folder,...

codezilla

codezilla

How to check for listening ports in Linux

Here is one of the most popular commands for troubleshooting network or specific application problem...

codezilla

codezilla

How to get octal file permissions from command line?

Sometimes we want to know the octal number representing the permission of a file like 755, 777, 664 ...

codezilla

codezilla