Skip to content

Commit 676b4ce

Browse files
committed
test: convert rest of the socket compat tests to TypeScript
1 parent ad9cd4c commit 676b4ce

File tree

5 files changed

+52
-47
lines changed

5 files changed

+52
-47
lines changed

.mocharc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
const config = {
55
require: ["ts-node/register"],
6-
spec: ["test/unit/**/*-test.ts", "test/unit/compat/*-test.js"],
6+
spec: ["test/unit/**/*-test.ts"],
77
"expose-gc": true,
88
"v8-expose-gc": true,
99
exit: true,

test/unit/compat/socket-test.js renamed to test/unit/compat/socket-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as zmq from "../../../v5-compat"
2-
import {assert} from "chai"
2+
import { assert } from "chai"
33

44
if (process.env.INCLUDE_COMPAT_TESTS) {
55
describe("compat socket", function () {

test/unit/compat/socket-unbind-test.js renamed to test/unit/compat/socket-unbind-test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/* This test is very unreliable in practice, especially in CI.
22
It is disabled by default. */
3+
import * as zmq from "../../../v5-compat"
4+
import semver from "semver"
5+
import {assert} from "chai"
6+
import {testProtos, uniqAddress} from "../helpers"
7+
38
if (
49
process.env.INCLUDE_COMPAT_TESTS &&
510
process.env.INCLUDE_COMPAT_UNBIND_TEST
611
) {
7-
const zmq = require("./load")
8-
const semver = require("semver")
9-
const {assert} = require("chai")
10-
const {testProtos, uniqAddress} = require("../helpers")
11-
1212
for (const proto of testProtos("tcp")) {
1313
describe(`compat socket with ${proto} unbind`, function () {
1414
beforeEach(function () {
@@ -18,14 +18,18 @@ if (
1818
}
1919
})
2020

21-
let sockA
22-
let sockB
23-
let sockC
21+
let sockA: zmq.Socket
22+
let sockB: zmq.Socket
23+
let sockC: zmq.Socket
24+
let address1: string
25+
let address2: string
2426

25-
beforeEach(function () {
27+
beforeEach(async function () {
2628
sockA = zmq.socket("dealer", {linger: 0})
2729
sockB = zmq.socket("dealer", {linger: 0})
2830
sockC = zmq.socket("dealer", {linger: 0})
31+
address1 = await uniqAddress(proto)
32+
address2 = await uniqAddress(proto)
2933
})
3034

3135
afterEach(function () {
@@ -35,22 +39,19 @@ if (
3539
})
3640

3741
it("should be able to unbind", function (done) {
38-
const address1 = uniqAddress(proto)
39-
const address2 = uniqAddress(proto)
40-
4142
let msgCount = 0
4243
sockA.bindSync(address1)
4344
sockA.bindSync(address2)
4445

45-
sockA.on("unbind", async function (addr) {
46+
sockA.on("unbind", async function (addr: string) {
4647
if (addr === address1) {
4748
sockB.send("Error from sockB.")
4849
sockC.send("Messsage from sockC.")
4950
sockC.send("Final message from sockC.")
5051
}
5152
})
5253

53-
sockA.on("message", async function (msg) {
54+
sockA.on("message", async function (msg: {toString: () => string}) {
5455
msgCount++
5556
if (msg.toString() === "Hello from sockB.") {
5657
sockA.unbindSync(address1)

test/unit/compat/socket-xpub-xsub-test.js renamed to test/unit/compat/socket-xpub-xsub-test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
if (process.env.INCLUDE_COMPAT_TESTS) {
2-
const zmq = require("./load")
3-
const {assert} = require("chai")
4-
const {testProtos, uniqAddress} = require("../helpers")
1+
import * as zmq from "../../../v5-compat"
2+
import {assert} from "chai"
3+
import {testProtos, uniqAddress} from "../helpers"
54

5+
if (process.env.INCLUDE_COMPAT_TESTS) {
66
for (const proto of testProtos("tcp", "inproc")) {
7+
let address1: string
8+
let address2: string
9+
10+
beforeEach(async function () {
11+
address1 = await uniqAddress(proto)
12+
address2 = await uniqAddress(proto)
13+
})
14+
715
describe(`compat socket with ${proto} xpub-xsub`, function () {
816
it("should support pub-sub tracing and filtering", function (done) {
917
let n = 0
@@ -13,9 +21,6 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
1321
const xpub = zmq.socket("xpub")
1422
const xsub = zmq.socket("xsub")
1523

16-
const address1 = uniqAddress(proto)
17-
const address2 = uniqAddress(proto)
18-
1924
pub.bind(address1, err => {
2025
if (err) {
2126
throw err

test/unit/compat/socket-zap-test.js renamed to test/unit/compat/socket-zap-test.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
if (process.env.INCLUDE_COMPAT_TESTS) {
2-
const zmq = require("./load")
3-
const semver = require("semver")
4-
const {assert} = require("chai")
5-
const {testProtos, uniqAddress} = require("../helpers")
1+
import * as zmq from "../../../v5-compat"
2+
import semver from "semver"
3+
import {assert} from "chai"
4+
import {testProtos, uniqAddress} from "../helpers"
65

6+
if (process.env.INCLUDE_COMPAT_TESTS) {
77
function start() {
88
const zap = zmq.socket("router")
99

@@ -49,7 +49,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
4949
)
5050
})
5151

52-
return new Promise((resolve, reject) => {
52+
return new Promise<zmq.Socket>((resolve, reject) => {
5353
zap.bind("inproc://zeromq.zap.01", err => {
5454
if (err) {
5555
return reject(err)
@@ -61,9 +61,10 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
6161

6262
for (const proto of testProtos("tcp", "inproc")) {
6363
describe(`compat socket with ${proto} zap`, function () {
64-
let zapSocket
65-
let rep
66-
let req
64+
let zapSocket: zmq.Socket
65+
let rep: zmq.Socket
66+
let req: zmq.Socket
67+
let address: string
6768

6869
before(async function () {
6970
zapSocket = await start()
@@ -76,14 +77,15 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
7677
})
7778
})
7879

79-
beforeEach(function () {
80+
beforeEach(async function () {
8081
/* Since ZAP uses inproc transport, it does not work reliably. */
8182
if (semver.satisfies(zmq.version, "< 4.2")) {
8283
this.skip()
8384
}
8485

8586
rep = zmq.socket("rep")
8687
req = zmq.socket("req")
88+
address = await uniqAddress(proto)
8789
})
8890

8991
afterEach(function () {
@@ -92,11 +94,12 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
9294
})
9395

9496
it("should support curve", function (done) {
97+
// TODO fix the capability type
98+
// @ts-expect-error
9599
if (!zmq.capability.curve) {
96100
this.skip()
97101
}
98102

99-
const address = uniqAddress(proto)
100103
const serverPublicKey = Buffer.from(
101104
"7f188e5244b02bf497b86de417515cf4d4053ce4eb977aee91a55354655ec33a",
102105
"hex",
@@ -114,7 +117,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
114117
"hex",
115118
)
116119

117-
rep.on("message", function (msg) {
120+
rep.on("message", function (msg: unknown) {
118121
assert.instanceOf(msg, Buffer)
119122
assert.equal(msg.toString(), "hello")
120123
rep.send("world")
@@ -125,7 +128,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
125128
rep.curve_secretkey = serverPrivateKey
126129
assert.equal(rep.mechanism, 2)
127130

128-
rep.bind(address, err => {
131+
rep.bind(address, (err: any) => {
129132
if (err) {
130133
throw err
131134
}
@@ -136,7 +139,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
136139

137140
req.connect(address)
138141
req.send("hello")
139-
req.on("message", function (msg) {
142+
req.on("message", function (msg: unknown) {
140143
assert.instanceOf(msg, Buffer)
141144
assert.equal(msg.toString(), "world")
142145
done()
@@ -145,9 +148,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
145148
})
146149

147150
it("should support null", function (done) {
148-
const address = uniqAddress(proto)
149-
150-
rep.on("message", function (msg) {
151+
rep.on("message", function (msg: unknown) {
151152
assert.instanceOf(msg, Buffer)
152153
assert.equal(msg.toString(), "hello")
153154
rep.send("world")
@@ -156,14 +157,14 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
156157
rep.zap_domain = "test"
157158
assert.equal(rep.mechanism, 0)
158159

159-
rep.bind(address, err => {
160+
rep.bind(address, (err: any) => {
160161
if (err) {
161162
throw err
162163
}
163164
assert.equal(req.mechanism, 0)
164165
req.connect(address)
165166
req.send("hello")
166-
req.on("message", function (msg) {
167+
req.on("message", function (msg: unknown) {
167168
assert.instanceOf(msg, Buffer)
168169
assert.equal(msg.toString(), "world")
169170
done()
@@ -172,9 +173,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
172173
})
173174

174175
it("should support plain", function (done) {
175-
const address = uniqAddress(proto)
176-
177-
rep.on("message", function (msg) {
176+
rep.on("message", function (msg: unknown) {
178177
assert.instanceOf(msg, Buffer)
179178
assert.equal(msg.toString(), "hello")
180179
rep.send("world")
@@ -184,7 +183,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
184183
rep.plain_server = 1
185184
assert.equal(rep.mechanism, 1)
186185

187-
rep.bind(address, err => {
186+
rep.bind(address, (err: any) => {
188187
if (err) {
189188
throw err
190189
}
@@ -194,7 +193,7 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
194193

195194
req.connect(address)
196195
req.send("hello")
197-
req.on("message", function (msg) {
196+
req.on("message", function (msg: unknown) {
198197
assert.instanceOf(msg, Buffer)
199198
assert.equal(msg.toString(), "world")
200199
done()

0 commit comments

Comments
 (0)