77
88import { tmpdir } from 'os' ;
99import * as path from 'path' ;
10+ import semver = require( 'semver' ) ;
1011import { cleanup , run , testIfHg , writeFiles } from '../Utils' ;
1112import runJest from '../runJest' ;
1213
1314const DIR = path . resolve ( tmpdir ( ) , 'jest_only_changed' ) ;
1415const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com' ;
1516const HG = 'hg --config ui.username=jest_test' ;
1617
18+ const gitVersionSupportsInitialBranch = ( ( ) => {
19+ const { stdout} = run ( `${ GIT } --version` ) ;
20+ const gitVersion = stdout . split ( ' ' ) . slice ( - 1 ) [ 0 ] ;
21+
22+ return semver . gte ( gitVersion , '2.28.0' ) ;
23+ } ) ( ) ;
24+
25+ const mainBranchName = gitVersionSupportsInitialBranch ? 'main' : 'master' ;
26+
27+ function gitInit ( dir : string ) {
28+ const initCommand = gitVersionSupportsInitialBranch
29+ ? `${ GIT } init --initial-branch=${ mainBranchName } `
30+ : `${ GIT } init` ;
31+
32+ run ( initCommand , dir ) ;
33+ }
34+
1735beforeEach ( ( ) => cleanup ( DIR ) ) ;
1836afterEach ( ( ) => cleanup ( DIR ) ) ;
1937
@@ -25,7 +43,7 @@ test('run for "onlyChanged" and "changedSince"', () => {
2543 'package.json' : '{}' ,
2644 } ) ;
2745
28- run ( ` ${ GIT } init` , DIR ) ;
46+ gitInit ( DIR ) ;
2947 run ( `${ GIT } add .` , DIR ) ;
3048 run ( `${ GIT } commit --no-gpg-sign -m "first"` , DIR ) ;
3149
@@ -34,9 +52,9 @@ test('run for "onlyChanged" and "changedSince"', () => {
3452 / N o t e s t s f o u n d r e l a t e d t o f i l e s c h a n g e d s i n c e l a s t c o m m i t ./ ,
3553 ) ;
3654
37- stdout = runJest ( DIR , [ ' --changedSince=master' ] ) . stdout ;
55+ stdout = runJest ( DIR , [ ` --changedSince=${ mainBranchName } ` ] ) . stdout ;
3856 expect ( stdout ) . toMatch (
39- / N o t e s t s f o u n d r e l a t e d t o f i l e s c h a n g e d s i n c e " m a s t e r " . / ,
57+ ` No tests found related to files changed since "${ mainBranchName } ".` ,
4058 ) ;
4159} ) ;
4260
@@ -53,7 +71,7 @@ test('run only changed files', () => {
5371 ( { stdout} = runJest ( DIR , [ '-o' ] ) ) ;
5472 expect ( stdout ) . toMatch ( / J e s t c a n o n l y f i n d u n c o m m i t t e d c h a n g e d f i l e s / ) ;
5573
56- run ( ` ${ GIT } init` , DIR ) ;
74+ gitInit ( DIR ) ;
5775 run ( `${ GIT } add .` , DIR ) ;
5876 run ( `${ GIT } commit --no-gpg-sign -m "first"` , DIR ) ;
5977
@@ -114,7 +132,7 @@ test('report test coverage for only changed files', () => {
114132 } ) ,
115133 } ) ;
116134
117- run ( ` ${ GIT } init` , DIR ) ;
135+ gitInit ( DIR ) ;
118136 run ( `${ GIT } add .` , DIR ) ;
119137 run ( `${ GIT } commit --no-gpg-sign -m "first"` , DIR ) ;
120138
@@ -153,7 +171,7 @@ test('report test coverage of source on test file change under only changed file
153171 } ) ,
154172 } ) ;
155173
156- run ( ` ${ GIT } init` , DIR ) ;
174+ gitInit ( DIR ) ;
157175 run ( `${ GIT } add .` , DIR ) ;
158176 run ( `${ GIT } commit --no-gpg-sign -m "first"` , DIR ) ;
159177
@@ -177,7 +195,7 @@ test('do not pickup non-tested files when reporting coverage on only changed fil
177195 'package.json' : JSON . stringify ( { name : 'original name' } ) ,
178196 } ) ;
179197
180- run ( ` ${ GIT } init` , DIR ) ;
198+ gitInit ( DIR ) ;
181199 run ( `${ GIT } add .` , DIR ) ;
182200 run ( `${ GIT } commit --no-gpg-sign -m "first"` , DIR ) ;
183201
@@ -204,7 +222,7 @@ test('collect test coverage when using onlyChanged', () => {
204222 } ) ,
205223 } ) ;
206224
207- run ( ` ${ GIT } init` , DIR ) ;
225+ gitInit ( DIR ) ;
208226 run ( `${ GIT } add .` , DIR ) ;
209227 run ( `${ GIT } commit --no-gpg-sign -m "first"` , DIR ) ;
210228 run ( `${ GIT } checkout -b new-branch` , DIR ) ;
@@ -233,7 +251,7 @@ test('onlyChanged in config is overwritten by --all or testPathPattern', () => {
233251 ( { stdout} = runJest ( DIR ) ) ;
234252 expect ( stdout ) . toMatch ( / J e s t c a n o n l y f i n d u n c o m m i t t e d c h a n g e d f i l e s / ) ;
235253
236- run ( ` ${ GIT } init` , DIR ) ;
254+ gitInit ( DIR ) ;
237255 run ( `${ GIT } add .` , DIR ) ;
238256 run ( `${ GIT } commit --no-gpg-sign -m "first"` , DIR ) ;
239257
@@ -351,7 +369,7 @@ test('path on Windows is case-insensitive', () => {
351369 'package.json' : '{}' ,
352370 } ) ;
353371
354- run ( ` ${ GIT } init` , modifiedDIR ) ;
372+ gitInit ( modifiedDIR ) ;
355373 run ( `${ GIT } add .` , modifiedDIR ) ;
356374 run ( `${ GIT } commit --no-gpg-sign -m "first"` , modifiedDIR ) ;
357375
0 commit comments