Debian Codegraph @

istanbuljs-schema/test/test.js

raw symbols SCIP
1 import path from 'path';
2 import {test} from 'tap';
3
4 import schemas from '..';
5
6 function isObjectSchema(t, obj) {
7 t.type(obj, 'object');
8 t.type(obj.description, 'string');
9 t.is(obj.type, 'object');
10 t.type(obj.properties, 'object');
11 }
12
13 test('expected exports', async t => {
14 t.type(schemas, 'object');
15 isObjectSchema(t, schemas.nyc);
16 isObjectSchema(t, schemas.testExclude);
17 isObjectSchema(t, schemas.babelPluginIstanbul);
18 isObjectSchema(t, schemas.instrumenter);
19 });
20
21 const noDefault = ['nycrcPath', 'cacheDir'];
22 const commandOptions = [null, 'instrument', 'report', 'check-coverage', 'merge'];
23 const infoOptions = [
24 'default',
25 'description',
26 'type',
27 'nycCommands',
28 'nycAlias',
29 'nycHiddenAlias'
30 ];
31
32 function maybeType(t, value, type) {
33 if (typeof value !== 'undefined') {
34 t.type(value, type);
35 }
36 }
37
38 Object.entries(schemas.nyc.properties).forEach(([name, info]) => {
39 test(`nyc.properties['${name}'] is valid`, async t => {
40 t.type(info, 'object', name);
41 t.type(info.description, 'string', name);
42 t.type(info.type, name === 'nycrcPath' ? 'undefined' : 'string');
43 t.true(Array.isArray(info.nycCommands));
44 t.same(info.nycCommands.filter(s => !commandOptions.includes(s)), []);
45
46 maybeType(t, info.nycAlias, 'string');
47 maybeType(t, info.nycHiddenAlias, 'string');
48
49 const extraInfo = Object.keys(info).filter(s => !infoOptions.includes(s));
50 if (info.type === 'array') {
51 t.type(info.items, 'object');
52 t.is(info.items.type, 'string');
53 t.true(Array.isArray(info.default));
54 t.true(info.default.every(s => typeof s === 'string'));
55 t.same(extraInfo, ['items']);
56 } else {
57 if (noDefault.includes(name)) {
58 t.type(info.default, 'undefined');
59 } else {
60 t.type(info.default, info.type);
61 }
62
63 if (info.type === 'number') {
64 maybeType(t, info.minimum, 'number');
65 maybeType(t, info.maximum, 'number');
66 t.same(extraInfo.filter(s => !['maximum', 'minimum'].includes(s)), []);
67 } else {
68 t.same(extraInfo, []);
69 }
70 }
71 });
72 });
73
74 function checkDefaults(t, id) {
75 const defaults = schemas.defaults[id];
76 if ('cwd' in defaults) {
77 t.is(defaults.cwd, process.cwd());
78 defaults.cwd = '$CWD';
79 }
80
81 Object.entries(defaults).forEach(([name, value]) => {
82 if (!value || typeof value !== 'object') {
83 return;
84 }
85
86 /* Verify arrays / objects are shallow clones. */
87 t.same(defaults[name], schemas[id].properties[name].default);
88 t.notEqual(defaults[name], schemas[id].properties[name].default);
89 });
90
91 t.matchSnapshot(defaults, id);
92 }
93
94 test('defaults', async t => {
95 const originalCwd = process.cwd();
96 process.chdir(path.resolve(__dirname, '..'));
97 checkDefaults(t, 'nyc');
98 checkDefaults(t, 'testExclude');
99 checkDefaults(t, 'babelPluginIstanbul');
100 checkDefaults(t, 'instrumentVisitor');
101 checkDefaults(t, 'instrumenter');
102
103 process.chdir(__dirname);
104 Object.entries(schemas.defaults).forEach(([type, defaults]) => {
105 if ('cwd' in defaults) {
106 t.is(defaults.cwd, __dirname, type);
107 }
108 });
109
110 process.chdir(originalCwd);
111 });