-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add async module configuration
BREAKING CHANGE: `QueueModule.forRoot()` connection argument interface was restructured and `AMQPService.getConnectionOptions()` was renamed to `AMQPService.getModuleOptions()`.
- Loading branch information
Showing
21 changed files
with
490 additions
and
122 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './queue.const'; |
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,3 @@ | ||
export const QUEUE_MODULE_OPTIONS = 'QueueModuleOptions'; | ||
export const AMQP_CLIENT_TOKEN = 'AMQP_CLIENT'; | ||
export const AMQP_CONNECTION_RECONNECT = 'amqp_connection_reconnect'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './amqp-connection-options.interface'; | ||
export * from './queue.interface'; | ||
export * from './queue-options.interface'; |
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,14 @@ | ||
import { ModuleMetadata, Type } from '@nestjs/common'; | ||
|
||
import { QueueModuleOptions } from './index'; | ||
|
||
export interface QueueModuleOptionsFactory { | ||
createQueueModuleOptions(): Promise<QueueModuleOptions> | QueueModuleOptions; | ||
} | ||
|
||
export interface QueueModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> { | ||
useExisting?: Type<QueueModuleOptionsFactory>; | ||
useClass?: Type<QueueModuleOptionsFactory>; | ||
useFactory?: (...args: any[]) => Promise<QueueModuleOptions> | QueueModuleOptions; | ||
inject?: any[]; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { Injectable, Module } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
jest.mock('rhea-promise'); | ||
|
||
import { QueueModuleOptions, QueueModuleOptionsFactory } from './interface'; | ||
import { QueueModule } from './queue.module'; | ||
import { AMQPService, QueueService } from './service'; | ||
|
||
describe('QueueModule', () => { | ||
const connectionUri = 'amqp://localhost:5672'; | ||
const moduleOptions: QueueModuleOptions = { | ||
connectionUri, | ||
}; | ||
const originalModuleProviders = (QueueModule as any).moduleDefinition.providers; | ||
let module: TestingModule; | ||
|
||
@Injectable() | ||
class TestForFeatureService { | ||
constructor(public readonly queueService: QueueService) {} | ||
} | ||
|
||
@Module({ | ||
imports: [QueueModule.forFeature()], | ||
providers: [TestForFeatureService], | ||
exports: [TestForFeatureService], | ||
}) | ||
class TestForFeatureModule {} | ||
|
||
@Injectable() | ||
class TestConfigService { | ||
public getAmqpUrl(): string { | ||
return connectionUri; | ||
} | ||
} | ||
|
||
@Module({ | ||
providers: [TestConfigService], | ||
exports: [TestConfigService], | ||
}) | ||
class TestConfigModule {} | ||
|
||
@Injectable() | ||
class TestQueueConfigService implements QueueModuleOptionsFactory { | ||
public async createQueueModuleOptions(): Promise<QueueModuleOptions> { | ||
return { connectionUri }; | ||
} | ||
} | ||
|
||
@Module({ | ||
providers: [TestQueueConfigService], | ||
exports: [TestQueueConfigService], | ||
}) | ||
class TestQueueConfigModule {} | ||
|
||
afterEach(async () => { | ||
await module.close(); | ||
(QueueModule as any).moduleDefinition.imports = []; | ||
(QueueModule as any).moduleDefinition.providers = originalModuleProviders; | ||
}); | ||
|
||
describe('forRoot()', () => { | ||
it('should import as sync root module', async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [QueueModule.forRoot(connectionUri)], | ||
}).compile(); | ||
const amqpService = module.get<AMQPService>(AMQPService); | ||
|
||
expect(amqpService.getModuleOptions()).toEqual(moduleOptions); | ||
}); | ||
}); | ||
|
||
describe('forFeature()', () => { | ||
it('should import as feature module', async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [QueueModule.forRoot(connectionUri), TestForFeatureModule], | ||
}).compile(); | ||
const forFeatureTestService = module.get<TestForFeatureService>(TestForFeatureService); | ||
|
||
expect((forFeatureTestService.queueService as any).amqpService.getModuleOptions()).toEqual(moduleOptions); | ||
}); | ||
}); | ||
|
||
describe('forRootAsync()', () => { | ||
it(`should import as sync module with 'useFactory'`, async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ | ||
QueueModule.forRootAsync({ | ||
useFactory: () => ({ connectionUri }), | ||
}), | ||
], | ||
}).compile(); | ||
const amqpService = module.get<AMQPService>(AMQPService); | ||
|
||
expect(amqpService.getModuleOptions()).toEqual({ connectionUri }); | ||
}); | ||
|
||
it(`should import as async module with 'useFactory'`, async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ | ||
QueueModule.forRootAsync({ | ||
imports: [TestConfigModule], | ||
inject: [TestConfigService], | ||
useFactory: (testConfigService: TestConfigService) => ({ | ||
connectionUri: testConfigService.getAmqpUrl(), | ||
}), | ||
}), | ||
], | ||
}).compile(); | ||
const amqpService = module.get<AMQPService>(AMQPService); | ||
|
||
expect(amqpService.getModuleOptions()).toEqual({ connectionUri }); | ||
}); | ||
|
||
it(`should import as async module with 'useClass'`, async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ | ||
QueueModule.forRootAsync({ | ||
imports: [TestQueueConfigModule], | ||
useClass: TestQueueConfigService, | ||
}), | ||
], | ||
}).compile(); | ||
const amqpService = module.get<AMQPService>(AMQPService); | ||
|
||
expect(amqpService.getModuleOptions()).toEqual({ connectionUri }); | ||
}); | ||
|
||
it(`should import as async module with 'useExisting'`, async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ | ||
QueueModule.forRootAsync({ | ||
imports: [TestQueueConfigModule], | ||
useExisting: TestQueueConfigService, | ||
}), | ||
], | ||
}).compile(); | ||
const amqpService = module.get<AMQPService>(AMQPService); | ||
|
||
expect(amqpService.getModuleOptions()).toEqual({ connectionUri }); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.