teardown.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. 'use strict';
  2. var tape = require('../');
  3. var tap = require('tap');
  4. var concat = require('concat-stream');
  5. var forEach = require('for-each');
  6. var v = require('es-value-fixtures');
  7. var inspect = require('object-inspect');
  8. var flatMap = require('array.prototype.flatmap');
  9. var stripFullStack = require('./common').stripFullStack;
  10. tap.test('teardowns', function (tt) {
  11. tt.plan(1);
  12. var test = tape.createHarness();
  13. test.createStream().pipe(concat(function (body) {
  14. tt.same(stripFullStack(body.toString('utf8')), [].concat(
  15. 'TAP version 13',
  16. '# success',
  17. 'ok 1 should be truthy',
  18. '# success teardown',
  19. '# success teardown 2',
  20. '# success (async)',
  21. 'ok 2 should be truthy',
  22. '# success (async) teardown',
  23. '# success (async) teardown 2',
  24. '# nested teardowns',
  25. '# nested success',
  26. 'ok 3 should be truthy',
  27. '# nested teardown (nested success level)',
  28. '# nested teardown (nested success level) 2',
  29. '# nested failure',
  30. 'not ok 4 nested failure!',
  31. ' ---',
  32. ' operator: fail',
  33. ' at: Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
  34. ' stack: |-',
  35. ' Error: nested failure!',
  36. ' [... stack stripped ...]',
  37. ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
  38. ' [... stack stripped ...]',
  39. ' ...',
  40. '# nested teardown (nested fail level)',
  41. '# nested teardown (nested fail level) 2',
  42. '# nested teardown (top level)',
  43. '# nested teardown (top level) 2',
  44. '# fail',
  45. 'not ok 5 failure!',
  46. ' ---',
  47. ' operator: fail',
  48. ' at: Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
  49. ' stack: |-',
  50. ' Error: failure!',
  51. ' [... stack stripped ...]',
  52. ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
  53. ' [... stack stripped ...]',
  54. ' ...',
  55. '# failure teardown',
  56. '# failure teardown 2',
  57. '# teardown errors do not stop the next teardown fn from running',
  58. 'ok 6 should be truthy',
  59. 'not ok 7 SyntaxError: teardown error!',
  60. ' ---',
  61. ' operator: fail',
  62. ' stack: |-',
  63. ' Error: SyntaxError: teardown error!',
  64. ' [... stack stripped ...]',
  65. ' ...',
  66. 'not ok 8 plan != count',
  67. ' ---',
  68. ' operator: fail',
  69. ' expected: 1',
  70. ' actual: 2',
  71. ' stack: |-',
  72. ' Error: plan != count',
  73. ' [... stack stripped ...]',
  74. ' ...',
  75. '# teardown runs after teardown error',
  76. '# teardown given non-function fails the test',
  77. 'ok 9 should be truthy',
  78. flatMap(v.nonFunctions, function (nonFunction, i) {
  79. var offset = 10;
  80. return [].concat(
  81. 'not ok ' + (offset + (i > 0 ? i + 1 : i)) + ' teardown: ' + inspect(nonFunction) + ' is not a function',
  82. ' ---',
  83. ' operator: fail',
  84. ' at: <anonymous> ($TEST/teardown.js:$LINE:$COL)',
  85. ' stack: |-',
  86. ' Error: teardown: ' + inspect(nonFunction) + ' is not a function',
  87. ' [... stack stripped ...]',
  88. ' at $TEST/teardown.js:$LINE:$COL',
  89. ' [... stack stripped ...]',
  90. ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
  91. ' [... stack stripped ...]',
  92. ' ...',
  93. i > 0 ? [] : [
  94. 'not ok ' + (offset + 1) + ' plan != count',
  95. ' ---',
  96. ' operator: fail',
  97. ' expected: 1',
  98. ' actual: 2',
  99. ' at: <anonymous> ($TEST/teardown.js:$LINE:$COL)',
  100. ' stack: |-',
  101. ' Error: plan != count',
  102. ' [... stack stripped ...]',
  103. ' at $TEST/teardown.js:$LINE:$COL',
  104. ' [... stack stripped ...]',
  105. ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
  106. ' [... stack stripped ...]',
  107. ' ...'
  108. ]
  109. );
  110. }),
  111. typeof Promise === 'function' ? [
  112. '# success (promise)',
  113. 'ok ' + (11 + v.nonFunctions.length) + ' should be truthy',
  114. '# success (promise) teardown: 1',
  115. '# success (promise) teardown: 2',
  116. '# success (promise) teardown: 3'
  117. ] : [
  118. '# SKIP success (promise)'
  119. ],
  120. [
  121. '',
  122. '1..' + ((typeof Promise === 'function' ? 1 : 0) + 10 + v.nonFunctions.length),
  123. '# tests ' + ((typeof Promise === 'function' ? 1 : 0) + 10 + v.nonFunctions.length),
  124. '# pass ' + ((typeof Promise === 'function' ? 1 : 0) + 5),
  125. '# fail ' + (5 + v.nonFunctions.length),
  126. ''
  127. ]
  128. ));
  129. }));
  130. test('success', function (t) {
  131. t.plan(1);
  132. t.teardown(function () {
  133. t.comment('success teardown');
  134. });
  135. t.teardown(function () {
  136. t.comment('success teardown 2');
  137. });
  138. t.ok('success!');
  139. });
  140. test('success (async)', function (t) {
  141. t.plan(1);
  142. t.teardown(function () {
  143. t.comment('success (async) teardown');
  144. });
  145. t.teardown(function () {
  146. t.comment('success (async) teardown 2');
  147. });
  148. setTimeout(function () {
  149. t.ok('success!');
  150. }, 10);
  151. });
  152. test('nested teardowns', function (t) {
  153. t.plan(2);
  154. t.teardown(function () {
  155. t.comment('nested teardown (top level)');
  156. });
  157. t.teardown(function () {
  158. t.comment('nested teardown (top level) 2');
  159. });
  160. t.test('nested success', function (st) {
  161. st.teardown(function () {
  162. st.comment('nested teardown (nested success level)');
  163. });
  164. st.teardown(function () {
  165. st.comment('nested teardown (nested success level) 2');
  166. });
  167. st.ok('nested success!');
  168. st.end();
  169. });
  170. t.test('nested failure', function (st) {
  171. st.plan(1);
  172. st.teardown(function () {
  173. st.comment('nested teardown (nested fail level)');
  174. });
  175. st.teardown(function () {
  176. st.comment('nested teardown (nested fail level) 2');
  177. });
  178. st.fail('nested failure!');
  179. });
  180. });
  181. test('fail', function (t) {
  182. t.plan(1);
  183. t.teardown(function () {
  184. t.comment('failure teardown');
  185. });
  186. t.teardown(function () {
  187. t.comment('failure teardown 2');
  188. });
  189. t.fail('failure!');
  190. });
  191. test('teardown errors do not stop the next teardown fn from running', function (t) {
  192. t.plan(1);
  193. t.ok('teardown error test');
  194. t.teardown(function () {
  195. throw new SyntaxError('teardown error!');
  196. });
  197. t.teardown(function () {
  198. t.comment('teardown runs after teardown error');
  199. });
  200. });
  201. test('teardown given non-function fails the test', function (t) {
  202. t.plan(1);
  203. t.ok('non-function test');
  204. forEach(v.nonFunctions, function (nonFunction) {
  205. t.teardown(nonFunction);
  206. });
  207. });
  208. test('success (promise)', { skip: typeof Promise !== 'function' }, function (t) {
  209. t.plan(1);
  210. t.teardown(function () {
  211. return new Promise(function (resolve) {
  212. t.comment('success (promise) teardown: 1');
  213. setTimeout(resolve, 10);
  214. }).then(function () {
  215. t.comment('success (promise) teardown: 2');
  216. });
  217. });
  218. t.teardown(function () {
  219. t.comment('success (promise) teardown: 3');
  220. });
  221. setTimeout(function () {
  222. t.ok('success!');
  223. }, 10);
  224. });
  225. });
  226. tap.test('teardown with promise', { skip: typeof Promise !== 'function', timeout: 1e3 }, function (tt) {
  227. tt.plan(2);
  228. tape('dummy test', function (t) {
  229. var resolved = false;
  230. t.teardown(function () {
  231. tt.pass('tape teardown');
  232. var p = Promise.resolve();
  233. p.then(function () {
  234. resolved = true;
  235. });
  236. return p;
  237. });
  238. t.on('end', function () {
  239. tt.is(resolved, true);
  240. });
  241. t.end();
  242. });
  243. });