Repository: cypress-io/cypress
File: scripts/unit/github-actions/update-browser-version-spec.js (JavaScript)

Code before:
}

const stubRepoVersions = ({ betaVersion, stableVersion }) => {
  mockfs({
    [CIRCLECI_WORKFLOWS_FILEPATH]: `chrome-stable-version: &chrome-stable-version "${stableVersion}"\nchrome-beta-version: &chrome-beta-version "${betaVersion}"\n`,
  })
}

const stubChromeVersions = ({ betaVersion, stableVersion }) => {
  if (!global.originalFetch) {
    global.originalFetch = global.fetch
  }

  const stableResponse = {
    versions: stableVersion ? [{ name: `chrome/platforms/linux/channels/stable/versions/${stableVersion}`, version: stableVersion }] : [],
    nextPageToken: '',
  }

  const betaResponse = {
    versions: betaVersion ? [{ name: `chrome/platforms/linux/channels/beta/versions/${betaVersion}`, version: betaVersion }] : [],
    nextPageToken: '',
  }

  global.fetch = sinon.stub().callsFake((url) => {
    if (url.includes('/channels/stable/')) {
      return Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve(JSON.stringify(stableResponse)) })
    }

    if (url.includes('/channels/beta/')) {
      return Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve(JSON.stringify(betaResponse)) })
    }

    throw new Error(`Unexpected fetch URL: ${url}`)
  })
}

describe('update browser version github action', () => {
  beforeEach(() => {
    sinon.restore()
    mockfs.restore()
    if (global.originalFetch) {
      global.fetch = global.originalFetch
      delete global.originalFetch
    }
  })

  context('.getVersions', () => {
    beforeEach(() => {
      stubRepoVersions({
        betaVersion: '1.1',
        stableVersion: '1.0',

Review comment (suggestion):
nock doesn't mock Node fetch, so replacing that. Also - it was weird nock was being used at the root as a dependency without being defined in root package.json because this was hoisting from other packages.

Code after:
}

const stubRepoVersions = ({ betaVersion, stableVersion }) => {
  mockfs({
    [CIRCLECI_WORKFLOWS_FILEPATH]: `chrome-stable-version: &chrome-stable-version "${stableVersion}"\nchrome-beta-version: &chrome-beta-version "${betaVersion}"\n`,
  })
}

const stubChromeVersions = ({ betaVersion, stableVersion }) => {
  if (!global.originalFetch) {
    global.originalFetch = global.fetch
  }

  const stableResponse = {
    versions: stableVersion ? [{ name: `chrome/platforms/linux/channels/stable/versions/${stableVersion}`, version: stableVersion }] : [],
    nextPageToken: '',
  }

  const betaResponse = {
    versions: betaVersion ? [{ name: `chrome/platforms/linux/channels/beta/versions/${betaVersion}`, version: betaVersion }] : [],
    nextPageToken: '',
  }

  global.fetch = sinon.stub().callsFake((url) => {
    if (url.includes('/channels/stable/')) {
      return Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve(JSON.stringify(stableResponse)) })
    }

    if (url.includes('/channels/beta/')) {
      return Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve(JSON.stringify(betaResponse)) })
    }

    throw new Error(`Unexpected fetch URL: ${url}`)
  })
}

describe('update browser version github action', () => {
  beforeEach(() => {
    sinon.restore()
    mockfs.restore()
  })

  afterEach(() => {
    if (global.originalFetch) {
      global.fetch = global.originalFetch
      delete global.originalFetch
    }
  })

  context('.getVersions', () => {
    beforeEach(() => {