diff --git a/e2e/Utils.ts b/e2e/Utils.ts index 614831acae17..52945c6f1041 100644 --- a/e2e/Utils.ts +++ b/e2e/Utils.ts @@ -14,6 +14,7 @@ import {ExecaReturnValue, sync as spawnSync} from 'execa'; import makeDir = require('make-dir'); import rimraf = require('rimraf'); import dedent = require('dedent'); +import which = require('which'); interface RunResult extends ExecaReturnValue { status: number; @@ -260,3 +261,19 @@ export const normalizeIcons = (str: string) => { .replace(new RegExp('\u00D7', 'g'), '\u2715') .replace(new RegExp('\u221A', 'g'), '\u2713'); }; + +// Certain environments (like CITGM and GH Actions) do not come with mercurial installed +let hgIsInstalled: boolean | null = null; + +export const testIfHg = (...args: Parameters) => { + if (hgIsInstalled === null) { + hgIsInstalled = which.sync('hg', {nothrow: true}) !== null; + } + + if (hgIsInstalled) { + test(...args); + } else { + console.warn('Mercurial (hg) is not installed - skipping some tests'); + test.skip(...args); + } +}; diff --git a/e2e/__tests__/jestChangedFiles.test.ts b/e2e/__tests__/jestChangedFiles.test.ts index 5b8e3014cd7b..480e1d966187 100644 --- a/e2e/__tests__/jestChangedFiles.test.ts +++ b/e2e/__tests__/jestChangedFiles.test.ts @@ -10,8 +10,7 @@ import * as path from 'path'; import {wrap} from 'jest-snapshot-serializer-raw'; import {findRepos, getChangedFilesForRoots} from 'jest-changed-files'; import {skipSuiteOnWindows} from '@jest/test-utils'; -import which = require('which'); -import {cleanup, run, writeFiles} from '../Utils'; +import {cleanup, run, testIfHg, writeFiles} from '../Utils'; import runJest from '../runJest'; skipSuiteOnWindows(); @@ -24,14 +23,6 @@ const HG = 'hg --config ui.username=jest_test'; beforeEach(() => cleanup(DIR)); afterEach(() => cleanup(DIR)); -// Certain environments (like CITGM and GH Actions) do not come with mercurial installed -const hgIsInstalled = which.sync('hg', {nothrow: true}) !== null; -const testIfHg = hgIsInstalled ? test : test.skip; - -if (!hgIsInstalled) { - console.warn('Mercurial (hg) is not installed - skipping some tests'); -} - testIfHg('gets hg SCM roots and dedupes them', async () => { writeFiles(DIR, { 'first-repo/file1.txt': 'file1', diff --git a/e2e/__tests__/onlyChanged.test.ts b/e2e/__tests__/onlyChanged.test.ts index 3a7ff38da596..52abdd6d0613 100644 --- a/e2e/__tests__/onlyChanged.test.ts +++ b/e2e/__tests__/onlyChanged.test.ts @@ -8,7 +8,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; import runJest from '../runJest'; -import {cleanup, run, writeFiles} from '../Utils'; +import {cleanup, run, testIfHg, writeFiles} from '../Utils'; const DIR = path.resolve(tmpdir(), 'jest_only_changed'); const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com'; @@ -252,7 +252,7 @@ test('onlyChanged in config is overwritten by --all or testPathPattern', () => { expect(stderr).toMatch(/PASS __tests__(\/|\\)file3.test.js/); }); -test('gets changed files for hg', async () => { +testIfHg('gets changed files for hg', async () => { if (process.env.CI) { // Circle and Travis have very old version of hg (v2, and current // version is v4.2) and its API changed since then and not compatible