tests.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. var hasSymbols = require('has-symbols')();
  3. module.exports = function runTests(promisify, t) {
  4. t.equal(typeof promisify, 'function', 'promisify is a function');
  5. t['throws'](
  6. function () { promisify(null); },
  7. TypeError,
  8. 'throws on non-functions'
  9. );
  10. var yes = function () {
  11. var cb = arguments[arguments.length - 1];
  12. cb(null, Array.prototype.slice.call(arguments, 0, -1));
  13. };
  14. var no = function () {
  15. var cb = arguments[arguments.length - 1];
  16. cb(Array.prototype.slice.call(arguments, 0, -1));
  17. };
  18. var pYes = promisify(yes);
  19. var pNo = promisify(no);
  20. t.equal(typeof pYes, 'function', 'pYes is a function');
  21. t.equal(typeof pNo, 'function', 'pNo is a function');
  22. t.test('pYes is properly promisified', { skip: typeof Promise !== 'function' }, function (st) {
  23. st.plan(1);
  24. var p = pYes(1, 2, 3);
  25. return p.then(function (result) {
  26. st.deepEqual(result, [1, 2, 3], 'fulfillment: arguments are preserved');
  27. });
  28. });
  29. t.test('pNo is properly promisified', { skip: typeof Promise !== 'function' }, function (st) {
  30. st.plan(1);
  31. var p = pNo(1, 2, 3);
  32. return p.then(null, function (args) {
  33. st.deepEqual(args, [1, 2, 3], 'rejection: arguments are preserved');
  34. });
  35. });
  36. t.test('custom symbol', { skip: !hasSymbols }, function (st) {
  37. st.equal(Symbol.keyFor(promisify.custom), 'nodejs.util.promisify.custom', 'is a global symbol with the right key');
  38. // eslint-disable-next-line no-restricted-properties
  39. st.equal(Symbol['for']('nodejs.util.promisify.custom'), promisify.custom, 'is the global symbol');
  40. st.end();
  41. });
  42. };