Stack Exchange
Cache-Manager package missing error when testing NestJS application with Jest
I have a NestJS application that uses caching with
@nestjs/cache-manager 

and cache-manager packages. The app is up and running, but when testing, jest is complaining that the cache-manager package is missing.
ERROR [PackageLoader] The "cache-manager" package is missing. Please, make sure to install this library ($ npm install cache-manager) to take advantage of CacheModule.
  ●  process.exit called with "1"

      at loadPackage (../node_modules/@nestjs/common/utils/load-package.util.js:14:17)
      at InstanceWrapper.useFactory [as metatype] (../node_modules/@nestjs/cache-manager/dist/cache.providers.js:18:70)
      at TestingInjector.instantiateClass (../node_modules/@nestjs/core/injector/injector.js:354:55)
      at callback (../node_modules/@nestjs/core/injector/injector.js:56:45)
      at TestingInjector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:136:24)
      at TestingInjector.loadInstance (../node_modules/@nestjs/core/injector/injector.js:61:13)
      at TestingInjector.loadProvider (../node_modules/@nestjs/core/injector/injector.js:88:9)
      at ../node_modules/@nestjs/core/injector/instance-loader.js:56:13
          at async Promise.all (index 3)


Can anyone help me identify why this is happening?
Here is a section of the package.json file:
"dependencies": {
    "@nest-modules/mailer": "^1.1.3",
    "@nestjs/cache-manager": "^2.2.0",
    "@nestjs/common": "^9.4.3",
    "@nestjs/core": "^9.4.3",
    "@nestjs/jwt": "^10.0.1",
    "@nestjs/passport": "^6.1.0",
    "@nestjs/platform-express": "^9.4.3",
    "@nestjs/schedule": "^0.4.3",
    "@nestjs/serve-static": "^2.2.2",
    "@types/nodemailer": "^6.4.4",
    "JSONStream": "^1.3.5",
    "ajv": "^6.12.6",
    "argon2": "^0.30.3",
    "cache-manager": "^5.4.0",
    "bcryptjs": "^2.4.3",
    "chalk": "^2.4.2",
    "class-transformer": "^0.3.1",
    "class-validator": "^0.14.0",
    "csv-parser": "^3.0.0",
    "exceljs": "^4.3.0",
    "faker": "^4.1.0",
    "handlebars": "^4.7.7",
    "hashids": "^2.0.0",
    "inquirer": "^7.0.0",
    "jest-junit": "^8.0.0",
    "js-joda": "^1.11.0",
    "json2csv": "^5.0.6",
    "knex": "^0.21.1",
    "nodemailer": "^6.7.2",
    "npm": "^8.19.2",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.1",
    "pg": "^8.9.0",
    "pg-query-stream": "^4.2.3",
    "pkg-dir": "^4.2.0",
    "ramda": "^0.26.1",
    "reflect-metadata": "^0.1.13",
    "remove-accents": "^0.4.2",
    "rimraf": "^3.0.0",
    "shelljs": "^0.8.5",
    "sinon": "^13.0.1",
    "stream-json": "^1.7.4",
    "ts-loader": "^6.0.4",
    "ts-node": "^8.3.0",
    "tsconfig-paths": "^3.8.0",
    "typescript": "^3.6.2",
    "xlsx": "^0.17.1",
    "yargs-parser": "^21.0.1"
  },
  "devDependencies": {
    "@nestjs/testing": "^9.4.3",
    "@types/bcryptjs": "^2.4.2",
    "@types/cron": "^1.7.2",
    "@types/dotenv": "^6.1.1",
    "@types/express": "^4.17.1",
    "@types/faker": "^4.1.5",
    "@types/inquirer": "^6.5.0",
    "@types/jest": "^29.5.11",
    "@types/knex": "^0.16.1",
    "@types/node": "^12.7.4",
    "@types/nodemailer": "^6.4.4",
    "@types/pg": "^7.11.1",
    "@types/puppeteer": "^1.19.1",
    "@types/rimraf": "^2.0.2",
    "@types/shelljs": "^0.8.5",
    "@types/supertest": "^2.0.8",
    "chalk": "^2.4.2",
    "faker": "^4.1.0",
    "inquirer": "^7.0.0",
    "jest": "^24.9.0",
    "nodemon": "^2.0.19",
    "prettier": "^1.18.2",
    "rimraf": "^3.0.0",
    "shelljs": "^0.8.5",
    "supertest": "^4.0.2",
    "ts-jest": "^24.0.2",
    "ts-loader": "^6.0.4",
    "ts-node": "^8.3.0",
    "tsconfig-paths": "^3.8.0",
    "tslint": "5.19.0",
    "tslint-config-prettier": "^1.18.0",
    "typescript": "^3.6.2",
    "webpack": "^4.39.3",
    "webpack-cli": "^3.3.7",
    "webpack-node-externals": "^1.7.2"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts",
      "util"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "globalSetup": "../test/global-setup.ts",
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  },
  "engines": {
    "node": "12.19.0",
    "npm": "6.10.2"
  }

Here is the setup for the test that is failing
import { CacheModule, CACHE_MANAGER } from '@nestjs/cache-manager';
import { HttpServer, INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';

import { AppModule } from '../../../src/app.module';
import { getAuthorizationHeaders } from '../../../test/e2e.utils';
import { SessionModule } from '../session/session.module';

import { PreferencesModule } from './preferences.module';

describe('Preferences (e2e)', () => {
  let app: INestApplication;
  let server: HttpServer;
  let serverRequest;
  let authorizationHeaders;

  beforeAll(async () => {
    const moduleFixture = await Test.createTestingModule({
      imports: [PreferencesModule, SessionModule, CacheModule],
      providers: [{ provide: CACHE_MANAGER, useValue: {} }]
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
    server = await app.getHttpServer();
    serverRequest = await request(server);
    authorizationHeaders = await getAuthorizationHeaders(serverRequest);
  });


The issue was with the versions of various npm packages.
These were the relevant dependencies that worked:
"cache-manager-redis-store": "^3.0.1"
"@nestjs/cache-manager": "^2.2.0"

Along with these dev dependencies:
"@types/cache-manager-redis-store": "^2.0.4"
"jest": "^27.5.1"
"ts-node": "^10.9.2"
"typescript": "^4.9.5"

Having the correct typescript version was important. The cache-manager-reids-store makes use of the nullish coalescing operator (??) in several places, and my older version of typescript was complaining bitterly about it.
