How to test custom nestjs provider? #477
-
|
Hi! I have a custom provider that uses a Symbol as the key, and returns a non-class type (a fetch client). The typing makes me think I am not allowed to? I've already gotten the exact same provider using unitRef, which worked great: beforeAll(async () => {
const { unit, unitRef } =
await TestBed.solitary(SomeService).compile();
client = unitRef.get(SomeClientProviderKey);
service = unit;
});But now I want to do something like this: const { unit } =
await TestBed.solitary(SomeClientProviderKey).compile();And it's not letting me, saying it is not assignable to type Basically this: https://suites.dev/docs/developer-guide/adapters/identifiers but the closest example there uses a symbol to resolve a mocked (unitRef) instance. I want the actual thing. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
I've solved this by using nest's testing utilities instead. But I am still curious how I could do the same with suites :) |
Beta Was this translation helpful? Give feedback.
-
|
Hi @RWOverdijk The main point to note is that Suites is primarily intended for testing classes that have injected dependencies, rather than for testing providers themselves. The main function of Suites is to oversee the dependency graph for class-based components. For your goal of conducting a straightforward functional unit test of the provider, the testing utilities provided by NestJS are indeed more suitable, which clarifies why that method was effective for you. The utilities are tailored to manage testing for various providers, including those utilizing Symbol keys. Essentially, Suites excels when it comes to testing a class that has dependencies, but it isn't intended for testing the providers directly without the framework of dependency injection into a class. |
Beta Was this translation helpful? Give feedback.
Hi @RWOverdijk
The main point to note is that Suites is primarily intended for testing classes that have injected dependencies, rather than for testing providers themselves. The main function of Suites is to oversee the dependency graph for class-based components.
By applying
TestBed.solitary()you set a testing environment for a class that might have dependencies injected into it. TheUnitRefprovides access to the injected dependencies, allowing for configuration or verification.For your goal of conducting a straightforward functional unit test of the provider, the testing utilities provided by NestJS are indeed more suitable, which clarifies why that method was effective for you. The …