forked from nboxhallburnett/mocha-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (90 loc) · 2.36 KB
/
index.js
File metadata and controls
100 lines (90 loc) · 2.36 KB
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
/**
* The following code is for adding mocha filters, these filters
* will add a clean way to skip tests based on a predefined scenario.
*
* Add commonly used filters to the checks variable below, and any other
* test-specific filters should be defined in the applicable test using
* the addFilter() method.
*/
// Predefined additional filters
var checks = {
/**
* Only run the tests when not in prod
*/
ignore: function () {
return false;
}
},
originalChecks = Object.keys(checks);
/**
* Adding filters to mocha tests does not persist across test
* files, so any tests making use of the functions must call
* this function to set them.
*/
module.exports.setupMocha = function (_checks, skipOriginals) {
if (!_checks) {
_checks = checks;
} else if (!skipOriginals) {
for (var key in checks) {
_checks[key] = checks[key];
}
}
var functions = [describe, it, before, after, beforeEach, afterEach];
/**
* Process the checks
*/
function ext(func, notFailed) {
var check = this,
returnFunction = {};
/**
* Parse the provided functions
*/
var parseFunctions = function (_key) {
/**
* Function to return when the checks have been processed
*/
returnFunction[_key] = function () {
var passed = _checks[_key]();
if (arguments.length > 0) {
if (passed && notFailed) {
func.apply(null, arguments);
}
} else {
return ext(func, passed && notFailed);
}
};
};
for (var key in _checks) {
parseFunctions(key);
}
return returnFunction;
}
/**
* Populate the functions with the new checks
*/
var populateFunctions = function (_i, _extension) {
functions[_i][_extension] = extensions[_extension];
};
for (var i in functions) {
var extensions = ext(functions[i], true);
for (var extension in extensions) {
populateFunctions(i, extension);
}
}
};
/**
* Add a new filter to the mocha functions. The predefined filters
* cannot be overwritten, but user-defined ones can.
*
* @param name The name of the filter, which will be used in the test
* @param filter The function that will determine whether to run the test
*/
module.exports.addFilter = function (name, filter) {
if (originalChecks.indexOf(name) > -1) {
return false;
}
checks[name] = filter;
var obj;
module.exports.setupMocha((obj = {}, obj[name] = filter, obj), true);
};
module.exports.setupMocha();