-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.js
203 lines (158 loc) · 4.51 KB
/
mod.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { ProgrammerError } from './lib/errors.js';
import EventEmitter from './lib/event-emitter.js';
import { createDescribeBlock } from './lib/describe-block.js';
import {
isFunction,
isNonEmptyString,
readDirectoryEntries
} from './lib/utils.js';
const emitter = new EventEmitter();
let options = {
timeout: 3000,
};
let rootBlocks = [];
export function on(...args) {
emitter.on(...args);
}
export function once(...args) {
emitter.once(...args);
}
export function off(...args) {
emitter.off(...args);
}
export function clear() {
options = {
timeout: 3000,
};
rootBlocks = [];
}
export function describe(name, fn, opts = {}) {
if (!isNonEmptyString(name)) {
throw new ProgrammerError(
'First argument to describe() must be a string',
null,
describe
);
}
// If only a name argument is given, this block is considered to be disabled.
const isDisabled = arguments.length < 2;
if (!isDisabled && !isFunction(fn)) {
throw new ProgrammerError(
'Second argument to describe() must be a function',
null,
describe
);
}
const blockType = isDisabled ? 'xdescribe' : 'describe';
const newBlocks = {
beforeBlocks: [],
testBlocks: [],
childBlocks: [],
afterBlocks: [],
};
const newDescribeBlock = createDescribeBlock({
emitter,
// Update the global options.
options: Object.assign({}, options, opts || {}),
ancestorNames: [ name ],
blocks: newBlocks,
});
rootBlocks.push({
blockType,
fn,
name,
ancestorNames: [],
blocks: newBlocks,
});
if (!isDisabled) {
fn(newDescribeBlock);
}
}
export async function run(opts = {}) {
Object.assign(options, opts || {});
async function walkBlock(_block) {
const {
beforeBlocks,
testBlocks,
childBlocks,
afterBlocks,
} = _block.blocks;
let subject;
let failure;
emitter.emit('describeBlockStart', { block: _block });
for (const block of beforeBlocks) {
if (failure) {
break;
}
const start = Date.now();
let error;
try {
if (isFunction(block.blockRunner)) {
subject = await block.blockRunner();
}
} catch (err) {
error = err;
failure = err;
}
const end = Date.now();
emitter.emit('beforeBlockEnd', { block, start, end, error });
}
for (const block of testBlocks) {
if (failure) {
break;
}
let error;
if (isFunction(block.fn)) {
try {
block.fn(subject);
} catch (err) {
error = err;
}
}
emitter.emit('testBlockEnd', { block, error });
}
for (const block of childBlocks) {
await walkBlock(block);
}
for (const block of afterBlocks) {
const start = Date.now();
let error;
if (isFunction(block.blockRunner)) {
try {
await block.blockRunner();
} catch (err) {
error = err;
}
}
const end = Date.now();
emitter.emit('afterBlockEnd', { block, start, end, error });
}
}
// Uncomment to inspect:
// console.log(JSON.stringify(rootBlocks[0], null, 2));
for (const block of rootBlocks) {
await walkBlock(block);
}
}
export function loadFromFiles(url, matchPattern = /test.js$/) {
async function walkDirectoryLevel(directoryUrl) {
const entries = await readDirectoryEntries(directoryUrl);
const files = [];
const directories = [];
for (const pathObject of entries) {
if (pathObject.isDirectory) {
directories.push(pathObject.url);
}
if (pathObject.isFile && matchPattern.test(pathObject.url.pathname)) {
files.push(pathObject.url);
}
}
for (const dirUrl of directories) {
await walkDirectoryLevel(dirUrl);
}
for (const fileUrl of files) {
await import(fileUrl);
}
}
return walkDirectoryLevel(url);
}