promise.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict';
  2. require('../lib/chalk').set();
  3. require('../lib/worker/options').set({color: false});
  4. const Promise = require('bluebird');
  5. const test = require('tap').test;
  6. const Test = require('../lib/test');
  7. function ava(fn) {
  8. return new Test({
  9. contextRef: null,
  10. failWithoutAssertions: true,
  11. fn,
  12. metadata: {type: 'test', callback: false},
  13. title: '[anonymous]'
  14. });
  15. }
  16. ava.cb = function (fn) {
  17. return new Test({
  18. contextRef: null,
  19. failWithoutAssertions: true,
  20. fn,
  21. metadata: {type: 'test', callback: true},
  22. title: '[anonymous]'
  23. });
  24. };
  25. function pass() {
  26. return new Promise(resolve => {
  27. setImmediate(resolve);
  28. });
  29. }
  30. function fail() {
  31. return new Promise((resolve, reject) => {
  32. setImmediate(() => {
  33. reject(new Error('unicorn'));
  34. });
  35. });
  36. }
  37. test('returning a promise from a legacy async fn is an error', t => {
  38. return ava.cb(a => {
  39. a.plan(1);
  40. return Promise.resolve(true).then(() => {
  41. a.pass();
  42. a.end();
  43. });
  44. }).run().then(result => {
  45. t.is(result.passed, false);
  46. t.match(result.error.message, /Do not return promises/);
  47. });
  48. });
  49. test('assertion plan is tested after returned promise resolves', t => {
  50. const start = Date.now();
  51. const instance = ava(a => {
  52. a.plan(2);
  53. const defer = Promise.defer();
  54. setTimeout(() => {
  55. defer.resolve();
  56. }, 500);
  57. a.pass();
  58. a.pass();
  59. return defer.promise;
  60. });
  61. return instance.run().then(result => {
  62. t.is(result.passed, true);
  63. t.is(instance.planCount, 2);
  64. t.is(instance.assertCount, 2);
  65. t.true(Date.now() - start >= 500);
  66. });
  67. });
  68. test('missing assertion will fail the test', t => {
  69. return ava(a => {
  70. a.plan(2);
  71. const defer = Promise.defer();
  72. setTimeout(() => {
  73. a.pass();
  74. defer.resolve();
  75. }, 200);
  76. return defer.promise;
  77. }).run().then(result => {
  78. t.is(result.passed, false);
  79. t.is(result.error.assertion, 'plan');
  80. });
  81. });
  82. test('extra assertion will fail the test', t => {
  83. return ava(a => {
  84. a.plan(2);
  85. const defer = Promise.defer();
  86. setTimeout(() => {
  87. a.pass();
  88. a.pass();
  89. }, 200);
  90. setTimeout(() => {
  91. a.pass();
  92. defer.resolve();
  93. }, 500);
  94. return defer.promise;
  95. }).run().then(result => {
  96. t.is(result.passed, false);
  97. t.is(result.error.assertion, 'plan');
  98. });
  99. });
  100. test('assert pass', t => {
  101. const instance = ava(a => {
  102. return pass().then(() => {
  103. a.pass();
  104. });
  105. });
  106. return instance.run().then(result => {
  107. t.is(result.passed, true);
  108. t.is(instance.assertCount, 1);
  109. });
  110. });
  111. test('assert fail', t => {
  112. return ava(a => {
  113. return pass().then(() => {
  114. a.fail();
  115. });
  116. }).run().then(result => {
  117. t.is(result.passed, false);
  118. t.is(result.error.name, 'AssertionError');
  119. });
  120. });
  121. test('reject', t => {
  122. return ava(a => {
  123. return fail().then(() => {
  124. a.pass();
  125. });
  126. }).run().then(result => {
  127. t.is(result.passed, false);
  128. t.is(result.error.name, 'AssertionError');
  129. t.is(result.error.message, 'Rejected promise returned by test');
  130. t.is(result.error.values.length, 1);
  131. t.is(result.error.values[0].label, 'Rejected promise returned by test. Reason:');
  132. t.match(result.error.values[0].formatted, /.*Error.*\n.*message: 'unicorn'/);
  133. });
  134. });
  135. test('reject with non-Error', t => {
  136. return ava(() => {
  137. return Promise.reject('failure'); // eslint-disable-line prefer-promise-reject-errors
  138. }).run().then(result => {
  139. t.is(result.passed, false);
  140. t.is(result.error.name, 'AssertionError');
  141. t.is(result.error.message, 'Rejected promise returned by test');
  142. t.is(result.error.values.length, 1);
  143. t.is(result.error.values[0].label, 'Rejected promise returned by test. Reason:');
  144. t.match(result.error.values[0].formatted, /failure/);
  145. });
  146. });