Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been trying to resolve the two issues, but however I tried to check and fix all codes and the location of each repository, they are not solved.

The first error
FAIL src/users/users.service.spec.ts ● UsersService › should be defined Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument "UserRepository" at index [0] is available in the RootTestModule context. Potential solutions: - Is RootTestModule a valid NestJS module? - If "UserRepository" is a provider, is it part of the current RootTestModule? - If "UserRepository" is exported from a separate u/Module, is that module imported within RootTestModule? u/Module({ imports: [ /* the Module containing "UserRepository" */ ] }) 6 | 7 | beforeEach(async () => { > 8 | const module: TestingModule = await Test.createTestingModule({ | ^ 9 | providers: [UsersService], 10 | }).compile(); 11 | at TestingInjector.lookupComponentInParentModules (../node_modules/@nestjs/core/injector/injector.js:254:19) at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/core/injector/injector.js:207:33) at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/testing/testing-injector.js:19:45) at resolveParam (../node_modules/@nestjs/core/injector/injector.js:128:38) at async Promise.all (index 0) at TestingInjector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:143:27) at TestingInjector.loadInstance (../node_modules/@nestjs/core/injector/injector.js:70:13) at TestingInjector.loadProvider (../node_modules/@nestjs/core/injector/injector.js:97:9) at ../node_modules/@nestjs/core/injector/instance-loader.js:56:13 at async Promise.all (index 3) at TestingInstanceLoader.createInstancesOfProviders (../node_modules/@nestjs/core/injector/instance-loader.js:55:9) at ../node_modules/@nestjs/core/injector/instance-loader.js:40:13 at async Promise.all (index 1) at TestingInstanceLoader.createInstances (../node_modules/@nestjs/core/injector/instance-loader.js:39:9) at TestingInstanceLoader.createInstancesOfDependencies (../node_modules/@nestjs/core/injector/instance-loader.js:22:13) at TestingInstanceLoader.createInstancesOfDependencies (../node_modules/@nestjs/testing/testing-instance-loader.js:9:9) at TestingModuleBuilder.createInstancesOfDependencies (../node_modules/@nestjs/testing/testing-module.builder.js:118:9) at TestingModuleBuilder.compile (../node_modules/@nestjs/testing/testing-module.builder.js:74:9) at Object.<anonymous> (users/users.service.spec.ts:8:35)

users.service.spec.ts
TypeScript
import { Test, TestingModule } from '@nestjs/testing'; import { UsersService } from './users.service'; describe('UsersService', () => { let service: UsersService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [UsersService], }).compile(); service = module.get<UsersService>(UsersService); }); it('should be defined', () => { expect(service).toBeDefined(); }); });

The second error
Nest can't resolve dependencies of the UsersController (?, AuthService). Please make sure that the argument UsersService at index [0] is available in the RootTestModule context. Potential solutions: - Is RootTestModule a valid NestJS module? - If UsersService is a provider, is it part of the current RootTestModule? - If UsersService is exported from a separate u/Module, is that module imported within RootTestModule? u/Module({ imports: [ /* the Module containing UsersService */ ] }) 6 | 7 | beforeEach(async () => { > 8 | const module: TestingModule = await Test.createTestingModule({ | ^ 9 | controllers: [UsersController], 10 | }).compile(); 11 | at TestingInjector.lookupComponentInParentModules (../node_modules/@nestjs/core/injector/injector.js:254:19) at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/core/injector/injector.js:207:33) at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/testing/testing-injector.js:19:45) at resolveParam (../node_modules/@nestjs/core/injector/injector.js:128:38) at async Promise.all (index 0) at TestingInjector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:143:27) at TestingInjector.loadInstance (../node_modules/@nestjs/core/injector/injector.js:70:13) at TestingInjector.loadController (../node_modules/@nestjs/core/injector/injector.js:88:9) at ../node_modules/@nestjs/core/injector/instance-loader.js:68:13 at async Promise.all (index 0) at TestingInstanceLoader.createInstancesOfControllers (../node_modules/@nestjs/core/injector/instance-loader.js:67:9) at ../node_modules/@nestjs/core/injector/instance-loader.js:42:13 at async Promise.all (index 1) at TestingInstanceLoader.createInstances (../node_modules/@nestjs/core/injector/instance-loader.js:39:9) at TestingInstanceLoader.createInstancesOfDependencies (../node_modules/@nestjs/core/injector/instance-loader.js:22:13) at TestingInstanceLoader.createInstancesOfDependencies (../node_modules/@nestjs/testing/testing-instance-loader.js:9:9) at TestingModuleBuilder.createInstancesOfDependencies (../node_modules/@nestjs/testing/testing-module.builder.js:118:9) at TestingModuleBuilder.compile (../node_modules/@nestjs/testing/testing-module.builder.js:74:9) at Object.<anonymous> (users/users.controller.spec.ts:8:35)

users.controller.spec.ts
TypeScript
import { Test, TestingModule } from '@nestjs/testing'; import { AppController } from './app.controller'; import { AppService } from './app.service'; describe('AppController', () => { let appController: AppController; beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], providers: [AppService], }).compile(); appController = app.get<AppController>(AppController); }); describe('root', () => { it('should return "Hello World!"', () => { expect(appController.getHello()).toBe('Hello World!'); }); }); });


What I have tried:

I wrote the below code
TypeScript
  1  import { DEFAULT_FACTORY_CLASS_METHOD_KEY } from '@nestjs/common/module-utils/constants';
  2  
  3  
  4  const moduleMocker = new ModuleMocker(global);
  5  
  6  describe('CatsController', () => {
  7    let controller: UsersController;
  8  
  9    beforeEach(async () => {
 10      const moduleRef = await Test.createTestingModule({
 11        controllers: [UsersController],
 12      })
 13        .useMocker((token) => {
 14          const results = ['test1', 'test2'];
 15          if (token === UsersService) {
 16            return { findAll: jest.fn().mockResolvedValue(results) };
 17          }
 18          if (typeof token === 'function') {
 19            const mockMetadata = moduleMocker.getMetadata(token) as MockFunctionMetadata<any, any="">;
 20            const Mock = moduleMocker.generateFromMetadata(mockMetadata);
 21            return new Mock();
 22          }
 23        })
 24        .compile();
 25  
 26      controller = moduleRef.get(UsersController);
 27    });
 28  });
Posted
Updated 13-Nov-23 21:46pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900