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