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.

Testing controllers (services) with DI for FastifyInstance

See original GitHub issue

Hey!

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:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
L2jLigacommented, Nov 29, 2020

Added an option to access controller instance in tests, also reworked documentation:

https://github.com/L2jLiga/fastify-decorators/blob/v3/docs/Testing.md

1reaction
L2jLigacommented, Nov 23, 2020

I would keep it open to not forget about documentation improvements and returning controller instance

Read more comments on GitHub >

github_iconTop 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 >

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