-
Notifications
You must be signed in to change notification settings - Fork 27
/
chain-permutations.ts
127 lines (117 loc) · 3.42 KB
/
chain-permutations.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// imported from https://github.com/veritem/eslint-plugin-vitest/pull/293
// This script generates all possible permutations for vitest methods
import { per } from 'percom'
import fs from 'node:fs'
import path from 'node:path'
const data = [
{
names: ['beforeEach', 'beforeAll', 'afterEach', 'afterAll'],
first: [],
conditions: [],
methods: [],
last: []
},
{
names: ['it', 'test'],
first: ['extend'],
conditions: ['skipIf', 'runIf'],
methods: ['skip', 'only', 'concurrent', 'sequential', 'todo', 'fails'],
last: ['each']
},
{
names: ['bench'],
first: [],
conditions: ['skipIf', 'runIf'],
methods: ['skip', 'only', 'todo'],
last: []
},
{
names: ['describe'],
first: [],
conditions: ['skipIf', 'runIf'],
methods: ['skip', 'only', 'concurrent', 'sequential', 'shuffle', 'todo'],
last: ['each']
},
{
names: ['suite'],
first: [],
conditions: ['skipIf', 'runIf'],
methods: ['skip', 'only', 'concurrent', 'sequential', 'shuffle', 'todo'],
last: ['each']
}
]
const DEPTH = 3
const allPermutations: string[] = []
const depths = (maxDepth) => Array.from({ length: maxDepth }, (_, i) => i)
data.forEach((q) => {
q.names.forEach((name) => {
allPermutations.push(name)
const maxDepth = Math.min(DEPTH, q.methods.length)
const methodPerms = depths(maxDepth).flatMap((i) => [
...per(q.methods, i + 1),
...q.first.flatMap((first) =>
(per(q.methods, i) || ['']).map((p) => [first, ...p])
),
...q.conditions.flatMap((condition) =>
(per(q.methods, i) || ['']).map((p) => [condition, ...p])
),
...q.last.flatMap((last) =>
(per(q.methods, i) || ['']).map((p) => [...p, last])
),
...(i > 0
? q.first.flatMap((first) =>
q.conditions.flatMap((condition) =>
(per(q.methods, i - 1) || ['']).map((p) => [
first,
condition,
...p
])
)
)
: []),
...(i > 0
? q.first.flatMap((first) =>
q.last.flatMap((last) =>
(per(q.methods, i - 1) || ['']).map((p) => [first, ...p, last])
)
)
: []),
...(i > 0
? q.conditions.flatMap((condition) =>
q.last.flatMap((last) =>
(per(q.methods, i - 1) || ['']).map((p) => [
condition,
...p,
last
])
)
)
: []),
...(i > 1
? q.first.flatMap((first) =>
q.conditions.flatMap((condition) =>
q.last.flatMap((last) =>
(per(q.methods, i - 2) || ['']).map((p) => [
first,
condition,
...p,
last
])
)
)
)
: [])
])
const allPerms = methodPerms.map((p) => [name, ...p].join('.'))
allPermutations.push(...allPerms)
})
})
const extra_rules = ['xtest', 'xtest.each', 'xit', 'xit.each', 'fit', 'xdescribe', 'xdescribe.each', 'fdescribe']
const output = `export const ValidVitestFnCallChains = new Set([${allPermutations.concat(extra_rules).map(item => `'${item}'`)}])`
const new_path = path.resolve(__dirname, '../src/utils/validVitestFnCallChains.ts')
try {
fs.writeFileSync(new_path, output)
console.log(`done writing to ${new_path.split('/')[new_path.split('/').length - 1]}`)
} catch (err) {
console.log(`err: ${err.message}`)
}