nest-modules / ioredis

:see_no_evil: :hear_no_evil: :speak_no_evil: A ioredis module for Nest framework (node.js)
MIT License
145 stars 31 forks source link

Impossible to mock in unit tests #286

Open MFrat opened 3 months ago

MFrat commented 3 months ago

It is not possible to mock @InjectRedis() private readonly redis: Redis in unit tests. The following error occurs:

ENOENT: no such file or directory, open 'node:child_process'

      at Runtime.readFile (node_modules/jest-runtime/build/index.js:1987:21)
      at Object.<anonymous> (node_modules/check-disk-space/dist/check-disk-space.cjs:5:26)
jleverenz commented 3 months ago

You might need to provide some additional context, like what you've tried in your unit test. I've setup a pattern along these lines with some success:

import { Test } from '@nestjs/testing';
import { getRedisConnectionToken } from '@nestjs-modules/ioredis';

describe('YourService', () => {
  const redisMock = {}; // replace with your mock

  beforeEach(async () => {
    const moduleRef = await Test.createTestingModule({
      providers: [
        {
          provide: getRedisConnectionToken(),
          useValue: redisMock,
        },
        YourService,
      ],
    }).compile();
  });
  // ...
});