Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to mock in unit tests #286

Open
MFrat opened this issue Aug 17, 2024 · 1 comment
Open

Impossible to mock in unit tests #286

MFrat opened this issue Aug 17, 2024 · 1 comment

Comments

@MFrat
Copy link

MFrat commented Aug 17, 2024

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
Copy link
Contributor

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();
  });
  // ...
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants