Skip to content

Commit

Permalink
Merge pull request #166 from ChALkeR/chalker/test/bundle-fixes/0
Browse files Browse the repository at this point in the history
test: add test bundler compatibility
  • Loading branch information
paulmillr authored Nov 9, 2024
2 parents fa6b011 + 022e43c commit 264388c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"build:clean": "rm *.{js,d.ts,d.ts.map,js.map} esm/*.{js,d.ts,d.ts.map,js.map} 2> /dev/null",
"lint": "prettier --check 'src/**/*.{js,ts}' 'test/*.js'",
"format": "prettier --write 'src/**/*.{js,ts}' 'test/*.js'",
"test": "node test/index.test.js"
"test": "node test/index.js"
},
"author": "Paul Miller (https://paulmillr.com)",
"homepage": "https://paulmillr.com/noble/",
Expand Down
2 changes: 1 addition & 1 deletion test/bls12-381.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const SCALAR_VECTORS = readFileSync('./test/bls12-381/bls12-381-scalar-test-vect
.map((l) => l.split(':'));

// @ts-ignore
const NUM_RUNS = Number(process.env.RUNS_COUNT || 10); // reduce to 1 to shorten test time
const NUM_RUNS = Number(globalThis.process?.env?.RUNS_COUNT || 10); // reduce to 1 to shorten test time
fc.configureGlobal({ numRuns: NUM_RUNS });

const G1Point = bls.G1.ProjectivePoint;
Expand Down
2 changes: 1 addition & 1 deletion test/bn254.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { bytesToNumberBE } from '../esm/abstract/utils.js';
import { jsonGZ } from './utils.js';
import { default as seda } from './bn254/seda.js';
import { default as ethDump } from './bn254/eth-dump.js';
const CROSS_PATH_GZ = './bn254/cross1000.json.gz';
const CROSS_PATH_GZ = './bn254/cross1000.json.gz'; // bundler hint: readFileSync('./test/bn254/cross1000.json.gz')

describe('bn254', () => {
const { Fp2, Fp6, Fp12 } = bn254.fields;
Expand Down
File renamed without changes.
14 changes: 11 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ import { gunzipSync } from 'node:zlib';
import { dirname, join as joinPath } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const _dirname = dirname(fileURLToPath(import.meta.url));

export function jsonGZ(path) {
const unz = gunzipSync(readFileSync(joinPath(__dirname, path)));
const unz = gunzipSync(readFileSync(joinPath(_dirname, path)));
return JSON.parse(unz.toString('utf8'));
}

export function json(path) {
return JSON.parse(readFileSync(joinPath(__dirname, path), { encoding: 'utf-8' }));
try {
// Node.js
return JSON.parse(readFileSync(joinPath(_dirname, path), { encoding: 'utf-8' }));
} catch {
// Bundler
const file = path.replace(/^\.\//, '').replace(/\.json$/, '');
if (path !== './' + file + '.json') throw new Error('Can not load non-json file');
return require('./' + file + '.json'); // in this form so that bundler can glob this
}
}

0 comments on commit 264388c

Please sign in to comment.