question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In Jest UT, Nest can't resolve dependencies of the RedisService (?). Please make sure that the argument Symbol(REDIS_CLIENT) at index [0] is available in the RootTestModule context.

See original GitHub issue

I have written the simple UT in Jest and also used nestjs-redis module for redis operation when i run the UT i am getting following error, i tried to mock as fn() but getting same issue.

Nest can't resolve dependencies of the RedisService (?). Please make sure that the argument Symbol(REDIS_CLIENT) at index [0] is available in the RootTestModule context.

   Potential solutions:
   - If Symbol(REDIS_CLIENT) is a provider, is it part of the current RootTestModule?
   - If Symbol(REDIS_CLIENT) is exported from a separate @Module, is that module imported within RootTestModule?
     @Module({
       imports: [ /* the Module containing Symbol(REDIS_CLIENT) */ ]
     })

Below is the code

import { Test, TestingModule } from '@nestjs/testing';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { getModelToken } from '@nestjs/mongoose';
import { RedisService } from '../../shared/lib/redis/redis.service';
describe('CatsController', () => {
    let controller: CatsController;
    let service: CatsService;
    
  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [CatsController],
      providers: [CatsService, RedisService, { provide: getModelToken('Cats'), useValue: { Symbol: jest.fn()} }],
    }).compile();

    controller = app.get<CatsController>(CatsController);
    service = app.get<CatsService>(CatsService);
  });

  it('should be defined', () => {
    expect(controller).toBeDefined();
    expect(service).toBeDefined();
  });

  const catsData = [{
      cat_name: "cat",
      cat_type: "type",
      cat_color: "black"
  }]
  describe('Cats List', () => {


    it('should return all cats', async() => {
        jest.spyOn(service, 'getAll').mockResolvedValue({data: catsData, success: true})
        const catsList = await controller.findAll()
      expect(catsList).toBe({data: catsData, success: true});
    });

    it('should throw error record not found', async() => {
        jest.spyOn(service, 'getAll').mockRejectedValue({message: 'Records not found'})
        try{
            await controller.findAll();
          }catch(e){
            expect(e.message).toBe('Records not found');
          }
      });

  });
});

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
eharaj1commented, Oct 8, 2021

Hey @rjpkuyper ,

Many thanks, you saved my days.

0reactions
rjpkuypercommented, Oct 8, 2021

Your welcome!

Could you please close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nest can't resolve dependencies of the (?). Please make sure ...
Please make sure that the argument at index [0] is available in the RootTestModule context. This project is using Mongoose for connecting to ......
Read more >
"make sure that the argument HttpService at index [0] is ...
Nest can't resolve dependencies of the MylibService (?). ... argument HttpService at index [0] is available in the RootTestModule context.
Read more >
Redis - npm
A modern, high performance Redis client. Latest version: 4.5.1, last published: a month ago. Start using redis in your project by running ...
Read more >
please make sure that the argument cache_manager at index ...
Nest can't resolve dependencies of the CACHE_MANAGER (?). Please make sure that the argument at index [0] is available in the CacheModule context....
Read more >
Redis - Microservices - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found