-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb-test-runner.setup.js
37 lines (35 loc) · 1.05 KB
/
web-test-runner.setup.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
/**
* Adds a simple `it.each` function to the global scope.
*/
it.each = ([header], ...values) => {
// read header from table
const headers = header.split('|').map((h) => h.trim());
const rows = [];
// gather data from table
for (let i = 0; i < values.length; i += headers.length) {
const row = values.slice(i, i + headers.length);
const params = {};
for (let j = 0; j < headers.length; j++) {
params[headers[j]] = row[j];
}
rows.push(params);
}
// provide the test function with the data
return (title, fn) => {
for (const row of rows) {
// prepare the title
const rowTitle = title.replace(/\$(\w+)/g, (_, key) => row[key.trim()] ?? _);
// call the test function
it(rowTitle, (done) => {
// call the given test function with the row data
const result = fn.call(this, row, done);
// do we have a promise?
if (result && result.then) {
result.then(done).catch(done);
}
// just call it synchronously
else done(result);
});
}
};
};