mini_test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. export function describe(name, cb) {
  2. console.log(`Running ${name}`);
  3. cb();
  4. }
  5. export async function it(name, cb) {
  6. console.log(` Running ${name}`);
  7. cb();
  8. }
  9. let errorCount = 0;
  10. export function expect(val) {
  11. return {
  12. toEqual: comparedVal => {
  13. const jsonVal = JSON.stringify(val);
  14. const comparedJsonVal = JSON.stringify(comparedVal);
  15. if (jsonVal !== comparedJsonVal) {
  16. console.trace("toEqual check failed.");
  17. console.error(`expected: ${comparedJsonVal}`);
  18. console.error(`got: ${jsonVal}`);
  19. errorCount++;
  20. }
  21. },
  22. toBeTruthy: () => {
  23. if (!val) {
  24. console.trace("toBeTruthy failed.");
  25. console.error(`expected: truthy value`);
  26. console.error(`got: ${val}`);
  27. errorCount++;
  28. }
  29. },
  30. toBeFalsy: () => {
  31. if (!!val) {
  32. console.trace("toBeFalsy failed.");
  33. console.error(`expected: null, false, undefined, 0 or empty string`);
  34. console.error(`got: ${val}`);
  35. errorCount++;
  36. }
  37. },
  38. toThrow: errorMessage => {
  39. try {
  40. val();
  41. }
  42. catch (e) {
  43. if (e.message !== errorMessage) {
  44. console.trace("toThrow caught exception, but messages differ");
  45. console.error(`expected: ${errorMessage}`);
  46. console.error(`got: ${e.message}`);
  47. console.error(`${e.stack}`);
  48. errorCount++;
  49. }
  50. return;
  51. }
  52. console.trace("toThrow did not catch any exception.");
  53. console.error(`expected: ${errorMessage}`);
  54. console.error(`got: [none]`);
  55. errorCount++;
  56. }
  57. }
  58. }
  59. export function execute() {
  60. console.log("");
  61. if (errorCount) {
  62. console.log(`!!!${errorCount} tests failed!!!`);
  63. }
  64. else {
  65. console.log("All tests passed!");
  66. }
  67. }