Skip to content

Commit 333e25d

Browse files
authored
fix: #22081 Cypress crashing behind proxy (#22099)
1 parent f987981 commit 333e25d

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

Diff for: packages/data-context/src/sources/VersionsDataSource.ts

+18-24
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ export class VersionsDataSource {
100100
}
101101

102102
private async getVersionMetadata (): Promise<Record<string, string>> {
103+
const now = new Date().toISOString()
103104
const DEFAULT_RESPONSE = {
104-
[pkg.version]: new Date().toISOString(),
105+
[pkg.version]: now,
105106
}
106107

107108
if (this.ctx.isRunMode) {
@@ -112,21 +113,18 @@ export class VersionsDataSource {
112113

113114
try {
114115
response = await this.ctx.util.fetch(NPM_CYPRESS_REGISTRY)
116+
const responseJson = await response.json() as { time: Record<string, string>}
117+
118+
debug('NPM release dates received %o', { modified: responseJson.time.modified })
119+
120+
return responseJson.time ?? now
115121
} catch (e) {
116122
// ignore any error from this fetch, they are gracefully handled
117123
// by showing the current version only
118124
debug('Error fetching %o', NPM_CYPRESS_REGISTRY, e)
119-
}
120125

121-
if (!response) {
122126
return DEFAULT_RESPONSE
123127
}
124-
125-
const responseJson = await response.json() as { time: Record<string, string>}
126-
127-
debug('NPM release dates received %o', { modified: responseJson.time.modified })
128-
129-
return responseJson.time
130128
}
131129

132130
private async getLatestVersion (): Promise<string> {
@@ -153,31 +151,27 @@ export class VersionsDataSource {
153151
manifestHeaders['x-machine-id'] = id
154152
}
155153

156-
let manifestResponse
157-
158154
try {
159-
manifestResponse = await this.ctx.util.fetch(url, {
155+
const manifestResponse = await this.ctx.util.fetch(url, {
160156
headers: manifestHeaders,
161157
})
158+
159+
debug('retrieving latest version information with headers: %o', manifestHeaders)
160+
161+
const manifest = await manifestResponse.json() as { version: string }
162+
163+
debug('latest version information: %o', manifest)
164+
165+
return manifest.version ?? pkg.version
162166
} catch (e) {
163167
// ignore any error from this fetch, they are gracefully handled
164168
// by showing the current version only
165169
debug('Error fetching %o', url, e)
166-
}
167170

168-
if (!manifestResponse) {
169171
return pkg.version
172+
} finally {
173+
this._initialLaunch = false
170174
}
171-
172-
debug('retrieving latest version information with headers: %o', manifestHeaders)
173-
174-
const manifest = await manifestResponse.json() as { version: string }
175-
176-
debug('latest version information: %o', manifest)
177-
178-
this._initialLaunch = false
179-
180-
return manifest.version
181175
}
182176

183177
private static async machineId (): Promise<string | undefined> {

Diff for: packages/data-context/test/unit/sources/VersionsDataSource.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from 'chai'
22
import os from 'os'
33
import sinon from 'sinon'
4+
import { Response } from 'cross-fetch'
5+
46
import { DataContext } from '../../../src'
57
import { VersionsDataSource } from '../../../src/sources'
68
import { createTestDataContext } from '../helper'
@@ -146,5 +148,39 @@ describe('VersionsDataSource', () => {
146148

147149
expect(versionInfo.current.version).to.eql(currentCypressVersion)
148150
})
151+
152+
it('handles invalid response errors', async () => {
153+
nmiStub.resolves('abcd123')
154+
155+
fetchStub
156+
.withArgs('https://download.cypress.io/desktop.json', {
157+
headers: {
158+
'Content-Type': 'application/json',
159+
'x-cypress-version': currentCypressVersion,
160+
'x-os-name': 'darwin',
161+
'x-arch': 'x64',
162+
'x-initial-launch': String(true),
163+
'x-machine-id': 'abcd123',
164+
'x-testing-type': 'e2e',
165+
},
166+
})
167+
.callsFake(async () => new Response('Error'))
168+
.withArgs('https://registry.npmjs.org/cypress')
169+
.callsFake(async () => new Response('Error'))
170+
171+
versionsDataSource = new VersionsDataSource(ctx)
172+
173+
const versionInfo = await versionsDataSource.versionData()
174+
175+
// Reset the testing type in the class
176+
// @ts-ignore
177+
versionsDataSource._currentTestingType = null
178+
179+
versionsDataSource.resetLatestVersionTelemetry()
180+
181+
await ctx.coreData.versionData.latestVersion
182+
183+
expect(versionInfo.current.version).to.eql(currentCypressVersion)
184+
})
149185
})
150186
})

0 commit comments

Comments
 (0)