-
-
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.
Merge branch 'master' into kafka-commit-offsets
- Loading branch information
Showing
158 changed files
with
5,055 additions
and
1,533 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ node_modules/ | |
/.idea | ||
/.awcache | ||
/.vscode | ||
/.devcontainer | ||
*.code-workspace | ||
|
||
# bundle | ||
|
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
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
96 changes: 96 additions & 0 deletions
96
integration/injector/e2e/optional-factory-provider-dep.spec.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,96 @@ | ||
import { UnknownDependenciesException } from '@nestjs/core/errors/exceptions/unknown-dependencies.exception'; | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Optional factory provider deps', () => { | ||
describe('when dependency is optional', () => { | ||
describe('and it is available', () => { | ||
it('then it should be injected into the factory function', async () => { | ||
const defaultValue = 'DEFAULT_VALUE'; | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: 'FACTORY', | ||
useFactory: dep => dep ?? defaultValue, | ||
inject: [{ token: 'MISSING_DEP', optional: true }], | ||
}, | ||
{ provide: 'MISSING_DEP', useValue: 'OPTIONAL_DEP_VALUE' }, | ||
], | ||
}).compile(); | ||
|
||
const factoryProvider = moduleRef.get('FACTORY'); | ||
expect(factoryProvider).to.equal('OPTIONAL_DEP_VALUE'); | ||
}); | ||
}); | ||
describe('otherwise', () => { | ||
it('"undefined" should be injected into the factory function', async () => { | ||
const defaultValue = 'DEFAULT_VALUE'; | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: 'FACTORY', | ||
useFactory: dep => dep ?? defaultValue, | ||
inject: [{ token: 'MISSING_DEP', optional: true }], | ||
}, | ||
], | ||
}).compile(); | ||
|
||
const factoryProvider = moduleRef.get('FACTORY'); | ||
expect(factoryProvider).to.equal(defaultValue); | ||
}); | ||
}); | ||
}); | ||
describe('otherwise', () => { | ||
describe('and dependency is not registered', () => { | ||
it('should error out', async () => { | ||
try { | ||
const builder = Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: 'FACTORY', | ||
useFactory: () => 'RETURNED_VALUE', | ||
inject: ['MISSING_DEP'], | ||
}, | ||
], | ||
}); | ||
await builder.compile(); | ||
} catch (err) { | ||
expect(err).to.be.instanceOf(UnknownDependenciesException); | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('and dependency is registered but it cannot be instantiated', () => { | ||
it('should error out', async () => { | ||
try { | ||
const builder = Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: 'POSSIBLY_MISSING_DEP', | ||
useFactory: () => null, | ||
inject: ['MISSING_DEP'], | ||
}, | ||
{ | ||
provide: 'FACTORY', | ||
useFactory: () => 'RETURNED_VALUE', | ||
inject: [{ token: 'POSSIBLY_MISSING_DEP', optional: false }], | ||
}, | ||
], | ||
}); | ||
await builder.compile(); | ||
} catch (err) { | ||
expect(err).to.be.instanceOf(UnknownDependenciesException); | ||
expect(err.message).to | ||
.equal(`Nest can't resolve dependencies of the POSSIBLY_MISSING_DEP (?). Please make sure that the argument MISSING_DEP at index [0] is available in the RootTestModule context. | ||
Potential solutions: | ||
- If MISSING_DEP is a provider, is it part of the current RootTestModule? | ||
- If MISSING_DEP is exported from a separate @Module, is that module imported within RootTestModule? | ||
@Module({ | ||
imports: [ /* the Module containing MISSING_DEP */ ] | ||
}) | ||
`); | ||
} | ||
}); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
integration/injector/src/inject/inject-same-name.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,16 @@ | ||
import { Module, Injectable, Inject } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
class CoreService { | ||
constructor(@Inject(CoreService.name) private readonly coreService: any) {} | ||
} | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: CoreService.name, | ||
useValue: 'anything', | ||
}, | ||
], | ||
}) | ||
export class InjectSameNameModule {} |
40 changes: 40 additions & 0 deletions
40
integration/injector/src/self-injection/self-injection-provider.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,40 @@ | ||
import { Module, Injectable, Inject, forwardRef } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
class ServiceInjectingItself { | ||
constructor(private readonly coreService: ServiceInjectingItself) {} | ||
} | ||
|
||
@Injectable() | ||
class ServiceInjectingItselfForwared { | ||
constructor( | ||
@Inject(forwardRef(() => ServiceInjectingItself)) | ||
private readonly coreService: ServiceInjectingItself, | ||
) {} | ||
} | ||
|
||
@Injectable() | ||
class ServiceInjectingItselfViaCustomToken { | ||
constructor(@Inject('AnotherToken') private readonly coreService: any) {} | ||
} | ||
|
||
@Module({ | ||
providers: [ServiceInjectingItself], | ||
}) | ||
export class SelfInjectionProviderModule {} | ||
|
||
@Module({ | ||
providers: [ServiceInjectingItselfForwared], | ||
}) | ||
export class SelfInjectionForwardProviderModule {} | ||
|
||
@Module({ | ||
providers: [ | ||
ServiceInjectingItselfViaCustomToken, | ||
{ | ||
provide: 'AnotherToken', | ||
useClass: ServiceInjectingItselfViaCustomToken, | ||
}, | ||
], | ||
}) | ||
export class SelfInjectionProviderCustomTokenModule {} |
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
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
Oops, something went wrong.