Skip to content

Commit

Permalink
test: Add integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Aug 25, 2021
1 parent c27cfd4 commit b31c08c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: docker-compose up -d
- run: npm install
- run: npm test
- name: Coveralls
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.2'
services:
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
globals: {
'ts-jest': {
tsConfig: {
tsconfig: {
target: 'es2019',
},
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"jest": "^27.0.6",
"jest-ts-webcompat-resolver": "^1.0.0",
"lint-staged": "^11.1.2",
"p-event": "^4.2.0",
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1",
"promise-tools": "^2.1.0",
Expand All @@ -66,7 +67,7 @@
},
"scripts": {
"prepare": "husky install && npm run build",
"prepublishOnly": "npm run build && npm test",
"prepublishOnly": "npm run build",
"build": "tsc && tsc -p tsconfig.cjs.json && ./bin/build-types.sh",
"clean": "rm -rf dist types coverage",
"test": "npm run test:lint && npm run test:unittest",
Expand Down
83 changes: 83 additions & 0 deletions test/integrationTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { ConfirmChannel, ConsumeMessage } from 'amqplib';
import chai from 'chai';
import chaiJest from 'chai-jest';
import pEvent from 'p-event';
import { defer, timeout } from 'promise-tools';
import amqp from '../src';

chai.use(chaiJest);

const { expect } = chai;

/**
* Tests in this file assume you have a RabbitMQ instance running on localhost.
* You can start one with:
*
* docker-compose up -d
*
*/
describe('Integration tests', () => {
beforeEach(() => {
// TODO: Uncomment this if you're using `jest.spyOn()` to restore mocks between tests
// jest.restoreAllMocks();
});

it('should connect to the broker', async () => {
// Create a new connection manager
const connection = amqp.connect(['amqp://localhost']);
await timeout(pEvent(connection, 'connect'), 3000);
await connection.close();
});

it('send and receive messages', async () => {
const queueName = 'testQueue1';
const content = `hello world - ${Date.now()}`;

// Create a new connection manager
const connection = amqp.connect(['amqp://localhost']);

// Ask the connection manager for a ChannelWrapper. Specify a setup function to
// run every time we reconnect to the broker.
const sendChannel = connection.createChannel({
setup: async (channel: ConfirmChannel) => {
await channel.assertQueue(queueName, { durable: false });
},
});

const rxPromise = defer<ConsumeMessage>();

const receiveWrapper = connection.createChannel({
setup: async (channel: ConfirmChannel) => {
// `channel` here is a regular amqplib `ConfirmChannel`.
// Note that `this` here is the channelWrapper instance.
await channel.assertQueue(queueName, { durable: false });
await channel.consume(
queueName,
(message) => {
if (!message) {
// Ignore.
} else if (message.content.toString() === content) {
rxPromise.resolve(message);
} else {
console.log(
`Received message ${message?.content.toString()} !== ${content}`
);
}
},
{
noAck: true,
}
);
},
});

await sendChannel.sendToQueue(queueName, content);

const result = await timeout(rxPromise.promise, 3000);
expect(result.content.toString()).to.equal(content);

await sendChannel.close();
await receiveWrapper.close();
await connection.close();
});
});

0 comments on commit b31c08c

Please sign in to comment.