Skip to content

Commit

Permalink
Add test to make sure generated data is the same as JS data (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Feb 20, 2024
1 parent d3eb851 commit bc4e17f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion scripts/generate-data.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'node:fs/promises';
import {readGlobals} from './utilities.mjs';
import {readGlobals} from '../utilities.mjs';

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

Expand Down
29 changes: 6 additions & 23 deletions scripts/utilities.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import fs from 'node:fs/promises';
import {outdent} from 'outdent';
import {unique, sortObject} from '../utilities.mjs';

const DATA_DIRECTORY = new URL('../data/', import.meta.url);

const readGlobals = async (environment, {ignoreNonExits} = {}) => {
const file = new URL(`${environment}.mjs`, DATA_DIRECTORY);
file.searchParams.set('ts', Date.now());

let data;

try {
({default: data} = await import(file));
} catch (error) {
if (ignoreNonExits && error.code === 'ERR_MODULE_NOT_FOUND') {
return {};
}

throw error;
}

return data;
};
import {
DATA_DIRECTORY,
unique,
sortObject,
readGlobals,
} from '../utilities.mjs';

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

export {
readGlobals,
updateGlobals,
getGlobalThisProperties,
createGlobals,
Expand Down
16 changes: 16 additions & 0 deletions test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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 => {
Expand Down Expand Up @@ -81,3 +83,17 @@ test('es versions', t => {
previousVersion = {esVersion, globals: Object.keys(globals[esVersion])};
}
});

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,
);
});
23 changes: 23 additions & 0 deletions utilities.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
const DATA_DIRECTORY = new URL('data/', import.meta.url);

const readGlobals = async (environment, {ignoreNonExits} = {}) => {
const file = new URL(`${environment}.mjs`, DATA_DIRECTORY);
file.searchParams.set('ts', Date.now());

let data;

try {
({default: data} = await import(file));
} catch (error) {
if (ignoreNonExits && error.code === 'ERR_MODULE_NOT_FOUND') {
return {};
}

throw error;
}

return data;
};

const sortObject = object =>
Object.fromEntries(
Object.entries(object).sort(([propertyA], [propertyB]) =>
Expand Down Expand Up @@ -33,8 +54,10 @@ function getIntersectionGlobals(globalsA, globalsB) {
}

export {
DATA_DIRECTORY,
unique,
sortObject,
mergeGlobals,
getIntersectionGlobals,
readGlobals,
};

0 comments on commit bc4e17f

Please sign in to comment.