'Why does jest think `TypeError: _global.setTimeout is not a function` in my TS app?

i'm having trouble setting up jest on an existing typescript app.

when i try to run a basic, "hello world" style test like:

test('foo', () => console.log('foo'))

i get:

  ● Test suite failed to run

    TypeError: _global.setTimeout is not a function

      at withGlobal (node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:146:35)
      at new FakeTimers (node_modules/@jest/fake-timers/build/modernFakeTimers.js:45:53)

why would this be?

my jest.config.js file is:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',

  roots: ['<rootDir>/src'],
  transform: {
    '^.+\\.(js|ts|tsx)?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  transformIgnorePatterns: ['"node_modules/(?!p-queue/.*)"'],
}

that transformIgnorePatterns in there is helping me get around a module that is written in esmodules, which otherwise causes a syntax error.

my relevant dependencies:

  "dependencies": {
    "@types/jest": "^27.5.0",
    "@types/p-queue": "^3.2.1",
    "jest": "^28.1.0",
    "p-queue": "^7.1.0",
    "ts-jest": "^28.0.2",
    "typescript": "^4.5.5",
    // etc...
  }


Sources

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

Source: Stack Overflow

Solution Source