Skip to content

Commit

Permalink
refactor(tests/test-nim-to-wasm): Run compile step before each corres…
Browse files Browse the repository at this point in the history
…ponding test

Intead of batching the compilation before all tests, as when a test fail it is
more difficult to figure out which one.
  • Loading branch information
PetarKirov committed Mar 22, 2024
1 parent a4eaee0 commit e188d5e
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions tests/test-nim-to-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ interface NimTestState<T extends WebAssembly.Exports = {}> {
}

describe('calling Nim functions compiled to Wasm', () => {
const filesToTest = glob(
dirname(fileURLToPath(import.meta.url)) + '/nimToWasm/*.nim',
{
ignore: '**/panicoverride\\.nim',
},
);
const basePath = dirname(fileURLToPath(import.meta.url)) + '/nimToWasm';

const filesToTest = glob(basePath + '/*.nim', {
ignore: '**/panicoverride\\.nim',
});

console.log({ filesToTest });

const perFileState: Record<string, NimTestState> = {};

Expand All @@ -29,35 +30,32 @@ describe('calling Nim functions compiled to Wasm', () => {
path: string,
func: (state: NimTestState<T>) => void,
) {
test(`Testing '${path}': '${testName}'`, () =>
func(perFileState[path] as NimTestState<T>));
}
test(`Testing '${path}': '${testName}'`, async () => {
const nimFilePath = basePath + `/${path}`;

beforeAll(async () => {
await Promise.all(
filesToTest.map(async nimFilePath => {
const wasmFilePath = (
await compileNimFileToWasm(nimFilePath, '--d:lightClientWASM')
).outputFileName;
const exports = await loadWasm<{}>({
from: {
filepath: wasmFilePath,
},
importObject: {
env: {
print: (x: unknown) =>
perFileState[basename(nimFilePath)].logMessages.push(String(x)),
},
const wasmFilePath = (
await compileNimFileToWasm(nimFilePath, '--d:lightClientWASM')
).outputFileName;
const exports = await loadWasm<{}>({
from: {
filepath: wasmFilePath,
},
importObject: {
env: {
print: (x: unknown) =>
perFileState[basename(nimFilePath)].logMessages.push(String(x)),
},
});
perFileState[basename(nimFilePath)] = {
wasmFilePath,
logMessages: [],
exports,
};
}),
);
}, 20000 /* timeout in milliseconds */);
},
});
perFileState[basename(nimFilePath)] = {
wasmFilePath,
logMessages: [],
exports,
};

func(perFileState[path] as NimTestState<T>);
});
}

testNimToWasmFile<{
printAdd: (a: number, b: number) => void;
Expand Down

0 comments on commit e188d5e

Please sign in to comment.