Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions integration/injector/e2e/inherited-optional-dependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Injectable, mixin, Module, Optional } from '@nestjs/common';
import { Test } from '@nestjs/testing';

import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { expect } from 'chai';
import { UnknownDependenciesException } from '@nestjs/core/errors/exceptions/unknown-dependencies.exception';

chai.use(chaiAsPromised);

describe('Inherited optional dependency', () => {
const Foo = () => {
class FooMixin {
constructor(@Optional() option: any) {}
}
return mixin(FooMixin);
};

@Injectable()
class NeededService {
exec() {
return 'exec';
}
}

@Module({
providers: [NeededService],
exports: [NeededService],
})
class SomeModule {}

@Injectable()
class FooService extends Foo() {
constructor(private readonly neededService: NeededService) {
super();
}

doSomething() {
return this.neededService.exec();
}
}

@Module({
imports: [],
providers: [FooService],
exports: [FooService],
})
class FooModule {}

/**
* You can see details on this issue here: https://github.com/nestjs/nest/issues/2581
*/
it('should remove optional dependency metadata inherited from base class', async () => {
const module = Test.createTestingModule({
imports: [SomeModule, FooModule],
});

await expect(
module.compile(),
).to.eventually.be.rejected.and.be.an.instanceOf(
UnknownDependenciesException,
);
});
});
2 changes: 1 addition & 1 deletion packages/core/injector/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export class Injector {
}

public reflectOptionalParams<T>(type: Type<T>): any[] {
return Reflect.getMetadata(OPTIONAL_DEPS_METADATA, type) || [];
return Reflect.getOwnMetadata(OPTIONAL_DEPS_METADATA, type) || [];
}

public reflectSelfParams<T>(type: Type<T>): any[] {
Expand Down