Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(rabbitmq): add testing section to rabbitmq README #736

Merged
Merged
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
80 changes: 80 additions & 0 deletions packages/rabbitmq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
- [Competing Consumers](#competing-consumers)
- [Handling errors](#handling-errors)
- [Handling errors during queue creation](#handling-errors-during-queue-creation)
- [Testing](#testing)
- [Prevent Connection Error](#prevent-connection-error)
- [Mock Connection](#mock-connection)
- [Contribute](#contribute)
- [License](#license)

Expand Down Expand Up @@ -601,6 +604,83 @@ You have the option to use `forceDeleteAssertQueueErrorHandler` which will try t

Obviously, you can also provide your own function and do whatever is best for you, in this case the function must return the name of the created queue.

## Testing

### Prevent Connection Error

Normally, you would not want to setup a RabbitMq broker locally or even connect to a external one during tests. So, first we have to prevent the connection to be attempted. This can be easily achieved by passing an `undefined` config to `RabbitMQModule.forRoot`:

```typescript
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';

@Module({
imports: [
RabbitMQModule.forRoot(
RabbitMQModule,
/** Not sending config object makes the connection process to be ignored */
process.env.NODE_ENV !== 'test'
? {
exchanges: [
{
name: 'exchange1',
type: 'topic',
},
],
uri: 'amqp://rabbitmq:rabbitmq@localhost:5672',
connectionInitOptions: { wait: false },
}
: undefined
),
],
})
export class RabbitExampleModule {}
```

In this example, when running on a `test` environment, config will be `undefined` thus connection will no happen.

### Mock Connection

You saw above how to prevent the connection during test, but now we'll have also to mock the `RabbitExampleModule` so it exports `AmqpConnection` to our publishers (we do not worry about the subscribers because they will be ignored along with the connection).

```typescript
// In your test setup...

import { Module } from '@nestjs/common';
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { createMock } from '@golevelup/ts-jest';
import { Test } from '@nestjs/testing';
import { AppModule } from 'where your root module is located';
import { RabbitExampleModule } from 'where your rabbitmq module is located';

// Create a valid mock module
@Module({
providers: [
{
provide: AmqpConnection,
useValue: createMock<AmqpConnection>(),
},
],
exports: [AmqpConnection],
})
class MockRabbitExampleModule {}

// Then override the real `RabbitMqModule` with the mocked one
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
})
.overrideModule(RabbitExampleModule)
.useModule(MockRabbitExampleModule)
.compile();

const app = moduleFixture.createNestApplication();

await app.init();
});
```

And now your publishers that inject `AmqpConnection` will find it very well mocked.

## Contribute

Contributions welcome! Read the [contribution guidelines](../../CONTRIBUTING.md) first.
Expand Down
Loading