-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
113 lines (95 loc) · 2.39 KB
/
test.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
(function() {
// test container
var test = self;
test.test = test;
// properties
test.el = document.getElementById('test');
test.bootTimeout = null;
test.groups = [];
test.group = null;
test.options = {
bail: false
};
test.results = {
total: 0,
passed: 0,
failed: 0
};
// methods
test.print = function print(msg) {
test.el.textContent += msg;
};
test.boot = function boot() {
for (var x = 0; x < test.groups.length; x++) {
var fail = false;
var group = test.groups[x];
test.print(group.msg + ':\n');
try {
group.before();
} catch(e) {
test.print('!FAIL!\n');
test.print(e.message + '\n' + e.stack + '\n');
test.results.total += group.cases.length;
test.results.failed += group.cases.length;
fail = true;
if (test.options.bail) break;
}
for (var y = 0; y < group.cases.length; y++) {
var c = group.cases[y];
test.print(' ' + c.msg + ' -- ');
test.results.total++;
try {
group.beforeEach();
c.fn();
group.afterEach();
test.print('ok\n');
test.results.passed++;
} catch(e) {
test.print('!FAIL!\n');
test.print(e.message + '\n' + e.stack + '\n');
test.results.failed++;
fail = true;
if (test.options.bail) break;
}
}
if (fail && test.options.bail) break;
try {
group.after();
} catch(e) {
test.print('!FAIL!\n');
test.print(e.message + '\n' + e.stack + '\n');
fail = true;
if (test.options.bail) break;
}
test.print('\n');
}
test.print(test.results.total + ' tests complete\n');
test.print(test.results.passed + ' passed\n');
test.print(test.results.failed + ' failed\n');
};
test.describe = function describe(msg, fn) {
clearTimeout(test.bootTimeout);
test.bootTimeout = setTimeout(test.boot, 0);
test.groups.push(test.group = {
msg: msg,
fn: fn,
cases: [],
before: noop,
beforeEach: noop,
after: noop,
afterEach: noop
});
test.group.fn();
};
test.it = function it(msg, fn) {
test.group.cases.push({
msg: msg,
fn: fn
});
};
test.before = function before(fn) { test.group.before = fn };
test.beforeEach = function beforeEach(fn) { test.group.beforeEach = fn };
test.after = function after(fn) { test.group.after = fn };
test.afterEach = function afterEach(fn) { test.group.afterEach = fn };
function noop() {/* noop */}
})();