Skip to content

Commit 739289a

Browse files
authored
chore: convert @packages/socket tests to vitest and TypeScript (#32640)
1 parent 3909ed0 commit 739289a

File tree

10 files changed

+308
-267
lines changed

10 files changed

+308
-267
lines changed

.circleci/src/pipeline/@pipeline.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ jobs:
17841784
source ./scripts/ensure-node.sh
17851785
yarn lerna run types
17861786
- sanitize-verify-and-store-mocha-results:
1787-
expectedResultCount: 11
1787+
expectedResultCount: 10
17881788

17891789
verify-release-readiness:
17901790
<<: *defaults

guides/esm-migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ When migrating some of these projects away from the `ts-node` entry [see `@packa
108108
- [x] packages/rewriter ✅ **COMPLETED**
109109
- [x] packages/scaffold-config ✅ **COMPLETED**
110110
- [ ] packages/server
111-
- [ ] packages/socket
111+
- [x] packages/socket**COMPLETED**
112112
- [x] packages/stderr-filtering ✅ **COMPLETED**
113113
- [x] packages/telemetry ✅ **COMPLETED**
114114
- [ ] packages/ts - ultimate goal is removal and likely not worth the effort to convert

packages/config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"clean-deps": "rimraf node_modules",
1616
"lint": "eslint --ext .js,.ts,.json, .",
1717
"test": "yarn test-unit",
18-
"test-debug": "npx vitest --inspect-brk --no-file-parallelism --test-timeout=0",
18+
"test-debug": "vitest --inspect-brk --no-file-parallelism --test-timeout=0",
1919
"test-unit": "vitest run",
2020
"test:clean": "find ./test/__fixtures__ -depth -name 'output.*' -type f -exec rm {} \\;",
2121
"tslint": "tslint --config ../ts/tslint.json --project ."

packages/network/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"clean-deps": "rimraf node_modules",
1111
"lint": "eslint --ext .js,.jsx,.ts,.tsx,.json, .",
1212
"test": "yarn test-unit",
13-
"test-debug": "npx vitest --inspect-brk --no-file-parallelism --test-timeout=0 --hook-timeout=0",
13+
"test-debug": "vitest --inspect-brk --no-file-parallelism --test-timeout=0 --hook-timeout=0",
1414
"test-unit": "vitest run",
1515
"test-watch": "yarn test-unit --watch",
1616
"tslint": "tslint --config ../ts/tslint.json --project ."

packages/scaffold-config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"clean-deps": "rimraf node_modules",
1515
"lint": "eslint --ext .js,.jsx,.ts,.tsx,.json, .",
1616
"test": "vitest run",
17-
"test-debug": "npx vitest --inspect-brk --no-file-parallelism --test-timeout=0",
17+
"test-debug": "vitest --inspect-brk --no-file-parallelism --test-timeout=0",
1818
"tslint": "tslint --config ../ts/tslint.json --project ."
1919
},
2020
"dependencies": {

packages/socket/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
"postinstall": "patch-package",
1212
"lint": "eslint --ext .js,.jsx,.ts,.tsx,.json, .",
1313
"test": "yarn test-unit",
14-
"test-debug": "yarn test-unit --inspect-brk=5566",
15-
"test-unit": "cross-env NODE_ENV=test mocha -r @packages/ts/register --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json",
16-
"test-watch": "cross-env NODE_ENV=test mocha -r @packages/ts/register --watch",
14+
"test-debug": "vitest --inspect-brk --no-file-parallelism --test-timeout=0 --hook-timeout=0",
15+
"test-unit": "vitest",
1716
"tslint": "tslint --config ../ts/tslint.json --project ."
1817
},
1918
"dependencies": {
@@ -30,11 +29,10 @@
3029
"devDependencies": {
3130
"@packages/types": "0.0.0-development",
3231
"@types/uuid": "8.3.2",
33-
"chai": "3.5.0",
3432
"cross-env": "7.0.3",
3533
"devtools-protocol": "0.0.1459876",
36-
"mocha": "3.5.3",
37-
"resolve-pkg": "2.0.0"
34+
"resolve-pkg": "2.0.0",
35+
"vitest": "^3.2.4"
3836
},
3937
"files": [
4038
"index.js",
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
import { describe, it, expect } from 'vitest'
2+
import fs from 'fs'
3+
import path from 'path'
4+
import parser from 'socket.io-parser'
5+
import { hasBinary } from 'socket.io-parser/dist/is-binary'
6+
// @ts-expect-error
7+
import pkg from '../package.json'
8+
import lib from '../index'
9+
import * as browserLib from '../lib/browser'
10+
import resolvePkg from 'resolve-pkg'
11+
12+
const { PacketType } = parser
13+
14+
describe('Socket', function () {
15+
it('exports server', function () {
16+
expect(lib.server).toBeDefined()
17+
})
18+
19+
it('exports client from lib/browser', function () {
20+
expect(browserLib.client).toBeDefined()
21+
})
22+
23+
it('exports createWebSocket from lib/browser', function () {
24+
expect(browserLib.createWebsocket).toBeDefined()
25+
})
26+
27+
it('creates a websocket for non chromium and non webkit browsers', function () {
28+
const socket = browserLib.createWebsocket({ path: '/path', browserFamily: 'firefox' })
29+
30+
// @ts-expect-error
31+
expect(socket.io.opts.path).toEqual('/path')
32+
// @ts-expect-error
33+
expect(socket.io.opts.transports[0]).toEqual('websocket')
34+
})
35+
36+
it('creates a websocket for chromium browsers', function () {
37+
// @ts-expect-error
38+
global.window = {}
39+
const socket = browserLib.createWebsocket({ path: '/path', browserFamily: 'chromium' })
40+
41+
// @ts-expect-error
42+
expect(socket._namespace).toEqual('/path/default')
43+
})
44+
45+
it('creates a websocket for webkit browsers', function () {
46+
const socket = browserLib.createWebsocket({ path: '/path', browserFamily: 'webkit' })
47+
48+
// @ts-expect-error
49+
expect(socket.io.opts.path).toEqual('/path')
50+
// @ts-expect-error
51+
expect(socket.io.opts.transports[0]).toEqual('polling')
52+
})
53+
54+
describe('.getPathToClientSource', function () {
55+
it('returns path to socket.io.js', function () {
56+
const clientPath = path.join(resolvePkg('socket.io-client'), 'dist', 'socket.io.js')
57+
58+
expect(lib.getPathToClientSource()).toEqual(clientPath)
59+
})
60+
61+
it('makes sure socket.io.js actually exists', function () {
62+
return new Promise((resolve, reject) => {
63+
fs.stat(lib.getPathToClientSource(), (err, stats) => {
64+
if (err) {
65+
reject(err)
66+
} else {
67+
resolve(stats)
68+
}
69+
})
70+
})
71+
})
72+
})
73+
74+
describe('.getClientVersion', function () {
75+
it('returns client version', function () {
76+
expect(lib.getClientVersion()).toEqual(pkg.dependencies['socket.io-client'])
77+
})
78+
})
79+
80+
describe('blob encoding + decoding', () => {
81+
it('correctly encodes and decodes binary blob data', () => {
82+
const encoder = new parser.Encoder()
83+
84+
const obj = {
85+
type: PacketType.EVENT,
86+
data: ['a', Buffer.from('abc', 'utf8')],
87+
// data: ['a', { foo: 'bar' }],
88+
id: 23,
89+
nsp: '/cool',
90+
}
91+
92+
const originalData = obj.data
93+
94+
const encodedPackets = encoder.encode(obj)
95+
96+
const decoder = new parser.Decoder()
97+
98+
return new Promise<void>((resolve) => {
99+
decoder.on('decoded', (packet) => {
100+
obj.data = originalData
101+
// @ts-expect-error
102+
obj.attachments = undefined
103+
expect(packet).toEqual(obj)
104+
resolve()
105+
})
106+
107+
for (let i = 0; i < encodedPackets.length; i++) {
108+
decoder.add(encodedPackets[i])
109+
}
110+
})
111+
})
112+
113+
it('correctly encodes and decodes circular data', () => {
114+
const encoder = new parser.Encoder()
115+
116+
const circularObj = {
117+
foo: {},
118+
}
119+
120+
// @ts-expect-error
121+
circularObj.foo.circularObj = circularObj
122+
123+
const obj = {
124+
type: PacketType.EVENT,
125+
data: ['a', circularObj],
126+
id: 23,
127+
nsp: '/cool',
128+
}
129+
130+
const originalData = obj.data
131+
132+
const encodedPackets = encoder.encode(obj)
133+
134+
const decoder = new parser.Decoder()
135+
136+
return new Promise<void>((resolve) => {
137+
decoder.on('decoded', (packet) => {
138+
obj.data = originalData
139+
expect(packet.data[1] === packet.data[1].foo.circularObj).toBe(true)
140+
expect(packet).toEqual(obj)
141+
resolve()
142+
})
143+
144+
for (let i = 0; i < encodedPackets.length; i++) {
145+
decoder.add(encodedPackets[i])
146+
}
147+
})
148+
})
149+
150+
it('correctly encodes and decodes circular data in array', () => {
151+
const encoder = new parser.Encoder()
152+
153+
const circularObj = {
154+
foo: {},
155+
}
156+
157+
// @ts-expect-error
158+
circularObj.foo.circularArray = [circularObj, circularObj]
159+
160+
const obj = {
161+
type: PacketType.EVENT,
162+
data: ['a', circularObj],
163+
id: 23,
164+
nsp: '/cool',
165+
}
166+
167+
const originalData = obj.data
168+
169+
const encodedPackets = encoder.encode(obj)
170+
171+
const decoder = new parser.Decoder()
172+
173+
return new Promise<void>((resolve) => {
174+
decoder.on('decoded', (packet) => {
175+
obj.data = originalData
176+
expect(packet.data[1] === packet.data[1].foo.circularArray[0]).toBe(true)
177+
expect(packet.data[1] === packet.data[1].foo.circularArray[1]).toBe(true)
178+
expect(packet).toEqual(obj)
179+
resolve()
180+
})
181+
182+
for (let i = 0; i < encodedPackets.length; i++) {
183+
decoder.add(encodedPackets[i])
184+
}
185+
})
186+
})
187+
188+
it('correctly encodes and decodes circular data containing binary', () => {
189+
const encoder = new parser.Encoder()
190+
191+
const circularObj = {
192+
foo: {},
193+
bin: Buffer.from('abc', 'utf8'),
194+
}
195+
196+
// @ts-expect-error
197+
circularObj.foo.circularObj = circularObj
198+
199+
const obj = {
200+
type: PacketType.EVENT,
201+
data: ['a', circularObj],
202+
id: 23,
203+
nsp: '/cool',
204+
}
205+
206+
const originalData = obj.data
207+
208+
const encodedPackets = encoder.encode(obj)
209+
210+
const decoder = new parser.Decoder()
211+
212+
return new Promise<void>((resolve) => {
213+
decoder.on('decoded', (packet) => {
214+
obj.data = originalData
215+
// @ts-expect-error
216+
obj.attachments = undefined
217+
expect(packet.data[1] === packet.data[1].foo.circularObj).toBe(true)
218+
expect(packet).toEqual(obj)
219+
resolve()
220+
})
221+
222+
for (let i = 0; i < encodedPackets.length; i++) {
223+
decoder.add(encodedPackets[i])
224+
}
225+
})
226+
})
227+
228+
it('correctly encodes and decodes binary data with objs with no prototype', () => {
229+
const encoder = new parser.Encoder()
230+
231+
const noProtoObj = Object.create(null)
232+
233+
noProtoObj.foo = 'foo'
234+
235+
const obj = {
236+
type: PacketType.EVENT,
237+
data: ['a', noProtoObj, Buffer.from('123', 'utf8')],
238+
id: 23,
239+
nsp: '/cool',
240+
}
241+
242+
const originalData = obj.data
243+
244+
const encodedPackets = encoder.encode(obj)
245+
246+
const decoder = new parser.Decoder()
247+
248+
return new Promise<void>((resolve) => {
249+
decoder.on('decoded', (packet) => {
250+
obj.data = originalData
251+
// @ts-expect-error
252+
obj.attachments = undefined
253+
expect(packet).toEqual(obj)
254+
resolve()
255+
})
256+
257+
for (let i = 0; i < encodedPackets.length; i++) {
258+
decoder.add(encodedPackets[i])
259+
}
260+
})
261+
})
262+
})
263+
264+
describe('hasBinary', () => {
265+
it('hasBinary handles binary data in toJSON()', () => {
266+
const x = {
267+
toJSON () {
268+
return Buffer.from('123', 'utf8')
269+
},
270+
}
271+
272+
const data = ['a', x]
273+
274+
expect(hasBinary(data)).toBe(true)
275+
})
276+
})
277+
})

0 commit comments

Comments
 (0)