You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using DiTest / PlatformTest, classes registered using a "type" instead of "provide" key in order to allow multi-instance injection. This functionality seems to work when running the actual server instance, but when writing tests using injection, these instances don't seem to be resolved.
Inside a test, I'm unable to create an instance of the ApiClient using DiTest.invoke or PlatformTest.invoke. Either one fails the same way, either some crypticTypeError ReflectMetadata error or the result being undefined.
Scenario
I have several instances of an API client that I want to inject into a service. I've decorated them all with a decorator like so
Run the following code snippet (using Jest framework).
import{DITest}from"@tsed/common";import{Inject,Injectable}from"@tsed/di";describe("FakeCrmClient",()=>{beforeEach(async()=>awaitDITest.create());afterEach(async()=>awaitDITest.reset());it("Should return a client",async()=>{consttoken=Symbol("token_provide");
@Injectable({provide: token,})classFakeCrmClient{}constit=awaitDITest.invoke(token);expect(it).toBeInstanceOf(FakeCrmClient);});it("Should inject a client",async()=>{consttoken=Symbol("token_type");interfaceFoo{}
@Injectable({type: token,})classFakeCrmClientimplementsFoo{}
@Injectable({type: token,})classFakeCrmClient2implementsFoo{}
@Injectable()classFakeService{constructor(@Inject(token)publicinstances: Foo[]){}}constservice=awaitDITest.invoke(FakeService);expect(service).toBeInstanceOf(FakeService);expect(service.instances[0]).toBeInstanceOf(FakeCrmClient);});it("Should return a client",async()=>{consttoken=Symbol("token_type");
@Injectable({type: token,})classFakeCrmClient{}constit=awaitDITest.invoke<FakeCrmClient[]>(token);expect(it).toBeInstanceOf(FakeCrmClient);});});
The last two test fail in a similar manner. Something to the effect of
Error: expect(received).toBeInstanceOf(expected)
Expected constructor: FakeCrmClient
Received value has no prototype
Received value: undefined
at Object.<anonymous> (/Users/me/Development/myproject/api/src/packages/crm/clients/FakeCrmClient.spec.ts:41:38)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
I have also seen the following error but I can't figure out exactly what circumstances produce this vs the previous error
TypeError at Reflect.getMetadata (/Users/me/Development/myproject/api/node_modules/reflect-metadata/Reflect.js:354:23)
at Function.getParamTypes (/Users/me/Development/myproject/api/node_modules/@tsed/core/src/domain/Metadata.ts:488:20)
at InjectorService.mapInvokeOptions (/Users/me/Development/myproject/api/node_modules/@tsed/di/src/services/InjectorService.ts:704:31)
at InjectorService.resolve (/Users/me/Development/myproject/api/node_modules/@tsed/di/src/services/InjectorService.ts:601:31)
at InjectorService.invoke (/Users/me/Development/myproject/api/node_modules/@tsed/di/src/services/InjectorService.ts:202:23)
at Function.invoke (/Users/me/Development/myproject/api/node_modules/@tsed/di/src/services/DITest.ts:99:46)
at Object.<anonymous> (/Users/me/Development/myproject/api/src/packages/crm/clients/FakeCrmClient.spec.ts:52:33)
at Promise.then.completed (/Users/me/Development/myproject/api/node_modules/jest-circus/build/utils.js:298:28)
at new Promise (<anonymous>)
at callAsyncCircusFn (/Users/me/Development/myproject/api/node_modules/jest-circus/build/utils.js:231:10)
at _callCircusTest (/Users/me/Development/myproject/api/node_modules/jest-circus/build/run.js:316:40)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async _runTest (/Users/me/Development/myproject/api/node_modules/jest-circus/build/run.js:252:3)
at async _runTestsForDescribeBlock (/Users/me/Development/myproject/api/node_modules/jest-circus/build/run.js:126:9)
at async _runTestsForDescribeBlock (/Users/me/Development/myproject/api/node_modules/jest-circus/build/run.js:121:9)
at async run (/Users/me/Development/myproject/api/node_modules/jest-circus/build/run.js:71:3)
at async runAndTransformResultsToJestFormat (/Users/me/Development/myproject/api/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
at async jestAdapter (/Users/me/Development/myproject/api/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
at async runTestInternal (/Users/me/Development/myproject/api/node_modules/jest-runner/build/runTest.js:367:16)
at async runTest (/Users/me/Development/myproject/api/node_modules/jest-runner/build/runTest.js:444:34)
Expected behavior
DiTest.invoke / PlatformTest.invoke should produce an instance or instances of a provider registered using the type property.
Code snippets
No response
Repository URL example
No response
OS
macOS
Node version
Node 18.13.1
Library version
7.69.4
Additional context
No response
The text was updated successfully, but these errors were encountered:
this is because Symbol("token_type"); is not considered by the DI as a Token, so you cannot use this symbol with .invoke method. If you want to retrieve all providers related to a group type, you have to use the special method getMany():
constproviders=DITest.injector.getMany(Symbol("token_type"))// or use the real token provider FakeCrmClientconstprovider=DITest.invoke(FakeCrmClient,[])
Describe the bug
When using DiTest / PlatformTest, classes registered using a "type" instead of "provide" key in order to allow multi-instance injection. This functionality seems to work when running the actual server instance, but when writing tests using injection, these instances don't seem to be resolved.
Inside a test, I'm unable to create an instance of the ApiClient using DiTest.invoke or PlatformTest.invoke. Either one fails the same way, either some cryptic
TypeError ReflectMetadata
error or the result being undefined.Scenario
I have several instances of an API client that I want to inject into a service. I've decorated them all with a decorator like so
To Reproduce
Run the following code snippet (using Jest framework).
The last two test fail in a similar manner. Something to the effect of
I have also seen the following error but I can't figure out exactly what circumstances produce this vs the previous error
Expected behavior
DiTest.invoke
/PlatformTest.invoke
should produce an instance or instances of a provider registered using thetype
property.Code snippets
No response
Repository URL example
No response
OS
macOS
Node version
Node 18.13.1
Library version
7.69.4
Additional context
No response
The text was updated successfully, but these errors were encountered: