# @onify/fake-amqplib

> Fake amqplib

Latest version **3.7.0** (published 2026-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @onify/fake-amqplib
pnpm add @onify/fake-amqplib
yarn add @onify/fake-amqplib
bun add @onify/fake-amqplib
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.7.0 |
| Published | 2026-09-08 |
| First published | 2020-02-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 1 |
| Unpacked size | 66.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 5 |
| Author | Onify |
| Maintainers | paed01, rolu01 |
| Keywords | fake, mock, amqp, amqplib, rabbitmq |

## Links

- npm: https://www.npmjs.com/package/@onify/fake-amqplib
- Repository: https://github.com/onify/fake-amqplib
- Homepage: https://github.com/onify/fake-amqplib#readme
- Issues: https://github.com/onify/fake-amqplib/issues
- npm.io page: https://npm.io/package/@onify/fake-amqplib

## Dependencies (1)

- [smqp](https://npm.io/package/smqp.md) ^14.0.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [fakenamely](https://npm.io/package/fakenamely.md) — 0 weekly downloads
- [jest-mock-axios](https://npm.io/package/jest-mock-axios.md) — 0 weekly downloads
- [sinon-chai-es](https://npm.io/package/sinon-chai-es.md) — 0 weekly downloads

## Recent versions

- 3.7.0 (latest) — 2026-09-08
- 3.6.0 — 2026-05-10
- 3.5.0 — 2026-05-07
- 3.4.0 — 2025-11-27
- 3.3.0 — 2025-11-15
- 3.2.0 — 2024-11-13
- 3.1.0 — 2024-05-08
- 3.0.0 — 2023-10-26
- 2.0.0 — 2023-10-18
- 1.0.0 — 2022-11-09
- 0.9.1 — 2022-02-11
- 0.9.0 — 2022-02-04
- 0.8.5 — 2021-11-04
- 0.8.4 — 2021-11-03
- 0.8.3 — 2021-11-02
- … 12 more at https://npm.io/package/@onify/fake-amqplib/versions

## README

# Onify fake-amqplib

[![Built latest](https://github.com/onify/fake-amqplib/actions/workflows/build-latest.yaml/badge.svg)](https://github.com/onify/fake-amqplib/actions/workflows/build-latest.yaml)[![Coverage Status](https://coveralls.io/repos/github/onify/fake-amqplib/badge.svg?branch=default)](https://coveralls.io/github/onify/fake-amqplib?branch=default)

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

<!-- toc -->

- [Fake api](#fake-api)
- [RabbitMQ versions](#rabbitmq-versions)
- [Mocking amqplib](#mocking-amqplib)
  - [ESM](#esm)
    - [Node 20+ — `node:test` `mock.module` (recommended)](#node-20-nodetest-mockmodule-recommended)
    - [Alternative — Quibble](#alternative-quibble)
  - [CommonJS](#commonjs)

<!-- /toc -->

## 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:

```js
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_

```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_

```javascript
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](https://www.npmjs.com/package/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_

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

```json
{
  "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.

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

amqplib.connect = fakeAmqp.connect;
```

or:

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

mock('amqplib/callback_api', fakeAmqp);
```

or just mock the entire amqplib with:

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

mock('amqplib', fakeAmqp);
```

---
_Source: https://npm.io/package/@onify/fake-amqplib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
