@onify/fake-amqplib 1.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,13 +1,45 @@
1
- Onify fake-amqplib
2
- ==================
1
+ # Onify fake-amqplib
3
2
 
4
- [![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)
3
+ [![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)
5
4
 
6
5
  Mocked version of https://www.npmjs.com/package/amqplib.
7
6
 
8
- ### Overriding amqplib
7
+ ## Fake api
9
8
 
10
- You might want to override `amqplib` with `@onify/fake-amqplib` in tests. This can be done this way:
9
+ - `async connect(amqpurl[, ...otherOptions, callback])`: wait for a fake connection or expect one in the callback
10
+ - `connectSync(amqpurl[, ...otherOptions])`: utility method to create a connection without waiting for promise to resolve - synchronous
11
+ - `resetMock()`: reset all connections and brokers
12
+ - `setVersion(minor)`: next connection will be to a amqp of a specific version
13
+ - `connections`: list of faked connections
14
+
15
+ ## RabbitMQ versions
16
+
17
+ RabbitMQ behaviour differs between versions. To specify your version of RabbitMQ you can call `setVersion(minorVersionFloatOrString)`. Default version is 3.5.
18
+
19
+ Example:
20
+ ```js
21
+ var fakeAmqp = require('@onify/fake-amqplib');
22
+
23
+ // prepare your connections
24
+ (async () => {
25
+ fakeAmqp.setVersion('2.2');
26
+ const conn2 = await fakeAmqp.connect('amqp://rabbit2-2');
27
+
28
+ fakeAmqp.setVersion('3.2');
29
+ const conn3 = await fakeAmqp.connect('amqp://rabbit3-2');
30
+
31
+ fakeAmqp.setVersion('3.7');
32
+ const conn37 = await fakeAmqp.connect('amqp://rabbit3-7');
33
+ })()
34
+ ```
35
+
36
+ ## Mocking amqplib
37
+
38
+ You might want to override `amqplib` with `@onify/fake-amqplib` in tests. This can be done in a number of ways.
39
+
40
+ ### CommonJS
41
+
42
+ Example on how to mock amqplib when working with commonjs.
11
43
 
12
44
  ```javascript
13
45
  const amqplib = require('amqplib');
@@ -16,8 +48,7 @@ const fakeAmqp = require('@onify/fake-amqplib');
16
48
  amqplib.connect = fakeAmqp.connect;
17
49
  ```
18
50
 
19
- If you are using version 2 or higher of [exp-amqp-connection](https://www.npmjs.com/package/exp-amqp-connection)
20
- you can use [mock-require](https://www.npmjs.com/package/mock-require) to replace `amqplib` with `@onify/fake-amqplib` in your tests like this:
51
+ or:
21
52
 
22
53
  ```javascript
23
54
  const mock = require('mock-require');
@@ -26,7 +57,7 @@ const fakeAmqp = require('@onify/fake-amqplib');
26
57
  mock('amqplib/callback_api', fakeAmqp);
27
58
  ```
28
59
 
29
- or just mock the entire amqplib with
60
+ or just mock the entire amqplib with:
30
61
 
31
62
  ```javascript
32
63
  const mock = require('mock-require');
@@ -35,33 +66,62 @@ const fakeAmqp = require('@onify/fake-amqplib');
35
66
  mock('amqplib', fakeAmqp);
36
67
  ```
37
68
 
38
- ### RabbitMQ versions
69
+ ### ESM
39
70
 
40
- Some behaviour differs between versions. To specify your version of RabbitMQ you can call `setVersion(minorVersionFloatOrString)`. Default version is 3.5.
71
+ Example on how to mock amqplib import when working with modules.
41
72
 
42
- Example:
43
- ```js
44
- var fakeAmqp = require('@onify/fake-amqplib');
73
+ **[Quibble](https://www.npmjs.com/package/quibble) mocha example**
45
74
 
46
- // prepare your connections
47
- (async () => {
48
- fakeAmqp.setVersion('2.2');
49
- const conn2 = await fakeAmqp.connect('amqp://rabbit2-2');
75
+ Both amqplib and fake-amqplib have to be quibbled if reset mock is used during testing.
50
76
 
51
- fakeAmqp.setVersion('3.2');
52
- const conn3 = await fakeAmqp.connect('amqp://rabbit3-1');
77
+ _test/setup.js_
78
+ ```js
79
+ import * as fakeAmqpLib from '@onify/fake-amqplib';
80
+ import { connect as fakeConnect } from '@onify/fake-amqplib';
81
+ import quibble from 'quibble';
53
82
 
54
- fakeAmqp.setVersion('3.7');
55
- const conn37 = await fakeAmqp.connect('amqp://rabbit3-7');
56
- })()
83
+ (async () => {
84
+ await quibble.esm('amqplib', { connect: fakeConnect });
85
+ await quibble.esm('amqplib/callback_api', { connect: fakeConnect });
86
+ await quibble.esm('@onify/fake-amqplib', { ...fakeAmqpLib });
87
+ })();
57
88
  ```
58
89
 
59
- ### Fake api
60
-
61
- List of available methods and a property
90
+ _.mocharc.json_ (true for node version < 20)
91
+ ```json
92
+ {
93
+ "recursive": true,
94
+ "require": ["test/setup.js"],
95
+ "node-option": [
96
+ "experimental-specifier-resolution=node",
97
+ "no-warnings",
98
+ "loader=quibble"
99
+ ]
100
+ }
101
+ ```
62
102
 
63
- - `connections`: property with all faked connections
64
- - `connectSync(amqpurl[, ...otherOptions])`: helper method to create a connection without waiting for promise to resolve - synchronous
65
- - `resetMock()`: reset all connections and brokers
66
- - `setVersion(minor)`: next connection will be to a amqp of a specific version
67
- - `async connect(amqpurl[, ...otherOptions, callback])`: make a fake connection
103
+ _test/amqplib-connection-test.js_
104
+ ```js
105
+ import assert from 'node:assert';
106
+ import { connect } from 'amqplib';
107
+ import { connect as connectCb } from 'amqplib/callback_api';
108
+
109
+ import { resetMock } from '@onify/fake-amqplib';
110
+
111
+ describe('connection', () => {
112
+ afterEach(resetMock);
113
+
114
+ it('connect promise', async () => {
115
+ const connection = await connect('amqp://host');
116
+ assert.equal(connection.connection.serverProperties.version, '3.5.0');
117
+ });
118
+
119
+ it('connect callback', (done) => {
120
+ connectCb('amqp://host', (err, connection) => {
121
+ if (err) return done(err);
122
+ assert.equal(connection.connection.serverProperties.version, '3.5.0');
123
+ done();
124
+ });
125
+ });
126
+ });
127
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ /// <reference types="amqplib" />
2
+ /// <reference types="node" />
3
+
4
+ import { Options, Connection, Channel } from "amqplib";
5
+ import { EventEmitter } from "events";
6
+ import { Broker } from 'smqp';
7
+
8
+ export interface FakeAmqplibChannel extends Channel {
9
+ /** Channel name and identifier, for faking purposes */
10
+ _channelName: string;
11
+ _broker: Broker;
12
+ _version: number;
13
+ new(broker: Broker, connection: FakeAmqplibConnection): FakeAmqplibChannel;
14
+ get _closed(): boolean;
15
+ }
16
+
17
+ export interface FakeAmqplibConnection extends Connection {
18
+ _channels: FakeAmqplibChannel[];
19
+ _url: URL;
20
+ /** Connection identifier, for faking purposes */
21
+ _id: string;
22
+ _broker: Broker;
23
+ _version: number;
24
+ new(broker: Broker, version: number, amqpUrl: string, options?: any): FakeAmqplibConnection;
25
+ get _closed(): boolean;
26
+ }
27
+
28
+ interface SocketOptions {
29
+ host?: string;
30
+ keepAlive?: boolean;
31
+ keepAliveDelay?: number;
32
+ noDelay?: boolean;
33
+ port?: number;
34
+ serverName?: string;
35
+ timeout?: number;
36
+ [x: string]: any;
37
+ }
38
+
39
+ type connectCallback = (
40
+ err: Error,
41
+ connection: FakeAmqplibConnection
42
+ ) => void;
43
+
44
+ export class FakeAmqplib {
45
+ connections: FakeAmqplibConnection[];
46
+ constructor(version?: number)
47
+ connect(url: string | Options.Connect, socketOptions?: SocketOptions): Promise<FakeAmqplibConnection>;
48
+ connect(url: string | Options.Connect, socketOptions: SocketOptions, callback: connectCallback): void;
49
+ connect(url: string | Options.Connect, callback: (err: Error, connection: FakeAmqplibConnection) => void): void;
50
+ connectSync(url: string | Options.Connect, socketOptions?: SocketOptions): FakeAmqplibConnection;
51
+ resetMock(): void;
52
+ setVersion(minorVersion: number): void;
53
+ }
54
+
55
+ export function connect(url: string | Options.Connect, socketOptions?: SocketOptions): Promise<FakeAmqplibConnection>;
56
+ export function connect(url: string | Options.Connect, socketOptions: SocketOptions, callback: connectCallback): void;
57
+ export function connect(url: string | Options.Connect, callback: connectCallback): void;
58
+ export function connectSync(url: string | Options.Connect, socketOptions?: SocketOptions): FakeAmqplibConnection;
59
+ export function resetMock(): void;
60
+ export function setVersion(minorVersion: number): void;