run.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Ultra simple test runner with TAP output.
  2. var inspect = require('util').inspect;
  3. var defer = require('../lib/defer.js');
  4. var log = console.log;
  5. console.log = function () {
  6. var args = [].slice.call(arguments).map(function (arg) {
  7. return inspect(arg, {colors:true});
  8. });
  9. log(args.join(" ").split("\n").map(function (line) {
  10. return "# " + line;
  11. }).join("\n"));
  12. };
  13. module.exports = function (tests) {
  14. var timeout;
  15. var test;
  16. var index = 0;
  17. log("1.." + (tests.length));
  18. next();
  19. function next(err) {
  20. if (timeout) clearTimeout(timeout);
  21. if (index) {
  22. if (err) {
  23. log(err.stack.split("\n").map(function (line) {
  24. return "# " + line;
  25. }).join("\n"));
  26. log("not ok " + index + " - " + test.name);
  27. }
  28. else {
  29. log("ok " + index + " - " + test.name);
  30. }
  31. }
  32. test = tests[index++];
  33. if (!test) return;
  34. timeout = setTimeout(onTimeout, 1000);
  35. try {
  36. if (test.length) test(next);
  37. else test();
  38. }
  39. catch (err) { return next(err); }
  40. if (!test.length) defer(next);
  41. }
  42. function onTimeout() {
  43. next(new Error("Test timeout"));
  44. }
  45. };