npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@onify/fake-amqplib

v3.2.0

Published

Fake amqplib

Downloads

701

Readme

Onify fake-amqplib

Built latestCoverage Status

Mocked version of https://www.npmjs.com/package/amqplib.

Fake api

  • async connect(amqpurl[, ...otherOptions, callback]): wait for a fake connection or expect one in the callback
  • connectSync(amqpurl[, ...otherOptions]): utility method to create a connection without waiting for promise to resolve - synchronous
  • resetMock(): reset all connections and brokers
  • setVersion(minor): next connection will be to a amqp of a specific version
  • connections: list of faked connections

RabbitMQ versions

RabbitMQ behaviour differs between versions. To specify your version of RabbitMQ you can call setVersion(minorVersionFloatOrString). Default version is 3.5.

Example:

var fakeAmqp = require('@onify/fake-amqplib');

// prepare your connections
(async () => {
  fakeAmqp.setVersion('2.2');
  const conn2 = await fakeAmqp.connect('amqp://rabbit2-2');

  fakeAmqp.setVersion('3.2');
  const conn3 = await fakeAmqp.connect('amqp://rabbit3-2');

  fakeAmqp.setVersion('3.7');
  const conn37 = await fakeAmqp.connect('amqp://rabbit3-7');
})();

Mocking amqplib

You might want to override amqplib with @onify/fake-amqplib in tests. This can be done in a number of ways.

CommonJS

Example on how to mock amqplib when working with commonjs.

const amqplib = require('amqplib');
const fakeAmqp = require('@onify/fake-amqplib');

amqplib.connect = fakeAmqp.connect;

or:

const mock = require('mock-require');
const fakeAmqp = require('@onify/fake-amqplib');

mock('amqplib/callback_api', fakeAmqp);

or just mock the entire amqplib with:

const mock = require('mock-require');
const fakeAmqp = require('@onify/fake-amqplib');

mock('amqplib', fakeAmqp);

ESM

Example on how to mock amqplib import when working with modules.

Quibble mocha example

Both amqplib and fake-amqplib have to be quibbled if reset mock is used during testing.

test/setup.js

import * as fakeAmqpLib from '@onify/fake-amqplib';
import { connect as fakeConnect } from '@onify/fake-amqplib';
import quibble from 'quibble';

(async () => {
  await quibble.esm('amqplib', { connect: fakeConnect });
  await quibble.esm('@onify/fake-amqplib', { ...fakeAmqpLib });
})();

.mocharc.json (true for node version < 20)

{
  "recursive": true,
  "require": ["test/setup.js"],
  "node-option": ["experimental-specifier-resolution=node", "no-warnings", "loader=quibble"]
}

test/amqplib-connection-test.js

import assert from 'node:assert';
import { connect } from 'amqplib';
import { connect as connectCb } from 'amqplib';

import { resetMock } from '@onify/fake-amqplib';

describe('connection', () => {
  afterEach(resetMock);

  it('connect promise', async () => {
    const connection = await connect('amqp://host');
    assert.equal(connection.connection.serverProperties.version, '3.5.0');
  });

  it('connect callback', (done) => {
    connectCb('amqp://host', (err, connection) => {
      if (err) return done(err);
      assert.equal(connection.connection.serverProperties.version, '3.5.0');
      done();
    });
  });
});