-
Notifications
You must be signed in to change notification settings - Fork 2
/
syntax.js
111 lines (93 loc) · 2.16 KB
/
syntax.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
const Suite = require('./Suite');
const Test = require('./Test');
const Hook = require('./Hook');
const context = new Array();
function describe(name, fn, options) {
return _describe(name, fn, { ...options });
}
function xdescribe(name, fn, options) {
return _describe(name, fn, { ...options, skip: true });
}
function odescribe(name, fn, options) {
return _describe(name, fn, { ...options, exclusive: true });
}
function _describe(name, fn, options) {
const suite = new Suite(name, options);
context.push(suite);
fn();
context.pop();
publish(suite);
return suite;
}
function it(name, fn, options) {
return _it(name, fn, { ...options });
}
function xit(name, fn, options) {
return _it(name, fn, { ...options, skip: true });
}
function oit(name, fn, options) {
return _it(name, fn, { ...options, exclusive: true });
}
function _it(name, fn, options) {
const test = new Test(name, fn, options);
publish(test);
return test;
}
function before(...args) {
const hook = createHook('before', args);
currentSuite().before(hook);
}
function beforeEach(...args) {
const hook = createHook('beforeEach', args);
currentSuite().beforeEach(hook);
}
function afterEach(...args) {
const hook = createHook('afterEach', args);
currentSuite().afterEach(hook);
}
function after(...args) {
const hook = createHook('after', args);
currentSuite().after(hook);
}
function createHook(defaultName, args) {
const { name, fn, options } = { name: defaultName, ...getHookParameters(args) };
return new Hook(name, fn, options);
}
function getHookParameters(args) {
return typeof args[0] === 'string'
? {
name: args[0],
fn: args[1],
options: args[2],
}
: {
fn: args[0],
options: args[1],
};
}
function include(...testables) {
currentSuite().add(testables);
}
function currentSuite() {
return context[context.length - 1];
}
function publish(testable) {
if (context.length > 0) {
currentSuite().add(testable);
} else {
process.emit('zunit:testable', testable);
}
}
module.exports = {
describe,
xdescribe,
odescribe,
it,
xit,
oit,
before,
beforeEach,
after,
afterEach,
include,
};