Skip to content

Commit

Permalink
test(core): add multiple providers resolution integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 7, 2022
1 parent ba75fd7 commit a8e5bc6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
49 changes: 49 additions & 0 deletions integration/injector/e2e/multiple-providers.spec.ts
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']);
});
});
});
16 changes: 16 additions & 0 deletions integration/injector/src/multiple-providers/a.module.ts
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 {}
16 changes: 16 additions & 0 deletions integration/injector/src/multiple-providers/b.module.ts
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 {}
16 changes: 16 additions & 0 deletions integration/injector/src/multiple-providers/c.module.ts
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 {}
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 {}

0 comments on commit a8e5bc6

Please sign in to comment.