-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): add multiple providers resolution integration tests
- Loading branch information
1 parent
ba75fd7
commit a8e5bc6
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import { MultipleProvidersModule } from '../src/multiple-providers/multiple-providers.module'; | ||
|
||
describe('Multiple providers under the same token ("each" feature)', () => { | ||
describe('get()', () => { | ||
it('should return an array of providers', async () => { | ||
const builder = Test.createTestingModule({ | ||
imports: [MultipleProvidersModule], | ||
}); | ||
const testingModule = await builder.compile(); | ||
|
||
const multiProviderInstances = testingModule.get<string>( | ||
'MULTI_PROVIDER', | ||
{ | ||
each: true, | ||
}, | ||
); | ||
|
||
// make sure "multiProviderInstances" is string[] not string | ||
// @ts-expect-error | ||
multiProviderInstances.charAt; | ||
|
||
expect(multiProviderInstances).to.be.eql(['A', 'B', 'C']); | ||
}); | ||
}); | ||
describe('resolve()', () => { | ||
it('should return an array of providers', async () => { | ||
const builder = Test.createTestingModule({ | ||
imports: [MultipleProvidersModule], | ||
}); | ||
const testingModule = await builder.compile(); | ||
|
||
const multiProviderInstances = await testingModule.resolve<string>( | ||
'REQ_SCOPED_MULTI_PROVIDER', | ||
undefined, | ||
{ | ||
each: true, | ||
}, | ||
); | ||
|
||
// make sure "multiProviderInstances" is string[] not string | ||
// @ts-expect-error | ||
multiProviderInstances.charAt; | ||
|
||
expect(multiProviderInstances).to.be.eql(['A', 'B', 'C']); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Module, Scope } from '@nestjs/common'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: 'MULTI_PROVIDER', | ||
useValue: 'A', | ||
}, | ||
{ | ||
provide: 'REQ_SCOPED_MULTI_PROVIDER', | ||
useFactory: () => 'A', | ||
scope: Scope.REQUEST, | ||
}, | ||
], | ||
}) | ||
export class AModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Module, Scope } from '@nestjs/common'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: 'MULTI_PROVIDER', | ||
useValue: 'B', | ||
}, | ||
{ | ||
provide: 'REQ_SCOPED_MULTI_PROVIDER', | ||
useFactory: () => 'B', | ||
scope: Scope.REQUEST, | ||
}, | ||
], | ||
}) | ||
export class BModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Module, Scope } from '@nestjs/common'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: 'MULTI_PROVIDER', | ||
useValue: 'C', | ||
}, | ||
{ | ||
provide: 'REQ_SCOPED_MULTI_PROVIDER', | ||
useFactory: () => 'C', | ||
scope: Scope.REQUEST, | ||
}, | ||
], | ||
}) | ||
export class CModule {} |
9 changes: 9 additions & 0 deletions
9
integration/injector/src/multiple-providers/multiple-providers.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AModule } from './a.module'; | ||
import { BModule } from './b.module'; | ||
import { CModule } from './c.module'; | ||
|
||
@Module({ | ||
imports: [AModule, BModule, CModule], | ||
}) | ||
export class MultipleProvidersModule {} |