-
Notifications
You must be signed in to change notification settings - Fork 592
/
examples.test.ts
108 lines (88 loc) · 3.16 KB
/
examples.test.ts
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
108
import Ajv from 'ajv';
import draft6Schema from 'ajv/lib/refs/json-schema-draft-06.json';
import fs from 'fs';
import path from 'path';
import {Spec as VgSpec} from 'vega';
import vgSchema from 'vega/build/vega-schema.json';
import vlSchema from '../build/vega-lite-schema.json';
import {compile} from '../src/compile/compile';
import * as log from '../src/log';
import {TopLevelSpec} from '../src/spec';
import {duplicate} from '../src/util';
// import {inspect} from 'util';
const ajv = new Ajv({
validateSchema: true,
allErrors: true,
// format: 'full', // remove since we don't encode refs
extendRefs: 'fail',
schemaId: 'auto' // for draft 04 and 06 schemas
});
ajv.addMetaSchema(draft6Schema);
ajv.addFormat('color-hex', () => true);
console.warn = () => {
throw new Error('We should not call console.warn.');
};
const validateVl = ajv.compile(vlSchema);
const validateVg = ajv.compile(vgSchema);
function validateVL(spec: TopLevelSpec) {
const valid = validateVl(spec);
const errors = validateVl.errors;
if (!valid) {
// uncomment to show schema validation error details
// console.log(inspect(errors, {depth: 10, colors: true}));
}
expect(errors?.map((err: Ajv.ErrorObject) => err.message).join(', ')).toBeUndefined();
expect(valid).toBe(true);
expect(spec.$schema.substr(0, 42)).toBe('https://vega.github.io/schema/vega-lite/v4');
}
function validateVega(vegaSpec: VgSpec) {
const valid = validateVg(vegaSpec);
const errors = validateVg.errors;
if (!valid) {
// uncomment to show schema validation error details
// console.log(inspect(errors, {depth: 10, colors: true}));
}
expect(errors?.map((err: Ajv.ErrorObject) => err.message).join(', ')).toBeUndefined();
expect(valid).toBe(true);
}
const BROKEN_SUFFIX = '_broken.vl.json';
const FUTURE_SUFFIX = '_future.vl.json';
const examples = fs.readdirSync('examples/specs').map(file => 'examples/specs/' + file);
const normalizedExamples = fs.readdirSync('examples/specs/normalized').map(file => 'examples/specs/normalized/' + file);
for (const example of [...examples, ...normalizedExamples]) {
if (path.extname(example) !== '.json') {
continue;
}
const jsonSpec = JSON.parse(fs.readFileSync(example).toString());
const originalSpec = duplicate(jsonSpec);
describe(
// eslint-disable-next-line jest/valid-describe, jest/valid-title
example,
log.wrap(localLogger => {
const vegaSpec: VgSpec = compile(jsonSpec).spec;
it('should not cause any side effects', () => {
expect(jsonSpec).toEqual(originalSpec);
});
// eslint-disable-next-line jest/expect-expect
it('should be valid Vega-Lite with proper $schema', () => {
if (example.endsWith(FUTURE_SUFFIX)) {
return;
}
validateVL(jsonSpec);
});
it('should not produce any warning', () => {
if (example.endsWith(BROKEN_SUFFIX)) {
return;
}
expect(localLogger.warns).toEqual([]);
});
// eslint-disable-next-line jest/expect-expect
it('should produce valid Vega', () => {
if (example.endsWith(BROKEN_SUFFIX)) {
return;
}
validateVega(vegaSpec);
});
})
);
}