-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
85 lines (75 loc) · 2.01 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
const path = require('path');
const glob = require('glob');
const through2 = require('through2');
const cucumber = require('cucumber');
const featureRegExp = /\.feature$/i;
function clearFileCache(filePath) {
delete require.cache[require.resolve(path.resolve(filePath))];
}
function getFiles(patterns) {
const output = new Set();
[].concat(patterns).forEach((p) => {
glob.sync(p).forEach((f) => {
output.add(f);
});
});
return [...output];
}
module.exports = function ({
files = [],
runOptions = [],
emitErrors = true,
support = [],
steps = [],
format,
tags,
compiler,
} = {}) {
[].concat(files)
.concat(getFiles(support))
.concat(getFiles(steps))
.forEach((file) => {
clearFileCache(file);
runOptions.push('-r');
runOptions.push(file);
});
if (format) {
[].concat(format)
.forEach((f) => {
runOptions.push('--format');
runOptions.push(f);
});
}
if (tags) {
runOptions.push('--tags');
runOptions.push(tags);
}
if (compiler) {
runOptions.push('--compiler');
runOptions.push(compiler);
}
const features = [];
function collect(file, enc, cb) {
const filename = file.path;
if (featureRegExp.test(filename)) {
features.push(filename);
}
cb();
}
function run() {
const cli = new cucumber.Cli({
argv: ['node', 'cucumber-js'].concat(runOptions).concat(features),
cwd: process.cwd(),
stdout: process.stdout,
});
cli.run().then((result) => {
const success = typeof(result) === 'object' ? result.success : result
if (success || !emitErrors) {
this.emit('end');
} else {
this.emit('error', new Error('Cucumber tests failed!'));
}
});
}
return through2.obj(collect, run);
};