Skip to content

Commit 08c1aa6

Browse files
authored
Use vitest and test WebSocket browser client (#71)
* Use docker compose for test environment * Convert tests to vitest * Add init object option to AMQPWebSocketClient constructor * Add browser based testing using vitest * Add test for browser in ci.yml * Bump minimum required version of TypeScript and ts-node - For code before super() - TypeScript #29374 * Extract AMQP client creation function Makes most of the tests common between Node.js and WebSockets and easier to compare.
1 parent 117c90f commit 08c1aa6

14 files changed

Lines changed: 1050 additions & 259 deletions

File tree

.etc_rabbitmq/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pem

.etc_rabbitmq/rabbitmq.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
loopback_users = none
2+
3+
log.default.level = info
4+
log.file = false
5+
log.console = true
6+
7+
#listeners.ssl.default = 5671
8+
#ssl_options.cacertfile = /etc/rabbitmq/rootCA.pem
9+
#ssl_options.certfile = /etc/rabbitmq/localhost.pem
10+
#ssl_options.keyfile = /etc/rabbitmq/localhost-key.pem

.github/workflows/ci.yml

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,61 @@ jobs:
1616
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1717

1818
steps:
19-
- name: Install RabbitMQ
20-
run: sudo apt-get update && sudo apt-get install -y rabbitmq-server
21-
- name: Stop RabbitMQ
22-
run: sudo systemctl stop rabbitmq-server
23-
2419
- name: Set up Homebrew
2520
uses: Homebrew/actions/setup-homebrew@master
2621
- name: Install github.com/FiloSottile/mkcert
2722
run: brew install mkcert
28-
- name: Create local CA
29-
run: sudo CAROOT=/etc/rabbitmq $(brew --prefix)/bin/mkcert -install
30-
- name: Create certificate
23+
- uses: actions/checkout@v3
24+
- name: Create local CA and certificate
3125
run: |
32-
sudo $(brew --prefix)/bin/mkcert -key-file /etc/rabbitmq/localhost-key.pem -cert-file /etc/rabbitmq/localhost.pem localhost
33-
sudo chmod +r /etc/rabbitmq/localhost-key.pem
26+
mkdir -p .etc_rabbitmq
27+
CAROOT="$(pwd)/.etc_rabbitmq" $(brew --prefix)/bin/mkcert -install
28+
$(brew --prefix)/bin/mkcert -key-file ./.etc_rabbitmq/localhost-key.pem -cert-file ./.etc_rabbitmq/localhost.pem localhost
29+
chmod +r ./.etc_rabbitmq/localhost-key.pem
3430
- name: Create RabbitMQ config
3531
run: |
36-
sudo tee /etc/rabbitmq/rabbitmq.conf <<'EOF'
32+
tee ./.etc_rabbitmq/rabbitmq.conf <<'EOF'
33+
loopback_users = none
3734
listeners.ssl.default = 5671
3835
ssl_options.cacertfile = /etc/rabbitmq/rootCA.pem
3936
ssl_options.certfile = /etc/rabbitmq/localhost.pem
4037
ssl_options.keyfile = /etc/rabbitmq/localhost-key.pem
4138
EOF
42-
- name: Start RabbitMQ
43-
run: sudo systemctl start rabbitmq-server
44-
- name: Verify RabbitMQ started correctly
45-
run: while true; do sudo rabbitmq-diagnostics status 2>/dev/null && break; echo -n .; sleep 2; done
46-
47-
- uses: actions/checkout@v3
39+
- name: Start Docker Compose
40+
run: docker compose up -d
4841
- name: Use Node.js ${{ matrix.node-version }}
4942
uses: actions/setup-node@v3
5043
with:
5144
node-version: ${{ matrix.node-version }}
5245
- run: npm install
46+
env:
47+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
5348
- run: npm test
5449
env:
5550
AMQPS_URL: amqps://localhost?insecure
5651
- run: npm run lint
52+
53+
test-browser:
54+
runs-on: ubuntu-latest
55+
env:
56+
NODE_VERSION: 16.x
57+
58+
steps:
59+
- uses: actions/checkout@v3
60+
- name: Create RabbitMQ config
61+
run: |
62+
mkdir -p .etc_rabbitmq
63+
tee ./.etc_rabbitmq/rabbitmq.conf <<'EOF'
64+
loopback_users = none
65+
EOF
66+
- name: Start Docker Compose
67+
run: docker compose up -d
68+
- name: Use Node.js ${{ env.NODE_VERSION }}
69+
uses: actions/setup-node@v3
70+
with:
71+
node-version: ${{ env.NODE_VERSION }}
72+
- run: npm install
73+
env:
74+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
75+
- run: npx playwright install --with-deps chromium
76+
- run: npm run test-browser -- run --browser.headless

.github/workflows/docs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
- uses: actions/checkout@v3
1717
- uses: actions/setup-node@v3
1818
- run: npm install
19+
env:
20+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
1921
- run: npm run docs
2022
- name: Deploy docs
2123
uses: JamesIves/github-pages-deploy-action@4.1.7

.github/workflows/release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
with:
1717
registry-url: https://registry.npmjs.org/
1818
- run: npm install
19+
env:
20+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
1921
- run: npm run build
2022
- run: npm publish --access public
2123
env:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
### Changed
1818

1919
- Breaking change: Removed support for end-of-life versions of Node.js. A minimum of Node.js 16 is now required.
20+
- New overload for `AMQPWebSocketClient` constructor to allow setting optional parameters through an init object.
2021

2122
## [2.1.1] - 2022-12-13
2223

docker-compose.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
services:
2+
rabbitmq:
3+
image: rabbitmq:3
4+
volumes:
5+
- rabbitmq_data:/var/lib/rabbitmq
6+
- rabbitmq_logs:/var/log/rabbitmq
7+
- ./.etc_rabbitmq:/etc/rabbitmq
8+
ports:
9+
- "5671:5671"
10+
- "5672:5672"
11+
#- "15672:15672"
12+
environment:
13+
RABBITMQ_DEFAULT_USER: guest
14+
RABBITMQ_DEFAULT_PASS: guest
15+
healthcheck:
16+
test: ["CMD", "rabbitmq-diagnostics", "status"]
17+
interval: 10s
18+
timeout: 5s
19+
retries: 3
20+
start_period: 20s
21+
22+
websocket-relay:
23+
image: cloudamqp/websocket-tcp-relay:v1.0.6
24+
ports:
25+
- "15670:15670"
26+
links:
27+
- rabbitmq
28+
depends_on:
29+
rabbitmq:
30+
condition: service_healthy
31+
command: ["--port", "15670", "--upstream", "tcp://rabbitmq:5672"]
32+
33+
volumes:
34+
rabbitmq_data:
35+
rabbitmq_logs:

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"scripts": {
2323
"docs": "typedoc src/index.ts",
2424
"lint": "eslint src",
25-
"test": "c8 ava",
25+
"test": "vitest run --coverage",
26+
"test-browser": "vitest --config ./vitest.config.browser.ts",
2627
"prebuild": "rm -rf dist lib types",
2728
"build": "tsc && tsc --module commonjs --outDir lib/cjs && tsc --emitDeclarationOnly --removeComments false && rollup -c",
2829
"postbuild": "echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json",
@@ -49,17 +50,19 @@
4950
},
5051
"homepage": "https://github.com/cloudamqp/amqp-client.js#readme",
5152
"devDependencies": {
52-
"@types/node": "^17.0.1",
53+
"@types/node": "^16.18.37",
5354
"@typescript-eslint/eslint-plugin": "^5.8.0",
5455
"@typescript-eslint/parser": "^5.8.0",
55-
"ava": "^3.15.0",
56-
"c8": "^7.10.0",
56+
"@vitest/browser": "^0.33.0",
57+
"@vitest/coverage-v8": "^0.33.0",
5758
"eslint": "^8.5.0",
59+
"playwright": "^1.36.1",
5860
"rollup": "^2.39.1",
5961
"rollup-plugin-sourcemaps": "^0.6.3",
60-
"ts-node": "^10.4.0",
62+
"ts-node": "^10.8.0",
6163
"typedoc": "^0.22.10",
62-
"typescript": "^4.5.4"
64+
"typescript": "^4.6.4",
65+
"vitest": "^0.33.0"
6366
},
6467
"ava": {
6568
"extensions": {

src/amqp-websocket-client.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import { AMQPBaseClient } from './amqp-base-client.js'
22
import { AMQPView } from './amqp-view.js'
33
import { AMQPError } from './amqp-error.js'
44

5+
interface AMQPWebSocketInit {
6+
url: string
7+
vhost?: string
8+
username?: string
9+
password?: string
10+
name?: string
11+
frameMax?: number
12+
heartbeat?: number
13+
}
14+
515
/**
616
* WebSocket client for AMQP 0-9-1 servers
717
*/
@@ -15,7 +25,18 @@ export class AMQPWebSocketClient extends AMQPBaseClient {
1525
/**
1626
* @param url to the websocket endpoint, example: wss://server/ws/amqp
1727
*/
18-
constructor(url: string, vhost = "/", username = "guest", password = "guest", name?: string, frameMax = 4096, heartbeat = 0) {
28+
constructor(url: string, vhost?: string, username?: string, password?: string, name?: string, frameMax?: number, heartbeat?: number);
29+
constructor(init: AMQPWebSocketInit);
30+
constructor(url: string | AMQPWebSocketInit, vhost = "/", username = "guest", password = "guest", name?: string, frameMax = 4096, heartbeat = 0) {
31+
if (typeof url === 'object') {
32+
vhost = url.vhost ?? vhost
33+
username = url.username ?? username
34+
password = url.password ?? password
35+
name = url.name ?? name
36+
frameMax = url.frameMax ?? frameMax
37+
heartbeat = url.heartbeat ?? heartbeat
38+
url = url.url
39+
}
1940
super(vhost, username, password, name, AMQPWebSocketClient.platform(), frameMax, heartbeat)
2041
this.url = url
2142
this.frameBuffer = new Uint8Array(frameMax)

0 commit comments

Comments
 (0)