-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmocha-cakes.js
160 lines (124 loc) · 4.13 KB
/
mocha-cakes.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
'use strict';
var Mocha = require('mocha'),
Suite = require('mocha/lib/suite'),
Test = require('mocha/lib/test');
Mocha.interfaces['mocha-cakes-2'] = module.exports = mochaCakes;
function mochaCakes(suite) {
var suites = [suite];
function registerDescendantsHook(hookType, suiteType) {
var hookName = hookType + suiteType;
return function(name, fn) {
if (!suites[0][hookName]) suites[0][hookName] = [];
suites[0][hookName].push([name, fn]);
}
}
suite.on('pre-require', function (context, file, mocha) {
var common = require('mocha/lib/interfaces/common')(suites, context, mocha);
context.run = mocha.options.delay && common.runWithSuite(suite);
var wrapperCreator = createWrapper(file, suites, context, mocha);
var testTypeCreator = createTestType(file, suites, mocha);
context.after = common.after;
context.afterEach = common.afterEach;
context.before = common.before;
context.beforeEach = common.beforeEach;
context.afterEachFeature = registerDescendantsHook('afterEach', 'Feature');
context.afterEachScenario = registerDescendantsHook('afterEach', 'Scenario');
context.beforeEachFeature = registerDescendantsHook('beforeEach', 'Feature');
context.beforeEachScenario = registerDescendantsHook('beforeEach', 'Scenario');
context.Scenario = wrapperCreator('Scenario');
context.Feature = wrapperCreator('Feature');
context.describe = wrapperCreator('');
context.Given = testTypeCreator('Given');
context.When = testTypeCreator('When');
context.Then = testTypeCreator('Then');
context.And = testTypeCreator('And');
context.But = testTypeCreator('But');
context.it = testTypeCreator('');
context.xit = context.it.skip;
// lower-case aliases
context.scenario = context.Scenario;
context.feature = context.Feature;
context.given = context.Given;
context.when = context.When;
context.then = context.Then;
context.and = context.And;
context.but = context.But;
});
}
/**
* Helper functions
**/
function createTestType(file, suites, mocha) {
return function testTypeCreator(type) {
function testType(title, fn) {
var suite, test;
var testName = type ? type + ' ' + title : title;
suite = suites[0];
if (suite.pending) fn = null;
test = new Test(testName, fn);
test.file = file;
suite.addTest(test);
return test;
}
testType.skip = function skip(title) {
testType(title);
};
testType.only = function only(title, fn) {
var test = testType(title, fn);
mocha.grep(test.fullTitle());
};
return testType;
};
}
function createWrapper(file, suites, context, mocha) {
return function wrapperCreator(type) {
function createLabel(title) {
if (!type) return title;
return type + ': ' + title;
}
function wrapper(title, fn) {
var suite = Suite.create(suites[0], createLabel(title));
suite.file = file;
suites.unshift(suite);
fn.call(suite);
suites.shift();
applyRegisteredHooks(suite, type);
return suite;
}
wrapper.skip = function skip(title, fn) {
var suite = Suite.create(suites[0], createLabel(title));
suite.pending = true;
suites.unshift(suite);
fn.call(suite);
suites.shift();
};
wrapper.only = function only(title, fn) {
var suite = wrapper(title, fn);
mocha.grep(suite.fullTitle());
};
return wrapper;
};
}
function applyRegisteredHooks(suite, suiteType) {
getRegisteredHooks(suite, 'beforeEach', suiteType).forEach(function(hook) {
suite.beforeAll(hook[0], hook[1]);
});
getRegisteredHooks(suite, 'afterEach', suiteType).forEach(function(hook) {
suite.afterAll(hook[0], hook[1]);
});
}
function getRegisteredHooks(suite, hookType, suiteType) {
var ancestors = [];
var hooks = [];
var parent = suite.parent;
while (parent) {
ancestors.push(parent);
parent = parent.parent;
}
ancestors.forEach(function(ancestor) {
if (ancestor.hasOwnProperty(hookType + suiteType)) {
hooks = hooks.concat( ancestor[hookType + suiteType] || [] );
}
});
return hooks;
}