Skip to content

Commit a4b7dcd

Browse files
authored
chore: convert @packages/network tests from mocha to vitest (#32630)
* start vitest convertion and convert connect spec to vitest * chore: convert agent spec to vitest * chore: convert block spec to vitest * chore: convert client-certificates test to vitest and remove circular dependency on singletoon in agent and client certificats * chore: convert the connect spec to vitest * chore: convert cors spec to vitest * chore: convert document domain spec to vitest * chore: convert uri spec to vitest * chore: cleanup files
1 parent 3f1a150 commit a4b7dcd

19 files changed

+866
-944
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: 12
1787+
expectedResultCount: 11
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
@@ -102,7 +102,7 @@ When migrating some of these projects away from the `ts-node` entry [see `@packa
102102
- [ ] packages/icons
103103
- [ ] packages/launcher
104104
- [ ] packages/net-stubbing
105-
- [ ] packages/network
105+
- [x] packages/network**COMPLETED**
106106
- [ ] packages/packherd-require
107107
- [ ] packages/proxy
108108
- [x] packages/rewriter ✅ **COMPLETED**

packages/network/.mocharc.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/network/lib/agent.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getProxyForUrl } from 'proxy-from-env'
77
import url from 'url'
88
import { createRetryingSocket, getAddress } from './connect'
99
import { lenientOptions } from './http-utils'
10-
import { ClientCertificateStore } from './client-certificates'
10+
import { clientCertificateStoreSingleton } from './client-certificates'
1111
import { CaOptions, getCaOptions } from './ca'
1212

1313
const debug = debugModule('cypress:network:agent')
@@ -55,8 +55,6 @@ const mergeCAOptions = (options: https.RequestOptions, caOptions: CaOptions): ht
5555
}
5656
}
5757

58-
export const clientCertificateStore = new ClientCertificateStore()
59-
6058
type WithProxyOpts<RequestOptions> = RequestOptions & {
6159
proxy: string
6260
shouldRetry?: boolean
@@ -254,7 +252,7 @@ export class CombinedAgent {
254252
debug('got family %o', _.pick(options, 'family', 'href'))
255253

256254
if (isHttps) {
257-
_.assign(options, clientCertificateStore.getClientCertificateAgentOptionsForUrl(uri))
255+
_.assign(options, clientCertificateStoreSingleton.getClientCertificateAgentOptionsForUrl(uri))
258256

259257
return this.httpsAgent.addRequest(req, options as https.RequestOptions)
260258
}

packages/network/lib/client-certificates.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { URL, Url } from 'url'
22
import debugModule from 'debug'
33
import minimatch from 'minimatch'
44
import fs from 'fs-extra'
5-
import { clientCertificateStore } from './agent'
65

76
const { pki, asn1, pkcs12, util } = require('node-forge')
87

@@ -188,6 +187,8 @@ export class ClientCertificateStore {
188187
}
189188
}
190189

190+
export const clientCertificateStoreSingleton = new ClientCertificateStore()
191+
191192
/**
192193
* Load and parse the client certificate configuration. The structure and content of this
193194
* has already been validated; this function reads cert content from file and adds it to the
@@ -200,7 +201,7 @@ export function loadClientCertificateConfig (config) {
200201
let index = 0
201202

202203
try {
203-
clientCertificateStore.clear()
204+
clientCertificateStoreSingleton.clear()
204205

205206
// The basic validation of the certificate configuration has already been done by this point
206207
// within the 'isValidClientCertificatesSet' function within packages/config/src/validation.js
@@ -324,12 +325,12 @@ export function loadClientCertificateConfig (config) {
324325
}
325326
})
326327

327-
clientCertificateStore.addClientCertificatesForUrl(urlClientCertificates)
328+
clientCertificateStoreSingleton.addClientCertificatesForUrl(urlClientCertificates)
328329
index++
329330
})
330331

331332
debug(
332-
`loaded client certificates for ${clientCertificateStore.getCertCount()} URL(s)`,
333+
`loaded client certificates for ${clientCertificateStoreSingleton.getCertCount()} URL(s)`,
333334
)
334335
}
335336
} catch (e: any) {

packages/network/package.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"clean-deps": "rimraf node_modules",
1111
"lint": "eslint --ext .js,.jsx,.ts,.tsx,.json, .",
1212
"test": "yarn test-unit",
13-
"test-debug": "yarn test-unit --inspect-brk=5566",
14-
"test-unit": "mocha --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json",
13+
"test-debug": "npx vitest --inspect-brk --no-file-parallelism --test-timeout=0 --hook-timeout=0",
14+
"test-unit": "vitest run",
1515
"test-watch": "yarn test-unit --watch",
1616
"tslint": "tslint --config ../ts/tslint.json --project ."
1717
},
@@ -32,13 +32,10 @@
3232
"@cypress/request-promise": "^5.0.0",
3333
"@packages/https-proxy": "0.0.0-development",
3434
"@packages/socket": "0.0.0-development",
35-
"@packages/ts": "0.0.0-development",
3635
"@types/concat-stream": "1.6.0",
3736
"express": "4.21.0",
38-
"mocha": "6.2.2",
39-
"sinon": "7.3.1",
40-
"sinon-chai": "3.7.0",
41-
"typescript": "~5.4.5"
37+
"typescript": "~5.4.5",
38+
"vitest": "^3.2.4"
4239
},
4340
"files": [
4441
"lib"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, it, expect } from 'vitest'
2+
import _ from 'lodash'
3+
import net, { AddressInfo } from 'net'
4+
import * as connect from '../../lib/connect'
5+
6+
describe('lib/connect', function () {
7+
describe('.getAddress', function () {
8+
it('resolves localhost on 127.0.0.1 immediately', {
9+
// TODO: fix flaky test https://github.com/cypress-io/cypress/issues/23155
10+
retry: 15,
11+
timeout: 50,
12+
}, async () => {
13+
const server = net.createServer(_.partialRight(_.invoke, 'close'))
14+
15+
// start the server
16+
await new Promise<void>((resolve) => {
17+
server.listen({
18+
port: 0,
19+
host: '127.0.0.1',
20+
}, resolve)
21+
})
22+
23+
const address = await connect.getAddress((server.address() as AddressInfo).port, 'localhost')
24+
25+
expect(address).toEqual({
26+
family: 4,
27+
address: '127.0.0.1',
28+
})
29+
30+
// stop the server
31+
await new Promise<Error>((resolve) => {
32+
server.close(resolve)
33+
})
34+
})
35+
36+
// Error: listen EADDRNOTAVAIL ::1
37+
// NOTE: add an ipv6 lo if to the docker container
38+
it('resolves localhost on ::1 immediately', {
39+
timeout: 50,
40+
}, async () => {
41+
const server = net.createServer(_.partialRight(_.invoke, 'close'))
42+
43+
// start the server
44+
await new Promise<void>((resolve) => {
45+
server.listen({
46+
port: 0,
47+
host: '::1',
48+
}, resolve)
49+
})
50+
51+
const address = await connect.getAddress((server.address() as AddressInfo).port, 'localhost')
52+
53+
expect(address).toEqual({
54+
family: 6,
55+
address: '::1',
56+
})
57+
58+
// stop the server
59+
await new Promise<Error>((resolve) => {
60+
server.close(resolve)
61+
})
62+
})
63+
})
64+
})

packages/network/test/integration/connect_spec.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Promise from 'bluebird'
1+
import Bluebird from 'bluebird'
22
import express from 'express'
33
import http from 'http'
44
import https from 'https'
@@ -31,11 +31,12 @@ function createExpressApp (requestCallback: (req) => void) {
3131
return app
3232
}
3333

34-
function getCAInformation () {
35-
return CA.create()
36-
.then((ca) => {
37-
return Promise.all([ca.generateServerCertificateKeys('localhost'), ca.getCACertPath()])
38-
})
34+
async function getCAInformation () {
35+
const ca = await CA.create()
36+
37+
const [serverCertificateKeys, caCertificatePath] = await Promise.all([ca.generateServerCertificateKeys('localhost'), ca.getCACertPath()])
38+
39+
return { serverCertificateKeys, caCertificatePath }
3940
}
4041

4142
function onWsConnection (socket) {
@@ -51,43 +52,42 @@ export class Servers {
5152
caCertificatePath: string
5253
lastRequestHeaders: any
5354

54-
start (httpPort: number, httpsPort: number) {
55-
return Promise.join(
55+
async start (httpPort: number, httpsPort: number) {
56+
const [app, { serverCertificateKeys, caCertificatePath }]: [Express.Application, {serverCertificateKeys: string[], caCertificatePath: string}] = await Promise.all([
5657
createExpressApp((req) => this.lastRequestHeaders = req.headers),
5758
getCAInformation(),
58-
)
59-
.spread((app: Express.Application, [serverCertificateKeys, caCertificatePath]: [serverCertificateKeys: string[], caCertificatePath: string]) => {
60-
this.httpServer = Promise.promisifyAll(
61-
allowDestroy(http.createServer(app)),
62-
) as http.Server & AsyncServer
63-
64-
this.wsServer = new SocketIOServer(this.httpServer)
65-
66-
this.caCertificatePath = caCertificatePath
67-
this.https = { cert: serverCertificateKeys[0], key: serverCertificateKeys[1] }
68-
this.httpsServer = Promise.promisifyAll(
69-
allowDestroy(https.createServer(this.https, <http.RequestListener>app)),
70-
) as https.Server & AsyncServer
71-
72-
this.wssServer = new SocketIOServer(this.httpsServer)
73-
74-
;[this.wsServer, this.wssServer].map((ws) => {
75-
ws.on('connection', onWsConnection)
76-
})
77-
78-
// @ts-skip
79-
return Promise.join(
80-
this.httpServer.listenAsync(httpPort),
81-
this.httpsServer.listenAsync(httpsPort),
82-
)
83-
.return()
59+
])
60+
61+
this.httpServer = Bluebird.promisifyAll(
62+
allowDestroy(http.createServer(app)),
63+
) as http.Server & AsyncServer
64+
65+
this.wsServer = new SocketIOServer(this.httpServer)
66+
67+
this.caCertificatePath = caCertificatePath
68+
this.https = { cert: serverCertificateKeys[0], key: serverCertificateKeys[1] }
69+
this.httpsServer = Bluebird.promisifyAll(
70+
allowDestroy(https.createServer(this.https, <http.RequestListener>app)),
71+
) as https.Server & AsyncServer
72+
73+
this.wssServer = new SocketIOServer(this.httpsServer)
74+
75+
;[this.wsServer, this.wssServer].map((ws) => {
76+
ws.on('connection', onWsConnection)
8477
})
78+
79+
await Promise.all([
80+
this.httpServer.listenAsync(httpPort),
81+
this.httpsServer.listenAsync(httpsPort),
82+
])
83+
84+
return undefined
8585
}
8686

87-
stop () {
88-
return Promise.join(
87+
async stop () {
88+
await Promise.all([
8989
this.httpServer.destroyAsync(),
9090
this.httpsServer.destroyAsync(),
91-
)
91+
])
9292
}
9393
}

0 commit comments

Comments
 (0)