-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
188 lines (166 loc) · 5.83 KB
/
index.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/// <reference types="cypress" />
// standard Node module "util" has "format" function
const { format } = require('util')
function formatTitle(pattern, ...values) {
// count how many format placeholders are in the pattern
// by counting the "%" characters
const placeholders = pattern.match(/%/g)
const count = placeholders ? placeholders.length : 0
return format.apply(null, [pattern].concat(values.slice(0, count)))
}
function getChunk(values, totalChunks, chunkIndex) {
// split all items into N chunks and take just a single chunk
if (totalChunks < 0) {
throw new Error('totalChunks must be >= 0')
}
if (chunkIndex < 0 || chunkIndex >= totalChunks) {
throw new Error(
`Invalid chunk index ${chunkIndex} vs all chunks ${totalChunks}`,
)
}
const chunkSize = Math.ceil(values.length / totalChunks)
const chunkStart = chunkIndex * chunkSize
const chunkEnd = chunkStart + chunkSize
const chunk = values.slice(chunkStart, chunkEnd)
return chunk
}
function makeTitle(titlePattern, value, k, values) {
if (typeof titlePattern === 'string') {
let testTitle = titlePattern
.replace('%k', k)
.replace('%K', k + 1)
.replace('%N', values.length)
if (Array.isArray(value)) {
// apply any positional arguments
// https://github.com/bahmutov/cypress-each/issues/50
testTitle = testTitle
.replace('%0', value[0])
.replace('%1', value[1])
.replace('%2', value[2])
return formatTitle(testTitle, ...value)
} else {
return formatTitle(testTitle, value)
}
} else if (typeof titlePattern === 'function') {
return titlePattern(value, k, values)
}
throw new Error('titlePattern must be a string or function')
}
if (!it.each) {
it.each = function (values, totalChunks, chunkIndex) {
if (typeof values === 'number') {
// the user wants to repeat the same test N times
if (values < 1) {
throw new Error('Number of test repetitions must be >= 1')
}
values = Cypress._.range(0, values)
}
if (typeof totalChunks === 'number') {
if (!Array.isArray(values)) {
throw new Error('cypress-each: values must be an array')
}
}
return function (titlePattern, testCallback) {
if (typeof totalChunks === 'number' && typeof chunkIndex === 'number') {
// split all items into N chunks and take just a single chunk
values = getChunk(values, totalChunks, chunkIndex)
} else if (
typeof totalChunks === 'number' &&
typeof chunkIndex === 'undefined'
) {
// take every Nth item
values = values.filter((_, k) => k % totalChunks === 0)
} else if (typeof totalChunks === 'function') {
// filter using the given predicate
values = values.filter(totalChunks)
}
if (Cypress._.isPlainObject(values)) {
testCallback = titlePattern
if (typeof testCallback !== 'function') {
throw new Error(
'When using a single test case object, cannot provide title pattern',
)
}
const pairs = Cypress._.toPairs(values)
pairs.forEach(function (pair) {
const [title, value] = pair
// define a test for each value
if (Array.isArray(value)) {
it(title, function itArrayCallback() {
return testCallback.apply(this, value)
})
} else {
it(title, function itCallback() {
return testCallback.call(this, value)
})
}
}, this)
} else if (Array.isArray(values)) {
values.forEach(function (value, k) {
const title = makeTitle(titlePattern, value, k, values)
if (!title) {
throw new Error(
`Could not compute the test title ${k} for value ${value}`,
)
}
// define a test for each value
if (Array.isArray(value)) {
it(title, function itArrayCallback() {
return testCallback.apply(this, value, k)
})
} else {
it(title, function itCallback() {
return testCallback.call(this, value, k)
})
}
}, this)
// returns the number of created tests
return values.length
} else {
console.error(values)
throw new Error(
'Do not know how to create tests from the values array / object. See DevTools console',
)
}
}
}
}
if (!describe.each) {
context.each = describe.each = function (values) {
if (typeof values === 'number') {
// the user wants to repeat the same suite N times
if (values < 1) {
throw new Error('Number of suite repetitions must be >= 1')
}
values = Cypress._.range(0, values)
}
if (!Array.isArray(values)) {
throw new Error('cypress-each: values must be an array')
}
if (typeof totalChunks === 'number' && typeof chunkIndex === 'number') {
// split all items into N chunks and take just a single chunk
values = getChunk(values, totalChunks, chunkIndex)
}
return function describeEach(titlePattern, testCallback) {
// define a test for each value
values.forEach((value, k) => {
const title = makeTitle(titlePattern, value, k, values)
if (!title) {
throw new Error(
`Could not compute the suite title ${k} for value ${value}`,
)
}
if (Array.isArray(value)) {
// const title = formatTitle(testTitle, ...value)
describe(title, testCallback.bind(null, ...value))
} else {
// const title = formatTitle(testTitle, value)
describe(title, testCallback.bind(null, value))
}
})
// returns the number of created suites
return values.length
}
}
}
module.exports = { formatTitle, makeTitle, getChunk }