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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Define Mochia's helpers on the given scope.
*/
(() => {
/**
* The context of each test suite.
*/
class Context {
static #stack = [];
static current() {
return Context.#stack.at(-1);
}
static push(ctx) {
Context.#stack.push(ctx);
}
static pop() {
Context.#stack.pop();
}
constructor() {
this.description = [];
this.beforeEach = [];
this.afterEach = [];
}
clone() {
const newCtx = new Context();
newCtx.description.push(...this.description);
newCtx.beforeEach.push(...this.beforeEach);
newCtx.afterEach.push(...this.afterEach);
return newCtx;
}
}
Context.push(new Context());
let _testScope = null;
/**
* @typedef {void|Promise<void>} MaybePromise
*
* Either undefined or a Promise that resolves to undefined.
*/
const MochiaImpl = {
/**
* Describe a new test suite, which is a scoped environment for running setup
* and teardown.
*
* @param {string} desc
* A description of the test suite.
*
* @param {function(): MaybePromise} suite
* The test suite
*/
async describe(desc, suite) {
const ctx = Context.current().clone();
ctx.description.push(desc);
Context.push(ctx);
const p = suite();
if (p?.then) {
await p;
}
Context.pop();
},
/**
* Register a setup funciton to run before each test.
*
* Multiple functions can be registered with `beforeEach` and they will be run
* in order before each test in the suite and the suites nested inside of it.
*
* @param {function(): MaybePromise} setupFn
* The setup function. If this function returns a `Promise` it will be
* awaited.
*/
beforeEach(setupFn) {
Context.current().beforeEach.push(setupFn);
},
/**
* Register a tear down function to run at the end of each test.
*
* Multiple functions can be registered with `afterEach` and they will run in
* reverse order after each test in the suite and the suites nested inside of it.
*
* @param {function(): MaybePromise} tearDownFn
* The tear down function. If this function returns a `Promise` it will
* be awaited.
*/
afterEach(tearDownFn) {
Context.current().afterEach.push(tearDownFn);
},
/**
* Register a test function.
*
* The test will be registered via `add_task`. Each setup function registered
* before this function call will be called in order before the actual test
* and each teardown function before this function will be called in reverse
* order after the actual test.
*
* @param {string} desc
* A description of the test. This is logged at the start of the test.
*
* @param {function(): MaybePromise} testFn
* The test function. If this function returns a `Promise` it will be
* awaited.
*
* @returns {any}
* The result of calling `add_task` with the wrapped function.
*/
it(desc, testFn) {
return _testScope.add_task(MochiaImpl.wrap(desc, testFn));
},
/**
* Register a test that will be the only test run.
*
* @param {string} desc
* A description of the test. This is logged at the start of the test.
*
* @param {function(): MaybePromise} testFn
* The test function. If this function returns a `Promise` it will be
* awaited.
*/
only(desc, testFn) {
MochiaImpl.it(desc, testFn).only();
},
/**
* Register a test that will be skipped.
*
* @param {string} desc
* A description of the test. This is logged at the start of the test.
*
* @param {function(): MaybePromise} testFn
* The test function. If this function returns a `Promise` it will be
* awaited.
*/
skip(desc, testFn) {
MochiaImpl.it(desc, testFn).skip();
},
/**
* Register a test that will be skipped if the provided predicate evaluates to
* a truthy value.
*
* @param {string} desc
* A description of the test. This is logged at the start of the test.
*
* @param {function(): boolean} skipFn
* A predicate that will be called by the test harness to determine
* whether or not the test should be skipped.
*
* @param {function(): MaybePromise} testFn
* The test function. If this function returns a `Promise` it will be
* awaited.
*
* @returns {any}
* The result of calling `add_task` with the wrapped function.
*/
skipIf(desc, skipFn, testFn) {
return _testScope.add_task(
{ skip_if: skipFn },
MochiaImpl.wrap(desc, testFn)
);
},
/**
* Wrap `fn` so that all the setup functions declared with `beforeEach` are
* called before it and all the teardown functions declared with `afterEach`
* are called after.
*
* @param {string} desc
* A description of the test.
*
* @param {function(): MaybePromise} fn
* The function to wrap.
*
* @returns {function(): Promise<void>}
* The wrapped function.
*/
wrap(desc, fn) {
const ctx = Context.current().clone();
const name = [...ctx.description, desc].join(" / ");
// This is a hack to give the function an implicit name.
const wrapper = {
[name]: async () => {
_testScope.info(name);
for (const before of ctx.beforeEach) {
const p = before();
if (p?.then) {
await p;
}
}
{
const p = fn();
if (p?.then) {
await p;
}
}
for (let i = ctx.afterEach.length - 1; i >= 0; i--) {
const after = ctx.afterEach[i];
const p = after();
if (p?.then) {
await p;
}
}
},
};
return wrapper[name];
},
};
Object.defineProperties(MochiaImpl.it, {
only: {
configurable: false,
value: MochiaImpl.only,
},
skip: {
configurable: false,
value: MochiaImpl.skip,
},
skipIf: {
configurable: false,
value: MochiaImpl.skipIf,
},
});
_testScope = this;
Object.assign(_testScope, {
describe: MochiaImpl.describe,
beforeEach: MochiaImpl.beforeEach,
afterEach: MochiaImpl.afterEach,
it: MochiaImpl.it,
});
})();
|