Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: run tests for noir_wasm in node under default resolver #3020

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"scripts": {
"build": "bash ./build.sh",
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test": "yarn test:node && yarn test:browser",
"test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' mocha",
"test:browser": "web-test-runner",
"clean": "chmod u+w web nodejs || true && rm -rf ./nodejs ./web ./target ./result",
"publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish",
Expand Down
34 changes: 32 additions & 2 deletions compiler/wasm/test/browser/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from '@esm-bundle/chai';
import initNoirWasm from '@noir-lang/noir_wasm';
import { compileNoirSource, nargoArtifactPath, noirSourcePath } from '../shared';
import initNoirWasm, { compile } from '@noir-lang/noir_wasm';
import { initializeResolver } from '@noir-lang/source-resolver';
import { nargoArtifactPath, noirSourcePath } from '../shared';

beforeEach(async () => {
await initNoirWasm();
Expand All @@ -22,6 +23,35 @@ async function getPrecompiledSource(): Promise<any> {
return JSON.parse(compiledData);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function compileNoirSource(noir_source: string): Promise<any> {
console.log('Compiling Noir source...');

initializeResolver((id: string) => {
console.log(`Resolving source ${id}`);

const source = noir_source;

if (typeof source === 'undefined') {
throw Error(`Could not resolve source for '${id}'`);
} else if (id !== '/main.nr') {
throw Error(`Unexpected id: '${id}'`);
} else {
return source;
}
});

try {
const compiled_noir = compile('main.nr');

console.log('Noir source compilation done.');

return compiled_noir;
} catch (e) {
console.log('Error while compiling:', e);
}
}

describe('noir wasm compilation', () => {
it('matches nargos compilation', async () => {
const source = await getSource();
Expand Down
1 change: 0 additions & 1 deletion compiler/wasm/test/index.d.ts

This file was deleted.

12 changes: 3 additions & 9 deletions compiler/wasm/test/node/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { expect } from 'chai';
import { compileNoirSource, nargoArtifactPath, noirSourcePath } from '../shared';
import { nargoArtifactPath, noirSourcePath } from '../shared';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { compile } from '@noir-lang/noir_wasm';

async function getFileContent(path: string): Promise<string> {
return readFileSync(join(__dirname, path)).toString();
}

async function getSource(): Promise<string> {
return getFileContent(noirSourcePath);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function getPrecompiledSource(): Promise<any> {
const compiledData = await getFileContent(nargoArtifactPath);
Expand All @@ -19,10 +16,7 @@ async function getPrecompiledSource(): Promise<any> {

describe('noir wasm compilation', () => {
it('matches nargos compilation', async () => {
const source = await getSource();

const wasmCircuit = await compileNoirSource(source);

const wasmCircuit = await compile(noirSourcePath);
const cliCircuit = await getPrecompiledSource();

// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
Expand Down
32 changes: 0 additions & 32 deletions compiler/wasm/test/shared.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,2 @@
import { initializeResolver } from '@noir-lang/source-resolver';
import { compile } from '@noir-lang/noir_wasm';

export const noirSourcePath = '../../noir-script/src/main.nr';
export const nargoArtifactPath = '../../noir-script/target/noir_wasm_testing.json';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function compileNoirSource(noir_source: string): Promise<any> {
console.log('Compiling Noir source...');

initializeResolver((id: string) => {
console.log(`Resolving source ${id}`);

const source = noir_source;

if (typeof source === 'undefined') {
throw Error(`Could not resolve source for '${id}'`);
} else if (id !== '/main.nr') {
throw Error(`Unexpected id: '${id}'`);
} else {
return source;
}
});

try {
const compiled_noir = compile('main.nr');

console.log('Noir source compilation done.');

return compiled_noir;
} catch (e) {
console.log('Error while compiling:', e);
}
}
Loading