9

I have a nestjs monorepo application with working tests via Jest. This is in relation to the global unit tests which take their configuration from the nestjs CLI-created configuration within package.json.

My storage.service.ts uses jimp in one of its methods to resize an image.

This has @jimp/types dependency that depends on @jimp/gif which depends on gifwrap.

For every test that runs in my console, I see this error:

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at node_modules/.pnpm/gifwrap@0.9.2/node_modules/gifwrap/src/gifcodec.js:7:15

I'm also using beforeAll() and afterAll() hook to close the nestjs module.

Jest config:

  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": ".",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "./coverage",
    "testEnvironment": "node",
    "roots": [
      "<rootDir>/apps/",
      "<rootDir>/libs/"
    ],
    "moduleNameMapper": {
...

How can I silence this error or perhaps even be as bold as fixing it?

SebastianG
  • 8,563
  • 8
  • 47
  • 111

3 Answers3

5

I had the same problem, and the issue occurs only if the test is synchronous.
A minimal timeout solves the problem:

afterAll(async () => {
  await new Promise(resolve => setTimeout(resolve));
});

or

afterAll(done => {
  setTimeout(done);
});
sky
  • 685
  • 7
  • 8
  • I've added `afterAll(done => { setTimeout(done); });` to my `setupTest.js` so this apply that code globally to all unit tests changing only one file and it solved my problem – tam.teixeira Dec 17 '21 at 18:28
  • For my case, I have to user `afterEach` rather than `afterAll` for the error to go away. – Yazid Feb 05 '22 at 07:05
2

Configuring the timers in jest config to modern solved the problem for me: jest-docs

"timers": "modern",
Ali Molaei
  • 396
  • 2
  • 13
0

Not sure if it will help your case, but the way I solved it in my typescript project is by mocking the jimp import itself. I was testing a class that used some classes that contained Jimp. Instead of mocking all of those, mocking jimp was easier.

jest.mock('jimp', () => {
  return {};
}

I put this below my imports and before my tests. Depending on what you're doing, you might need to go deeper in your mock as well. Example:

jest.mock('jimp', () => {
  return {
    read: jest.fn().mockImplementation(),
  };
});

I did it on jest 25, but I assume it works in most other versions as well.

My jest.config.js file:

module.exports = {
  moduleFileExtensions: ['js', 'json', 'ts'],
  rootDir: 'src',
  testRegex: '\\.spec\\.ts$',
  transform: {
    '^.+\\.(t|j)s$': 'ts-jest',
  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
    '^src/(.*)$': '<rootDir>/$1',
  },
  testPathIgnorePatterns: ['e2e/'],
  coverageDirectory: '../coverage',
  testEnvironment: 'node',
};

process.env = Object.assign(process.env, {
  APP_ENV: 'development',
});
Ars
  • 111
  • 3