-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathava-check.js
98 lines (83 loc) · 2.75 KB
/
ava-check.js
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
var testcheck = require('testcheck');
exports.gen = testcheck.gen;
exports.check = check;
function check(/* [options,] ...args, propertyFn */) {
// Gather arguments:
// - options, gen, gen, propFn
// - gen, gen, propFn
var i = 0;
var n = arguments.length - 1;
var options = arguments[i].constructor === Object ? arguments[i++] : {};
var propertyFn = arguments[n];
var argGens = [];
for (; i < n; i++) {
argGens.push(arguments[i]);
}
// Current stack used for failing tests without errors.
var callingStack = {};
Error.captureStackTrace(callingStack, check);
return function (api) {
var test = this;
// Detect unsupported cases
if (test.metadata.callback) {
throw new TypeError('ava-check cannot check tests with callbacks.');
}
// Build property
var fn = propertyFn.bind(test, api);
var property = testcheck.property(argGens, function testcheck$property() {
// Reset assertions and plan before every run.
test.assertError = undefined;
test.assertCount = 0;
test.planCount = null;
test.planStack = null;
var result = fn.apply(null, arguments);
if (typeof result === 'object' &&
(typeof result.then === 'function' ||
typeof result.subscribe === 'function')) {
throw new TypeError('ava-check cannot check async tests.');
}
if (test.assertError) {
throw test.assertError;
}
return result;
});
// Run testcheck
var checkResult = testcheck.check(property, options);
// Check for async assertions
if (this.planCount) {
throw new TypeError(`ava-check cannot check tests with async assertions. Please remove \`t.plan(${this.planCount})\`.`);
}
// Report results
if (checkResult.fail) {
var shrunk = checkResult.shrunk;
var args = shrunk ? shrunk.smallest : checkResult.fail;
var result = shrunk ? shrunk.result : checkResult.result;
test.title += ' ' + printArgs(args, checkResult.seed);
if (result instanceof Error) {
test.assertError = cleanStack(result);
} else {
var error = new Error(String(result));
error.stack = callingStack.stack;
error.actual = result;
error.expected = true;
error.operator = '===';
test.assertError = error;
}
}
}
}
function printArgs(args, seed) {
const inspect = require('util').inspect;
return ' (' + inspect(args, { depth: null, colors: true }).slice(1, -1) +
') Seed: ' + inspect(seed, { colors: true });
}
function cleanStack(error) {
var stack = error.stack.split('\n')
for (var i = 1; i < stack.length; i++) {
if (stack[i].indexOf('testcheck$property') !== -1) {
break;
}
}
error.stack = stack.slice(0, i).join('\n');
return error;
}