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 issueI 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:
- Created 2 years ago
- Comments:8
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @rjpkuyper ,
Many thanks, you saved my days.
Your welcome!
Could you please close the issue.