MES手机端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

96 lines
2.9 KiB

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