Skip to content

Commit bc4e17f

Browse files
authored
Add test to make sure generated data is the same as JS data (#225)
1 parent d3eb851 commit bc4e17f

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

scripts/generate-data.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from 'node:fs/promises';
2-
import {readGlobals} from './utilities.mjs';
2+
import {readGlobals} from '../utilities.mjs';
33

44
const DATA_FILE = new URL('../globals.json', import.meta.url);
55

scripts/utilities.mjs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
import fs from 'node:fs/promises';
22
import {outdent} from 'outdent';
3-
import {unique, sortObject} from '../utilities.mjs';
4-
5-
const DATA_DIRECTORY = new URL('../data/', import.meta.url);
6-
7-
const readGlobals = async (environment, {ignoreNonExits} = {}) => {
8-
const file = new URL(`${environment}.mjs`, DATA_DIRECTORY);
9-
file.searchParams.set('ts', Date.now());
10-
11-
let data;
12-
13-
try {
14-
({default: data} = await import(file));
15-
} catch (error) {
16-
if (ignoreNonExits && error.code === 'ERR_MODULE_NOT_FOUND') {
17-
return {};
18-
}
19-
20-
throw error;
21-
}
22-
23-
return data;
24-
};
3+
import {
4+
DATA_DIRECTORY,
5+
unique,
6+
sortObject,
7+
readGlobals,
8+
} from '../utilities.mjs';
259

2610
const writeGlobals = async (environment, globals) => {
2711
const file = new URL(`${environment}.mjs`, DATA_DIRECTORY);
@@ -84,7 +68,6 @@ async function createGlobals(names, {
8468
}
8569

8670
export {
87-
readGlobals,
8871
updateGlobals,
8972
getGlobalThisProperties,
9073
createGlobals,

test.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import fs from 'node:fs/promises';
12
import test from 'ava';
3+
import {DATA_DIRECTORY, readGlobals} from './utilities.mjs';
24
import globals from './index.js';
35

46
test('main', t => {
@@ -81,3 +83,17 @@ test('es versions', t => {
8183
previousVersion = {esVersion, globals: Object.keys(globals[esVersion])};
8284
}
8385
});
86+
87+
test('globals.json', async t => {
88+
const files = await fs.readdir(DATA_DIRECTORY);
89+
const environments = files.filter(filename => filename.endsWith('.mjs')).map(filename => filename.slice(0, -4));
90+
91+
const jsData = Object.fromEntries(
92+
await Promise.all(environments.map(async environment => [environment, await readGlobals(environment)])),
93+
);
94+
95+
t.deepEqual(
96+
jsData,
97+
globals,
98+
);
99+
});

utilities.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
const DATA_DIRECTORY = new URL('data/', import.meta.url);
2+
3+
const readGlobals = async (environment, {ignoreNonExits} = {}) => {
4+
const file = new URL(`${environment}.mjs`, DATA_DIRECTORY);
5+
file.searchParams.set('ts', Date.now());
6+
7+
let data;
8+
9+
try {
10+
({default: data} = await import(file));
11+
} catch (error) {
12+
if (ignoreNonExits && error.code === 'ERR_MODULE_NOT_FOUND') {
13+
return {};
14+
}
15+
16+
throw error;
17+
}
18+
19+
return data;
20+
};
21+
122
const sortObject = object =>
223
Object.fromEntries(
324
Object.entries(object).sort(([propertyA], [propertyB]) =>
@@ -33,8 +54,10 @@ function getIntersectionGlobals(globalsA, globalsB) {
3354
}
3455

3556
export {
57+
DATA_DIRECTORY,
3658
unique,
3759
sortObject,
3860
mergeGlobals,
3961
getIntersectionGlobals,
62+
readGlobals,
4063
};

0 commit comments

Comments
 (0)