Skip to content

Commit

Permalink
Add test for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Mar 3, 2020
1 parent c54ef04 commit fda3a62
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ process.on('unhandledRejection', err => {
throw err;
});

const importObject = {
env: {
memory: new WebAssembly.Memory({ initial: 16 }),
get_time: Date.now,
sleep: promisify(setTimeout)
}
};
function getSimpleImportObject() {
return {
env: {
memory: new WebAssembly.Memory({ initial: 16 }),
get_time: Date.now,
sleep: promisify(setTimeout)
}
};
}

async function runTest(name, path, importObject, callback) {
console.group(name);
Expand Down Expand Up @@ -67,13 +69,13 @@ async function simpleTestCb({ run, run2 }) {
await runTest(
'Exported memory',
'mem-export.wat',
importObject,
getSimpleImportObject(),
simpleTestCb
);
await runTest(
'Imported memory',
'mem-import.wat',
importObject,
getSimpleImportObject(),
simpleTestCb
);

Expand All @@ -83,14 +85,18 @@ async function simpleTestCb({ run, run2 }) {
});
}

await runTest(
'Proxy-based imports',
'mem-export.wat',
proxyGet(moduleName =>
proxyGet(importName => importObject[moduleName][importName])
),
simpleTestCb
);
{
let realImportObject = getSimpleImportObject();

await runTest(
'Proxy-based imports',
'mem-export.wat',
proxyGet(moduleName =>
proxyGet(importName => realImportObject[moduleName][importName])
),
simpleTestCb
);
}

{
let savedCallback;
Expand All @@ -112,4 +118,26 @@ async function simpleTestCb({ run, run2 }) {
}
);
}

await runTest(
'Error handling',
'mem-export.wat',
{
env: {
...getSimpleImportObject().env,
async sleep() {
throw new Error('Expected error');
}
}
},
async ({ run }) => {
for (let i = 0; i < 2; i++) {
await assert.rejects(run, {
name: 'Error',
message: 'Expected error',
stack: /^\s+at wasm-function\[2\]:/m
});
}
}
);
})();

0 comments on commit fda3a62

Please sign in to comment.