-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.compiled.js
More file actions
107 lines (89 loc) · 3.02 KB
/
index.compiled.js
File metadata and controls
107 lines (89 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = tapePromiseFactory;
var _onetime = require('onetime');
var _onetime2 = _interopRequireDefault(_onetime);
var _isPromise = require('is-promise');
var _isPromise2 = _interopRequireDefault(_isPromise);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// From: https://github.com/substack/tape/blob/17276d7473f9d98e37bab47ebdddf74ca1931f43/lib/test.js#L24
// Modified only for linting
const getTestArgs = function getTestArgs(name_, opts_, cb_) {
let name = '(anonymous)';
let opts = {};
let cb;
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
var t = typeof arg;
if (t === 'string') {
name = arg;
} else if (t === 'object') {
opts = arg || opts;
} else if (t === 'function') {
cb = arg;
}
}
return { name: name, opts: opts, cb: cb };
};
function registerNewAssertions(Test) {
Test.prototype.rejects = function (promise, expected, message = 'should reject', extra) {
if (typeof promise === 'function') promise = promise();
return promise.then(() => {
this.throws(() => {}, expected, message, extra);
}).catch(err => {
this.throws(() => {
throw err;
}, expected, message, extra);
}).then(() => {}); // resolve on failure to not stop execution (assertion is still failing)
};
Test.prototype.doesNotReject = function (promise, expected, message = 'should resolve', extra) {
if (typeof promise === 'function') promise = promise();
return promise.then(() => {
this.doesNotThrow(() => {}, expected, message, extra);
}).catch(err => {
this.doesNotThrow(() => {
throw err;
}, expected, message, extra);
}).then(() => {}); // resolve on failure to not stop execution (assertion is still failing)
};
}
function tapePromiseFactory(tapeTest) {
const Test = tapeTest.Test;
// when tapeTest.only() is passed in, Test will be undefined
if (Test) registerNewAssertions(Test);
function testPromise(...args) {
var _getTestArgs = getTestArgs(...args);
const name = _getTestArgs.name,
opts = _getTestArgs.opts,
cb = _getTestArgs.cb;
tapeTest(name, opts, function (t) {
t.end = (0, _onetime2.default)(t.end);
let plan = false;
const setPlan = () => {
plan = true;
};
t.once('plan', setPlan);
process.once('unhandledRejection', t.end);
try {
const p = cb(t);
if ((0, _isPromise2.default)(p) && !plan) p.then(() => t.end(), t.end);
} catch (e) {
t.end(e);
} finally {
process.removeListener('unhandledRejection', t.end);
t.removeListener('plan', setPlan);
}
});
}
Object.keys(tapeTest).forEach(key => {
if (typeof tapeTest[key] !== 'function') return;
if (key === 'only') {
testPromise[key] = tapePromiseFactory(tapeTest[key]);
} else {
testPromise[key] = tapeTest[key];
}
});
return testPromise;
}