@@ -16,9 +16,9 @@ function buildCtx(config?: Partial<Vitest['config']>) {
1616 } as unknown as Vitest
1717}
1818
19- function buildWorkspace ( ) {
19+ function buildWorkspace ( name = 'test' ) {
2020 return {
21- name : 'test' ,
21+ name,
2222 config : {
2323 root : import . meta. dirname ,
2424 sequence : { groupOrder : 0 } ,
@@ -28,8 +28,8 @@ function buildWorkspace() {
2828
2929const workspace = buildWorkspace ( )
3030
31- function workspaced ( files : string [ ] ) {
32- return files . map ( ( file ) => new TestSpecification ( workspace , file , 'forks' ) )
31+ function workspaced ( files : string [ ] , project : TestProject = workspace ) {
32+ return files . map ( ( file ) => new TestSpecification ( project , file , 'forks' ) )
3333}
3434
3535describe ( 'base sequencer' , ( ) => {
@@ -67,12 +67,48 @@ describe('base sequencer', () => {
6767} )
6868
6969describe ( 'random sequencer' , ( ) => {
70- test ( 'sorting is the same when seed is defined' , async ( ) => {
70+ function buildRandomSequencer ( seed : number ) {
7171 const ctx = buildCtx ( )
72- ctx . config . sequence . seed = 101
73- const sequencer = new RandomSequencer ( ctx )
72+ ctx . config . sequence . seed = seed
73+ return new RandomSequencer ( ctx )
74+ }
75+
76+ function names ( specs : TestSpecification [ ] ) {
77+ return specs . map ( ( spec ) => `${ spec . project . name } /${ spec . moduleId } ` )
78+ }
79+
80+ test ( 'sorting is the same when seed is defined' , async ( ) => {
81+ const sequencer = buildRandomSequencer ( 101 )
82+ const sorted = await sequencer . sort ( workspaced ( [ 'b' , 'a' , 'c' ] ) )
83+ expect ( names ( sorted ) ) . toMatchInlineSnapshot ( `
84+ [
85+ "test/b",
86+ "test/c",
87+ "test/a",
88+ ]
89+ ` )
90+ } )
91+
92+ test ( 'the order does not depend on the order of the input' , async ( ) => {
93+ const sequencer = buildRandomSequencer ( 101 )
94+ const web = buildWorkspace ( 'web' )
95+ const node = buildWorkspace ( 'node' )
96+ const first = await sequencer . sort ( [
97+ ...workspaced ( [ 'a' , 'b' , 'c' ] , web ) ,
98+ ...workspaced ( [ 'a' , 'b' , 'c' ] , node ) ,
99+ ] )
100+ const second = await sequencer . sort ( [
101+ ...workspaced ( [ 'c' , 'a' ] , node ) ,
102+ ...workspaced ( [ 'b' , 'c' , 'a' ] , web ) ,
103+ ...workspaced ( [ 'b' ] , node ) ,
104+ ] )
105+ expect ( names ( second ) ) . toEqual ( names ( first ) )
106+ } )
107+
108+ test ( 'does not change the input' , async ( ) => {
109+ const sequencer = buildRandomSequencer ( 101 )
74110 const files = workspaced ( [ 'b' , 'a' , 'c' ] )
75- const sorted = await sequencer . sort ( files )
76- expect ( sorted ) . toStrictEqual ( workspaced ( [ 'a ', 'c ' , 'b' ] ) )
111+ await sequencer . sort ( files )
112+ expect ( names ( files ) ) . toEqual ( [ 'test/b ', 'test/a ' , 'test/c' ] )
77113 } )
78114} )
0 commit comments