Newer
Older
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
import fs from 'fs';
import glob from 'glob';
import { expect } from 'chai';
import _jsf from '../../src';
import { checkType, checkSchema } from './validator';
export const jsf = _jsf;
export function pick(obj, key) {
const parts = key.split('.');
let out = obj;
while (parts.length) {
out = out[parts.shift()];
}
return out;
}
export function getTests(srcDir) {
const only = [];
const all = [];
glob.sync(`${srcDir}/**/*.json`).forEach(file => {
let suite;
try {
suite = JSON.parse(fs.readFileSync(file));
} catch (e) {
console.log(`Invalid JSON: ${file}`);
console.log(e.message);
process.exit(1);
}
(Array.isArray(suite) ? suite : [suite]).forEach(x => {
if (x.xdescription) return;
let _only = false;
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
suite.tests = suite.tests.sort((a, b) => {
if (a.only) return -1;
if (b.only) return 1;
return 0;
}).filter(y => {
if ((_only && !y.only) || y.xdescription) return false;
if (y.only) _only = true;
return true;
});
if (x.only || _only) only.push(suite);
all.push(suite);
});
});
return { only, all };
}
export function tryTest(test, refs, schema) {
return _jsf.resolve(schema, refs).then(sample => {
if (test.dump) {
console.log(JSON.stringify(sample, null, 2));
return;
}
if (test.type) {
checkType(sample, test.type);
}
if (test.valid) {
checkSchema(sample, schema, refs);
}
if (test.length) {
expect(sample.length).to.eql(test.length);
}
if (test.notEmpty) {
test.notEmpty.forEach(x => {
const value = pick(sample, x);
if (value === undefined || (Array.isArray(value) && !value.length)) {
throw new Error(`${x} should not be empty`);
}
});
}
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
if (test.hasProps) {
test.hasProps.forEach(prop => {
if (Array.isArray(sample)) {
sample.forEach(s => {
expect(s[prop]).not.to.eql(undefined);
});
} else {
expect(sample[prop]).not.to.eql(undefined);
}
});
}
if (test.onlyProps) {
expect(Object.keys(sample)).to.eql(test.onlyProps);
}
if (test.count) {
expect((Array.isArray(sample) ? sample : Object.keys(sample)).length).to.eql(test.count);
}
if (test.hasNot) {
expect(JSON.stringify(sample)).not.to.contain(test.hasNot);
}
if ('equal' in test) {
expect(sample).to.eql(test.equal);
}
}).catch(error => {
if (typeof test.throws === 'string') {
expect(error).to.match(new RegExp(test.throws, 'im'));
}
if (typeof test.throws === 'boolean') {
if (test.throws !== true) {
throw error;
}