index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. /* globals window */
  3. var test = require('tape');
  4. var isAsyncFunction = require('../index');
  5. var generatorFuncs = require('make-generator-function')();
  6. var asyncFuncs = require('make-async-function').list();
  7. var hasToStringTag = require('has-tostringtag/shams')();
  8. var forEach = require('for-each');
  9. test('returns false for non-functions', function (t) {
  10. var nonFuncs = [
  11. true,
  12. false,
  13. null,
  14. undefined,
  15. {},
  16. [],
  17. /a/g,
  18. 'string',
  19. 42,
  20. new Date()
  21. ];
  22. t.plan(nonFuncs.length);
  23. forEach(nonFuncs, function (nonFunc) {
  24. t.notOk(isAsyncFunction(nonFunc), nonFunc + ' is not a function');
  25. });
  26. t.end();
  27. });
  28. test('returns false for non-async functions', function (t) {
  29. var func = function () {};
  30. t.notOk(isAsyncFunction(func), 'anonymous function is not an async function');
  31. var namedFunc = function foo() {};
  32. t.notOk(isAsyncFunction(namedFunc), 'named function is not an async function');
  33. if (typeof window === 'undefined') {
  34. t.skip('window.alert is not an async function');
  35. } else {
  36. t.notOk(isAsyncFunction(window.alert), 'window.alert is not an async function');
  37. }
  38. t.end();
  39. });
  40. var fakeToString = function () { return 'async function () { return "TOTALLY REAL I SWEAR!"; }'; };
  41. test('returns false for non-async function with faked toString', function (t) {
  42. var func = function () {};
  43. func.toString = fakeToString;
  44. t.notEqual(String(func), Function.prototype.toString.apply(func), 'faked toString is not real toString');
  45. t.notOk(isAsyncFunction(func), 'anonymous function with faked toString is not an async function');
  46. t.end();
  47. });
  48. test('returns false for generator functions', function (t) {
  49. if (generatorFuncs.length > 0) {
  50. forEach(generatorFuncs, function (generatorFunc) {
  51. t.notOk(isAsyncFunction(generatorFunc), generatorFunc + ' is not async function');
  52. });
  53. } else {
  54. t.skip('generator function is not async function - this environment does not support ES6 generator functions. Please use an engine that supports them.');
  55. }
  56. t.end();
  57. });
  58. test('returns false for non-async function with faked @@toStringTag', { skip: !hasToStringTag || asyncFuncs.length === 0 }, function (t) {
  59. var asyncFunc = asyncFuncs[0];
  60. /** @type {{ toString(): unknown; valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
  61. var fakeAsyncFunction = {
  62. toString: function () { return String(asyncFunc); },
  63. valueOf: function () { return asyncFunc; }
  64. };
  65. fakeAsyncFunction[Symbol.toStringTag] = 'AsyncFunction';
  66. t.notOk(isAsyncFunction(fakeAsyncFunction), 'fake AsyncFunction with @@toStringTag "AsyncFunction" is not an async function');
  67. t.end();
  68. });
  69. test('returns true for async functions', function (t) {
  70. if (asyncFuncs.length > 0) {
  71. forEach(asyncFuncs, function (asyncFunc) {
  72. t.ok(isAsyncFunction(asyncFunc), asyncFunc + ' is async function');
  73. });
  74. } else {
  75. t.skip('async function is async function - this environment does not support ES2018 async functions. Please use an engine that supports them.');
  76. }
  77. t.end();
  78. });