From 474f02698050969e409e616b6ea1e20a16a3067e Mon Sep 17 00:00:00 2001 From: Mike Plummer Date: Mon, 13 Jun 2022 12:00:23 -0500 Subject: [PATCH] fix: Time out loading embed html for migration video, stub in e2e tests (#22165) --- .../src/sources/MigrationDataSource.ts | 8 +- .../unit/sources/MigrationDataSource.spec.ts | 77 +++++++++++++++++++ .../launchpad/cypress/e2e/migration.cy.ts | 40 +++++++--- 3 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 packages/data-context/test/unit/sources/MigrationDataSource.spec.ts diff --git a/packages/data-context/src/sources/MigrationDataSource.ts b/packages/data-context/src/sources/MigrationDataSource.ts index 8e099ea2a7db..849b28ada639 100644 --- a/packages/data-context/src/sources/MigrationDataSource.ts +++ b/packages/data-context/src/sources/MigrationDataSource.ts @@ -104,8 +104,12 @@ export class MigrationDataSource { const versionData = await this.ctx.versions.versionData() const embedOnLink = `https://on.cypress.io/v10-video-embed/${versionData.current.version}` + // Time out request if it takes longer than 3 seconds + const controller = new AbortController() + const timeoutId = setTimeout(() => controller.abort(), 3000) + try { - const response = await this.ctx.util.fetch(embedOnLink, { method: 'GET' }) + const response = await this.ctx.util.fetch(embedOnLink, { method: 'GET', signal: controller.signal }) const { videoHtml } = await response.json() this.ctx.update((d) => { @@ -116,6 +120,8 @@ export class MigrationDataSource { } catch { // fail silently, no user-facing error is needed return null + } finally { + clearTimeout(timeoutId) } } diff --git a/packages/data-context/test/unit/sources/MigrationDataSource.spec.ts b/packages/data-context/test/unit/sources/MigrationDataSource.spec.ts new file mode 100644 index 000000000000..4af5f767172d --- /dev/null +++ b/packages/data-context/test/unit/sources/MigrationDataSource.spec.ts @@ -0,0 +1,77 @@ +import { expect } from 'chai' +import dedent from 'dedent' +import sinon from 'sinon' + +import { DataContext } from '../../../src' +import { MigrationDataSource } from '../../../src/sources' +import { createTestDataContext } from '../helper' + +const pkg = require('@packages/root') + +describe('MigrationDataSource', () => { + context('.migration', () => { + let ctx: DataContext + let fetchStub: sinon.SinonStub + + beforeEach(() => { + ctx = createTestDataContext('open') + + ctx.coreData.currentTestingType = 'e2e' + + fetchStub = sinon.stub() + + sinon.stub(ctx.util, 'fetch').callsFake(fetchStub) + }) + + afterEach(() => { + fetchStub.reset() + sinon.restore() + }) + + describe('getVideoEmbedHtml', () => { + const expectedPayload = { + videoHtml: dedent` +