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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@onify/fake-amqplib

v3.6.0

Published

Fake amqplib

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.

ESM

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

Node 20+ — node:test mock.module (recommended)

Node's built-in test runner ships an experimental module-mocking API that needs no extra dependency. Set it up at the top of the test file (or in a setup file), then dynamically import amqplib.

.mocharc.json

{
  "recursive": true,
  "require": ["chai/register-expect.js"],
  "node-option": ["experimental-test-module-mocks", "no-warnings"]
}

The experimental-test-module-mocks flag enables mock.module; no-warnings silences the "experimental feature" notice. Drop it if you'd rather see the warning.

test/amqplib-connection-test.js

import { mock } from 'node:test';
import { connect as fakeConnect, resetMock } from '@onify/fake-amqplib';

describe('connection', () => {
  let connect;
  let ctx;

  before(async () => {
    ctx = mock.module('amqplib', { namedExports: { connect: fakeConnect } });
    ({ connect } = await import('amqplib'));
  });

  after(() => {
    ctx.restore();
    resetMock();
  });

  it('connects to the fake', async () => {
    const connection = await connect('amqp://host');
    expect(connection.connection.serverProperties).to.have.property('product', 'RabbitMQ');
  });
});

If you also use mocha --parallel or run tests via node --test, the same setup works — mock.module is process-global, so register it before the first dynamic import in each test file.

Alternative — Quibble

Quibble is useful if you're on a Node version older than 20, or prefer not to rely on an experimental flag.

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 (the loader=quibble option is only needed on Node < 20)

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

Then import amqplib normally in your tests; quibble rewires the import.

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);