/
sequencers.test.ts
112 lines (104 loc) · 3.52 KB
/
sequencers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type { Vitest } from 'vitest'
import { describe, expect, test, vi } from 'vitest'
import { RandomSequencer } from 'vitest/src/node/sequencers/RandomSequencer'
import { BaseSequencer } from 'vitest/src/node/sequencers/BaseSequencer'
const buildCtx = () => {
return {
config: {
sequence: {},
},
cache: {
getFileTestResults: vi.fn(),
getFileStats: vi.fn(),
},
} as unknown as Vitest
}
describe('base sequencer', () => {
test('sorting when no info is available', async () => {
const sequencer = new BaseSequencer(buildCtx())
const files = ['a', 'b', 'c']
const sorted = await sequencer.sort(files)
expect(sorted).toStrictEqual(files)
})
test('prioritize unknown files', async () => {
const ctx = buildCtx()
vi.spyOn(ctx.cache, 'getFileStats').mockImplementation((file) => {
if (file === 'b')
return { size: 2 }
})
const sequencer = new BaseSequencer(ctx)
const files = ['b', 'a', 'c']
const sorted = await sequencer.sort(files)
expect(sorted).toStrictEqual(['a', 'c', 'b'])
})
test('sort by size, larger first', async () => {
const ctx = buildCtx()
vi.spyOn(ctx.cache, 'getFileStats').mockImplementation((file) => {
if (file === 'a')
return { size: 1 }
if (file === 'b')
return { size: 2 }
if (file === 'c')
return { size: 3 }
})
const sequencer = new BaseSequencer(ctx)
const files = ['b', 'a', 'c']
const sorted = await sequencer.sort(files)
expect(sorted).toStrictEqual(['c', 'b', 'a'])
})
test('sort by results, failed first', async () => {
const ctx = buildCtx()
vi.spyOn(ctx.cache, 'getFileTestResults').mockImplementation((file) => {
if (file === 'a')
return { failed: false, duration: 1 }
if (file === 'b')
return { failed: true, duration: 1 }
if (file === 'c')
return { failed: true, duration: 1 }
})
const sequencer = new BaseSequencer(ctx)
const files = ['b', 'a', 'c']
const sorted = await sequencer.sort(files)
expect(sorted).toStrictEqual(['b', 'c', 'a'])
})
test('sort by results, long first', async () => {
const ctx = buildCtx()
vi.spyOn(ctx.cache, 'getFileTestResults').mockImplementation((file) => {
if (file === 'a')
return { failed: true, duration: 1 }
if (file === 'b')
return { failed: true, duration: 2 }
if (file === 'c')
return { failed: true, duration: 3 }
})
const sequencer = new BaseSequencer(ctx)
const files = ['b', 'a', 'c']
const sorted = await sequencer.sort(files)
expect(sorted).toStrictEqual(['c', 'b', 'a'])
})
test('sort by results, long and failed first', async () => {
const ctx = buildCtx()
vi.spyOn(ctx.cache, 'getFileTestResults').mockImplementation((file) => {
if (file === 'a')
return { failed: false, duration: 1 }
if (file === 'b')
return { failed: false, duration: 6 }
if (file === 'c')
return { failed: true, duration: 3 }
})
const sequencer = new BaseSequencer(ctx)
const files = ['b', 'a', 'c']
const sorted = await sequencer.sort(files)
expect(sorted).toStrictEqual(['c', 'b', 'a'])
})
})
describe('random sequencer', () => {
test('sorting is the same when seed is defined', async () => {
const ctx = buildCtx()
ctx.config.sequence.seed = 101
const sequencer = new RandomSequencer(ctx)
const files = ['b', 'a', 'c']
const sorted = await sequencer.sort(files)
expect(sorted).toStrictEqual(['a', 'c', 'b'])
})
})