Skip to content
Open
Show file tree
Hide file tree
Changes from all 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.spec.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 { UnknownDependenciesException } from '@nestjs/core/errors/exceptions/unknown-dependencies.exception';
import { expect } from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);

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

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

const Foo = () => {
class FooMixin {
constructor(@Optional() option: any) {}
}
return mixin(FooMixin);
};

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

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

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

describe('Inherited optional dependency', () => {
/**
* You can see details on this issue here: https://github.com/nestjs/nest/issues/2581
*/
describe('when the parent has an @Optional() parameter', () => {
it('should throw an UnknownDependenciesException due to the missing dependency', async () => {
const module = Test.createTestingModule({
imports: [NeededModule, 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