Testing controllers (services) with DI for FastifyInstance
See original GitHub issueHey!
First of all, thank you for this amazing project!
I’m having a bit of trouble testing a controller which has DI for FastifyInstance, st like this:
@Inject(FastifyInstanceToken)
private instance!: FastifyInstance;
However, I cant seem to get it working with configureControllerTest:
TypeError: Invalid argument provided for "MyController.instance". Expected class annotated with @Service.
at injectProperties (lib/decorators/helpers/inject-dependencies.ts:39:19)
at Object.createWithInjectedDependencies (lib/decorators/helpers/inject-dependencies.ts:29:5)
at Object.0 (lib/decorators/strategies/controller-type.ts:42:36)
at instance.register.prefix (lib/decorators/controller.ts:40:85)
at Plugin.Object.<anonymous>.Plugin.exec (node_modules/avvio/plugin.js:131:17)
at Boot.loadPlugin (node_modules/avvio/plugin.js:266:10)
at Task.release (node_modules/fastq/queue.js:143:16)
at worked (node_modules/fastq/queue.js:195:10)
at node_modules/avvio/plugin.js:269:7
at done (node_modules/avvio/plugin.js:201:5)
Also, as a side note, It might be good to mention somewhere in the docs, for whoever is using ts-jest, that they need to properly set up jest environment, i.e. the crucial part:
require('reflect-metadata');
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Testing - Fastify
Testing is one of the most important parts of developing an application. Fastify is very flexible when it comes to testing and is...
Read more >Test your Fastify REST API With Node Tap - YouTube
In this video, we test a Fastify REST API with Node Tap. You will learn how to mock imports and test against a...
Read more >How to test a controller with a service that has @Inject in its ...
I have a question regarding testing as described on https://docs.nestjs.com/advanced/unit-testing: beforeEach(async () => { const module ...
Read more >memberid/fastify-di v0.4.2 - npm.io
This package Dependency Injection (DI) mechanism will load all controllers, routes, services, and register them to fastify instance.
Read more >Unit test controller logic in ASP.NET Core - Microsoft Learn
A controller unit test avoids scenarios such as filters, routing, and model binding. Tests that cover the interactions among components that ...
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 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

Added an option to access controller instance in tests, also reworked documentation:
https://github.com/L2jLiga/fastify-decorators/blob/v3/docs/Testing.md
I would keep it open to not forget about documentation improvements and returning controller instance