Notes/spec-es6/mini_test.ts

80 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

2024-06-09 12:16:09 +02:00
export function describe(name: string, cb: () => any) {
2020-06-03 17:11:03 +02:00
console.log(`Running ${name}`);
cb();
}
2024-06-09 12:16:09 +02:00
export async function it(name: string, cb: () => any) {
2020-06-03 17:11:03 +02:00
console.log(` Running ${name}`);
cb();
}
let errorCount = 0;
2024-06-09 12:16:09 +02:00
export function expect(val: any) {
2020-06-03 17:11:03 +02:00
return {
2024-06-09 12:16:09 +02:00
toEqual: (comparedVal: any) => {
2020-06-03 17:11:03 +02:00
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 00:04:57 +02:00
},
2020-07-13 23:27:23 +02:00
toBeTruthy: () => {
if (!val) {
console.trace("toBeTruthy failed.");
console.error(`expected: truthy value`);
console.error(`got: ${val}`);
errorCount++;
}
},
2020-06-04 00:04:57 +02:00
toBeFalsy: () => {
if (!!val) {
console.trace("toBeFalsy failed.");
console.error(`expected: null, false, undefined, 0 or empty string`);
console.error(`got: ${val}`);
errorCount++;
}
},
2024-06-09 12:16:09 +02:00
toThrow: (errorMessage: any) => {
2020-06-04 00:04:57 +02:00
try {
val();
2025-01-09 18:07:02 +02:00
} catch (e: any) {
2020-06-04 00:04:57 +02:00
if (e.message !== errorMessage) {
console.trace("toThrow caught exception, but messages differ");
console.error(`expected: ${errorMessage}`);
console.error(`got: ${e.message}`);
2020-07-09 23:59:27 +02:00
console.error(`${e.stack}`);
2020-06-04 00:04:57 +02:00
errorCount++;
}
return;
}
console.trace("toThrow did not catch any exception.");
console.error(`expected: ${errorMessage}`);
console.error(`got: [none]`);
errorCount++;
2020-06-03 17:11:03 +02:00
}
2025-01-09 18:07:02 +02:00
};
2020-06-03 17:11:03 +02:00
}
2020-06-03 17:28:57 +02:00
export function execute() {
console.log("");
2020-06-03 17:11:03 +02:00
2020-06-03 17:28:57 +02:00
if (errorCount) {
console.log(`!!!${errorCount} tests failed!!!`);
2025-01-09 18:07:02 +02:00
} else {
2020-06-03 17:28:57 +02:00
console.log("All tests passed!");
}
2020-06-03 17:11:03 +02:00
}