-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
test.mjs
105 lines (86 loc) · 3.01 KB
/
test.mjs
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
import fs from 'node:fs/promises';
import test from 'ava';
import {DATA_DIRECTORY, readGlobals} from './utilities.mjs';
import globals from './index.js';
test('main', t => {
t.is(typeof globals, 'object');
t.true(Object.keys(globals).length > 10 && Object.keys(globals).length < 1000);
});
test('ensure alphabetical order', t => {
for (const env of Object.keys(globals)) {
const keys = Object.keys(globals[env]);
t.deepEqual(
[...keys], keys.sort((a, b) => a.localeCompare(b)),
`The \`${env}\` keys don't have the correct alphabetical order`,
);
}
});
test('`node` is `nodeBuiltin` with CommonJS arguments', t => {
// `globals.node` has `global`` which isn't a CommonJS argument and doesn't include
// `__filename` and `__dirname` which are.
const commonjsArguments = {
__dirname: false,
__filename: false,
exports: true,
module: false,
require: false,
};
t.deepEqual({...globals.nodeBuiltin, ...commonjsArguments}, globals.node);
// Ensure that there's no overlap between true globals and the CommonJS arguments above.
for (const builtin of Object.keys(globals.nodeBuiltin)) {
t.is(
commonjsArguments[builtin],
undefined,
`The builtin ${builtin} is not a CommonJS argument`,
);
}
});
test('should not contain builtins', t => {
const builtins = new Set(Object.keys(globals.builtin));
for (const [env, envGlobals] of Object.entries(globals)) {
if (env === 'builtin' || /^es\d+$/.test(env)) {
continue;
}
const keys = Object.keys(envGlobals).filter(key => builtins.has(key));
t.deepEqual(
keys,
[],
`The \`${env}\` keys should not contain builtins.`,
);
}
});
test('es versions', t => {
const builtins = new Map(Object.entries(globals.builtin));
const esVersions = Object.keys(globals)
.filter(key => /^es(?:3|5|\d{4})$/.test(key))
.sort((versionA, versionB) => Number(versionA.slice(2)) - Number(versionB.slice(2)));
let previousVersion;
for (const esVersion of esVersions) {
const data = globals[esVersion];
for (const [key, value] of Object.entries(data)) {
t.true(builtins.has(key), `The builtin '${key}' in '${esVersion}' is missing in 'builtin'.`);
t.is(value, builtins.get(key), `Value of '${key}' should be the same as 'builtin'.`);
}
if (previousVersion) {
t.deepEqual(
previousVersion.globals.filter(key => !Object.hasOwn(data, key)),
[],
`The builtins in '${previousVersion.esVersion}' are missing in '${esVersion}'.`,
);
}
previousVersion = {esVersion, globals: Object.keys(globals[esVersion])};
}
const latestVersion = esVersions.at(-1);
t.deepEqual(globals[latestVersion], globals.builtin, `'${latestVersion}' should be the same as 'builtin'.`);
});
test('globals.json', async t => {
const files = await fs.readdir(DATA_DIRECTORY);
const environments = files.filter(filename => filename.endsWith('.mjs')).map(filename => filename.slice(0, -4));
const jsData = Object.fromEntries(
await Promise.all(environments.map(async environment => [environment, await readGlobals(environment)])),
);
t.deepEqual(
jsData,
globals,
);
});