trilium/spec-es6/mini_test.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-06-03 23:28:57 +08:00
export function describe(name, cb) {
2020-06-03 23:11:03 +08:00
console.log(`Running ${name}`);
cb();
}
2020-06-03 23:28:57 +08:00
export async function it(name, cb) {
2020-06-03 23:11:03 +08:00
console.log(` Running ${name}`);
cb();
}
let errorCount = 0;
2020-06-03 23:28:57 +08:00
export function expect(val) {
2020-06-03 23:11:03 +08:00
return {
toEqual: comparedVal => {
const jsonVal = JSON.stringify(val);
const comparedJsonVal = JSON.stringify(comparedVal);
if (jsonVal !== comparedJsonVal) {
console.trace("toEqual check failed.");
console.error(`expected: ${comparedJsonVal}`);
console.error(`got: ${jsonVal}`);
errorCount++;
}
2020-06-04 06:04:57 +08:00
},
2020-07-14 05:27:23 +08:00
toBeTruthy: () => {
if (!val) {
console.trace("toBeTruthy failed.");
console.error(`expected: truthy value`);
console.error(`got: ${val}`);
errorCount++;
}
},
2020-06-04 06:04:57 +08:00
toBeFalsy: () => {
if (!!val) {
console.trace("toBeFalsy failed.");
console.error(`expected: null, false, undefined, 0 or empty string`);
console.error(`got: ${val}`);
errorCount++;
}
},
toThrow: errorMessage => {
try {
val();
}
catch (e) {
if (e.message !== errorMessage) {
console.trace("toThrow caught exception, but messages differ");
console.error(`expected: ${errorMessage}`);
console.error(`got: ${e.message}`);
2020-07-10 05:59:27 +08:00
console.error(`${e.stack}`);
2020-06-04 06:04:57 +08:00
errorCount++;
}
return;
}
console.trace("toThrow did not catch any exception.");
console.error(`expected: ${errorMessage}`);
console.error(`got: [none]`);
errorCount++;
2020-06-03 23:11:03 +08:00
}
}
}
2020-06-03 23:28:57 +08:00
export function execute() {
console.log("");
2020-06-03 23:11:03 +08:00
2020-06-03 23:28:57 +08:00
if (errorCount) {
console.log(`!!!${errorCount} tests failed!!!`);
}
else {
console.log("All tests passed!");
}
2020-06-03 23:11:03 +08:00
}