Unable to run tests because Nest can't resolve dependencies of a service
See original GitHub issueIβm submitting aβ¦
[ ] Regression
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
I have followed the unit test example but I am unable to get my very simple test to run (itβs literally just testing if true === true) but it wonβt work because Iβm met with this error
Nest can't resolve dependencies of the RoleService (?). Please verify whether [0] argument is available in the current context.
Minimal reproduction of the problem with instructions
You can find the repo at https://bitbucket.org/mogusbi/breeze-bb/
After cloning, run npm run api:test
// role.controller.spec.ts
import {Test} from '@nestjs/testing';
import {TestingModule} from '@nestjs/testing/testing-module';
import {RoleController} from './role.controller';
import {RoleService} from './role.service';
describe('Role controller', () => {
let controller: RoleController;
let service: RoleService;
beforeEach(async () => {
const mod: TestingModule = await Test
.createTestingModule({
components: [
RoleService
],
controllers: [
RoleController
]
})
.compile();
controller = mod.get<RoleController>(RoleController);
service = mod.get<RoleService>(RoleService);
});
it('should be true', () => {
expect(true).toBe(true);
});
});
// role.controller.ts
import {Controller} from '@nestjs/common';
import {RoleService} from './role.service';
@Controller('role')
export class RoleController {
constructor (
private readonly roleService: RoleService
) {}
...
}
// role.service.ts
import {InjectModel} from '@nestjs/mongoose';
import {PaginateModel} from 'mongoose';
import {IRole} from './role.interface';
import {RoleSchema} from './role.schema';
@Component()
export class RoleService {
constructor (
@InjectModel(RoleSchema) private readonly model: PaginateModel<IRole>
) {}
...
}
Environment
- Node version: 8.2.1
- Platform: Mac OS 10.13.2
βββ @nestjs/common@4.5.9
βββ @nestjs/core@4.5.9
βββ @nestjs/mongoose@2.0.0
βββ @nestjs/swagger@1.1.3
βββ @nestjs/testing@4.5.5
βββ @types/body-parser@1.16.8
βββ @types/express@4.11.0
βββ @types/jest@22.0.1
βββ @types/mongoose@4.7.32
βββ @types/mongoose-paginate@5.0.6
βββ @types/morgan@1.7.35
βββ @types/node@8.5.8
βββ body-parser@1.18.2
βββ class-transformer@0.1.8
βββ class-validator@0.7.3
βββ jest@22.0.6
βββ mongoose@4.13.9
βββ mongoose-paginate@5.0.3
βββ morgan@1.9.0
βββ nodemon@1.14.11
βββ reflect-metadata@0.1.10
βββ rxjs@5.5.6
βββ ts-jest@22.0.1
βββ ts-node@4.1.0
βββ tslint@5.9.1
βββ tslint-eslint-rules@4.1.1
βββ tslint-microsoft-contrib@5.0.1
βββ typescript@2.6.2
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:53 (9 by maintainers)
Top Results From Across the Web
Nest can't resolve dependencies in the RootTestModule ...
I have a nestjs project contains multiple apps and libs. When I run the test yarn jest --config ./jest.config.json libs/lib1 , it worksΒ ......
Read more >Common errors - FAQ - A progressive Node.js framework
"Cannot resolve dependency" error#. Probably the most common error message is about Nest not being able to resolve dependencies of a provider. The...
Read more >Nest can't resolve dependencies of the UsersService ... - Reddit
I did a small NestJs App with v9 and I'm getting this error. I can save my data in mi sqlite db, but...
Read more >nest can't resolve dependencies of the httpservice - You.com
I would like this module to provide a new HTTP service per request? problems that I am facing: When I try to add...
Read more >NestJS: Resolving Dependency Injection - Tevpro
Resolving a dependency inject issue in NestJS by changing the order of the files referenced ... Nest can't resolve dependencies of the AuthController...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top 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
Hey guys!
Iβve read the testing part of the database documentation but I could not find any hint or best practice for creating the
mockRepository
object.I would like to write tests where I mock the database layer of the service (e.g.: the
adminRepository
of theAuthService
) by providing good olβ POJOs as data source. Is this achievable with the current version of TypeORM / Nest?There is a related conversation in typeorm/typeorm#1267, but I couldnβt get closer to my desired functionality.
My AuthService looks like this:
The desired output would look like something like this:
I really look forward to hear some advices and tips about the pros/cons of this kind of testing strategy, my primary goal is to keep my unit tests as simple and as fast as I can. I really appreciate any help!
For people encoutering the same error message with TypeORM, please note that the DI loader cannot βknowβ your Repository instances as theyβre injected with
@InjectRepository
:I had more or less the same issue this morning and solved it by injecting βDogsRepositoryβ manually:
However, I would recommend extending Repository<Dog> with a custom repo Class (
class DogsRepository extends Repository<Dog>
) and use it in useClass (useClass: DogsRepository
, instead ofuseClass: Repository
), in order to be able to test the type of values returned by the repository instance (if you need to).@kamilmysliwiec: does everything sound correct? Or am I mistaken somewhere? Shall I add it do the documentation?