diff --git a/e2e/__tests__/jestChangedFiles.test.ts b/e2e/__tests__/jestChangedFiles.test.ts index 0cab7aa45340..bbc75f5ab44c 100644 --- a/e2e/__tests__/jestChangedFiles.test.ts +++ b/e2e/__tests__/jestChangedFiles.test.ts @@ -8,7 +8,8 @@ import {tmpdir} from 'os'; import * as path from 'path'; import {wrap} from 'jest-snapshot-serializer-raw'; -import slash from 'slash'; +import semver = require('semver'); +import slash = require('slash'); import {findRepos, getChangedFilesForRoots} from 'jest-changed-files'; import {cleanup, run, testIfHg, writeFiles} from '../Utils'; import runJest from '../runJest'; @@ -18,6 +19,23 @@ const DIR = path.resolve(tmpdir(), 'jest-changed-files-test-dir'); const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com'; const HG = 'hg --config ui.username=jest_test'; +const gitVersionSupportsInitialBranch = (() => { + const {stdout} = run(`${GIT} --version`); + const gitVersion = stdout.split(' ').slice(-1)[0]; + + return semver.gte(gitVersion, '2.28.0'); +})(); + +const mainBranchName = gitVersionSupportsInitialBranch ? 'main' : 'master'; + +function gitInit(dir: string) { + const initCommand = gitVersionSupportsInitialBranch + ? `${GIT} init --initial-branch=${mainBranchName}` + : `${GIT} init`; + + run(initCommand, dir); +} + beforeEach(() => cleanup(DIR)); afterEach(() => cleanup(DIR)); @@ -70,8 +88,8 @@ test('gets git SCM roots and dedupes them', async () => { 'second-repo/nested-dir/second-nested-dir/file3.txt': 'file3', }); - run(`${GIT} init`, path.resolve(DIR, 'first-repo')); - run(`${GIT} init`, path.resolve(DIR, 'second-repo')); + gitInit(path.resolve(DIR, 'first-repo')); + gitInit(path.resolve(DIR, 'second-repo')); const roots = [ '', @@ -108,7 +126,7 @@ testIfHg('gets mixed git and hg SCM roots and dedupes them', async () => { 'second-repo/nested-dir/second-nested-dir/file3.txt': 'file3', }); - run(`${GIT} init`, path.resolve(DIR, 'first-repo')); + gitInit(path.resolve(DIR, 'first-repo')); run(`${HG} init`, path.resolve(DIR, 'second-repo')); const roots = [ @@ -142,7 +160,7 @@ test('gets changed files for git', async () => { 'nested-dir/second-nested-dir/file3.txt': 'file3', }); - run(`${GIT} init`, DIR); + gitInit(DIR); const roots = [ '', @@ -235,9 +253,9 @@ test('gets changed files for git', async () => { run(`${GIT} commit --no-gpg-sign -m "test5"`, DIR); ({changedFiles: files} = await getChangedFilesForRoots(roots, { - changedSince: 'master', + changedSince: mainBranchName, })); - // Returns files from this branch but not ones that only exist on master + // Returns files from this branch but not ones that only exist on mainBranchName expect( Array.from(files) .map(filePath => path.basename(filePath)) @@ -252,7 +270,7 @@ test('monitors only root paths for git', async () => { 'nested-dir/second-nested-dir/file3.txt': 'file3', }); - run(`${GIT} init`, DIR); + gitInit(DIR); const roots = [path.resolve(DIR, 'nested-dir')]; @@ -267,11 +285,9 @@ test('monitors only root paths for git', async () => { it('does not find changes in files with no diff, for git', async () => { const roots = [path.resolve(DIR)]; - // create an empty file, commit it to "master" - writeFiles(DIR, { - 'file1.txt': '', - }); - run(`${GIT} init`, DIR); + // create an empty file, commit it to "mainBranchName" + writeFiles(DIR, {'file1.txt': ''}); + gitInit(DIR); run(`${GIT} add file1.txt`, DIR); run(`${GIT} commit --no-gpg-sign -m "initial"`, DIR); @@ -315,7 +331,7 @@ test('handles a bad revision for "changedSince", for git', async () => { 'package.json': '{}', }); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); diff --git a/e2e/__tests__/onlyChanged.test.ts b/e2e/__tests__/onlyChanged.test.ts index 6690bf528047..d237754f5c5d 100644 --- a/e2e/__tests__/onlyChanged.test.ts +++ b/e2e/__tests__/onlyChanged.test.ts @@ -7,6 +7,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; +import semver = require('semver'); import {cleanup, run, testIfHg, writeFiles} from '../Utils'; import runJest from '../runJest'; @@ -14,6 +15,23 @@ const DIR = path.resolve(tmpdir(), 'jest_only_changed'); const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com'; const HG = 'hg --config ui.username=jest_test'; +const gitVersionSupportsInitialBranch = (() => { + const {stdout} = run(`${GIT} --version`); + const gitVersion = stdout.split(' ').slice(-1)[0]; + + return semver.gte(gitVersion, '2.28.0'); +})(); + +const mainBranchName = gitVersionSupportsInitialBranch ? 'main' : 'master'; + +function gitInit(dir: string) { + const initCommand = gitVersionSupportsInitialBranch + ? `${GIT} init --initial-branch=${mainBranchName}` + : `${GIT} init`; + + run(initCommand, dir); +} + beforeEach(() => cleanup(DIR)); afterEach(() => cleanup(DIR)); @@ -25,7 +43,7 @@ test('run for "onlyChanged" and "changedSince"', () => { 'package.json': '{}', }); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); @@ -34,9 +52,9 @@ test('run for "onlyChanged" and "changedSince"', () => { /No tests found related to files changed since last commit./, ); - stdout = runJest(DIR, ['--changedSince=master']).stdout; + stdout = runJest(DIR, [`--changedSince=${mainBranchName}`]).stdout; expect(stdout).toMatch( - /No tests found related to files changed since "master"./, + `No tests found related to files changed since "${mainBranchName}".`, ); }); @@ -53,7 +71,7 @@ test('run only changed files', () => { ({stdout} = runJest(DIR, ['-o'])); expect(stdout).toMatch(/Jest can only find uncommitted changed files/); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); @@ -114,7 +132,7 @@ test('report test coverage for only changed files', () => { }), }); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); @@ -153,7 +171,7 @@ test('report test coverage of source on test file change under only changed file }), }); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); @@ -177,7 +195,7 @@ test('do not pickup non-tested files when reporting coverage on only changed fil 'package.json': JSON.stringify({name: 'original name'}), }); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); @@ -204,7 +222,7 @@ test('collect test coverage when using onlyChanged', () => { }), }); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); run(`${GIT} checkout -b new-branch`, DIR); @@ -233,7 +251,7 @@ test('onlyChanged in config is overwritten by --all or testPathPattern', () => { ({stdout} = runJest(DIR)); expect(stdout).toMatch(/Jest can only find uncommitted changed files/); - run(`${GIT} init`, DIR); + gitInit(DIR); run(`${GIT} add .`, DIR); run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); @@ -351,7 +369,7 @@ test('path on Windows is case-insensitive', () => { 'package.json': '{}', }); - run(`${GIT} init`, modifiedDIR); + gitInit(modifiedDIR); run(`${GIT} add .`, modifiedDIR); run(`${GIT} commit --no-gpg-sign -m "first"`, modifiedDIR);