Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

end-as-callback.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var tap = require("tap");
  2. var forEach = require("for-each");
  3. var tape = require("../");
  4. var concat = require('concat-stream');
  5. tap.test("tape assert.end as callback", function (tt) {
  6. var test = tape.createHarness({ exit: false });
  7. test.createStream().pipe(concat(function (rows) {
  8. tt.equal(rows.toString('utf8'), [
  9. 'TAP version 13',
  10. '# do a task and write',
  11. 'ok 1 null',
  12. 'ok 2 should be equal',
  13. '# do a task and write fail',
  14. 'ok 3 null',
  15. 'ok 4 should be equal',
  16. 'not ok 5 Error: fail',
  17. getStackTrace(rows), // tap error stack
  18. '',
  19. '1..5',
  20. '# tests 5',
  21. '# pass 4',
  22. '# fail 1'
  23. ].join('\n') + '\n');
  24. tt.end();
  25. }));
  26. test("do a task and write", function (assert) {
  27. fakeAsyncTask("foo", function (err, value) {
  28. assert.ifError(err);
  29. assert.equal(value, "taskfoo");
  30. fakeAsyncWrite("bar", assert.end);
  31. });
  32. });
  33. test("do a task and write fail", function (assert) {
  34. fakeAsyncTask("bar", function (err, value) {
  35. assert.ifError(err);
  36. assert.equal(value, "taskbar");
  37. fakeAsyncWriteFail("baz", assert.end);
  38. });
  39. });
  40. });
  41. function fakeAsyncTask(name, cb) {
  42. cb(null, "task" + name);
  43. }
  44. function fakeAsyncWrite(name, cb) {
  45. cb(null);
  46. }
  47. function fakeAsyncWriteFail(name, cb) {
  48. cb(new Error("fail"));
  49. }
  50. /**
  51. * extract the stack trace for the failed test.
  52. * this will change dependent on the environment
  53. * so no point hard-coding it in the test assertion
  54. * see: https://git.io/v6hGG for example
  55. * @param String rows - the tap output lines
  56. * @returns String stacktrace - just the error stack part
  57. */
  58. function getStackTrace(rows) {
  59. var stacktrace = ' ---\n';
  60. var extract = false;
  61. forEach(rows.toString('utf8').split('\n'), function (row) {
  62. if (!extract) {
  63. if (row.indexOf('---') > -1) { // start of stack trace
  64. extract = true;
  65. }
  66. } else {
  67. if (row.indexOf('...') > -1) { // end of stack trace
  68. extract = false;
  69. stacktrace += ' ...';
  70. } else {
  71. stacktrace += row + '\n';
  72. }
  73. }
  74. });
  75. // console.log(stacktrace);
  76. return stacktrace;
  77. }