@onify/fake-amqplib 0.9.1 → 2.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 +89 -29
- package/index.d.ts +60 -0
- package/index.js +451 -360
- package/main.cjs +591 -0
- package/package.json +30 -13
- package/CHANGELOG.md +0 -61
package/README.md
CHANGED
|
@@ -1,14 +1,46 @@
|
|
|
1
|
-
Onify fake-amqplib
|
|
2
|
-
==================
|
|
1
|
+
# Onify fake-amqplib
|
|
3
2
|
|
|
4
|
-
[](https://github.com/onify/fake-amqplib/actions/workflows/build-latest.yaml)
|
|
3
|
+
[](https://github.com/onify/fake-amqplib/actions/workflows/build-latest.yaml)[](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
|
-
|
|
7
|
+
## Fake api
|
|
8
|
+
|
|
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
|
+
Some 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-1');
|
|
30
|
+
|
|
31
|
+
fakeAmqp.setVersion('3.7');
|
|
32
|
+
const conn37 = await fakeAmqp.connect('amqp://rabbit3-7');
|
|
33
|
+
})()
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Mocking amqplib
|
|
9
37
|
|
|
10
38
|
You might want to override `amqplib` with `@onify/fake-amqplib` in tests. This can be done this way:
|
|
11
39
|
|
|
40
|
+
### CommonJS
|
|
41
|
+
|
|
42
|
+
Example on how to mock amqplib when working with commonjs.
|
|
43
|
+
|
|
12
44
|
```javascript
|
|
13
45
|
const amqplib = require('amqplib');
|
|
14
46
|
const fakeAmqp = require('@onify/fake-amqplib');
|
|
@@ -16,8 +48,7 @@ const fakeAmqp = require('@onify/fake-amqplib');
|
|
|
16
48
|
amqplib.connect = fakeAmqp.connect;
|
|
17
49
|
```
|
|
18
50
|
|
|
19
|
-
|
|
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
|
-
###
|
|
69
|
+
### ESM
|
|
39
70
|
|
|
40
|
-
|
|
71
|
+
Example on how to mock amqplib import when working with modules.
|
|
41
72
|
|
|
42
|
-
|
|
43
|
-
```js
|
|
44
|
-
var fakeAmqp = require('@onify/fake-amqplib');
|
|
73
|
+
**[Quibble](https://www.npmjs.com/package/quibble) mocha example**
|
|
45
74
|
|
|
46
|
-
|
|
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 mocked if reset mock is used during testing.
|
|
50
76
|
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
55
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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, credentials } from "amqplib";
|
|
5
|
+
import { EventEmitter } from "events";
|
|
6
|
+
import { Broker } from 'smqp';
|
|
7
|
+
|
|
8
|
+
export interface FakeAmqplibChannel extends Channel {
|
|
9
|
+
_channelName: string;
|
|
10
|
+
_version: number;
|
|
11
|
+
_broker: Broker;
|
|
12
|
+
new(broker: Broker, connection: FakeAmqplibConnection): FakeAmqplibChannel;
|
|
13
|
+
get _closed(): boolean;
|
|
14
|
+
get _emitter(): EventEmitter;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface FakeAmqplibConnection extends Connection {
|
|
18
|
+
_channels: FakeAmqplibChannel[];
|
|
19
|
+
_url: URL;
|
|
20
|
+
_id: string;
|
|
21
|
+
_broker: Broker;
|
|
22
|
+
_version: number;
|
|
23
|
+
new(broker: Broker, version: number, amqpUrl: string, options?: any): FakeAmqplibConnection;
|
|
24
|
+
get _closed(): boolean;
|
|
25
|
+
get _emitter(): EventEmitter;
|
|
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;
|