Skip to content

Commit

Permalink
Make it work in both browsers and node
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Mar 14, 2021
1 parent 06e063b commit aef51b5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -10,7 +10,7 @@
"build": "run-s lib:wasm:build lib:js:build",
"dev": "run-p lib:watch lib:test:watch",
"serve": "serve dist -p 8080",
"test": "mocha",
"test": "node ./test/test-runner.js",
"lint": "prettier --write **/*.js **/*.ts **/*.json !build/**/* !dist/**/*",
"lint:ci": "prettier --check **/*.js **/*.ts **/*.json !build/**/* !dist/**/*",
"lib:watch": "chokidar --initial \"lib/**/*\" -c \"run-s lib:wasm:build lib:js:build:dev test\"",
Expand Down
94 changes: 62 additions & 32 deletions test/test-runner.js
Expand Up @@ -3,7 +3,6 @@ const fs = require("fs/promises");

const Express = require("express");
const Mocha = require("mocha");
const assert = require("assert");
const glob = promisify(require("glob"));
const pptr = require("puppeteer");

Expand All @@ -15,16 +14,15 @@ globalThis.AsBind = require("../dist/as-bind.cjs.js");
const html = String.raw;

async function main() {
process.chdir(__dirname);
await asc.ready;

await compileAllAsc();

const [nodeFailures, browserFailures] = await Promise.all([
runTestsInNode(),
runTestsInPuppeteer()
]);
if (nodeFailures !== 0 || browserFailures !== 0) {
console.log({ nodeFailures, browserFailures });
if ((await runTestsInNode()) > 0) {
process.exit(1);
}
if ((await runTestsInPuppeteer()) > 0) {
process.exit(1);
}
console.log("Passed node and browser tests");
Expand Down Expand Up @@ -56,10 +54,6 @@ async function runTestsInNode() {
mocha.addFile(testFile);
}

mocha.globalSetup(() => {
this.assert = assert;
});

mocha.rootHooks({
async beforeEach() {
const { file } = this.currentTest;
Expand All @@ -74,41 +68,77 @@ async function runTestsInNode() {

async function runMochaAsync(mocha) {
await mocha.loadFilesAsync();
return new Promise(resolve => {
const runner = mocha.run(resolve);
});
return new Promise(resolve => mocha.run(resolve));
}

async function extractMochaStatDump(msg) {
if (msg.args().length == 1 && msg.text().startsWith("{")) {
const arg = await msg.args()[0].jsonValue();
let obj;
try {
obj = JSON.parse(arg);
} catch (e) {
return;
}
if (obj.hasOwnProperty("stats")) {
return obj;
}
}
}

const PORT = 50123;
const OPEN_DEVTOOLS = false;
async function runTestsInPuppeteer() {
const testFiles = await glob("./tests/**/test.js");
const browser = await pptr.launch({
headless: false
devtools: OPEN_DEVTOOLS
});
const page = await browser.newPage();
page.on("console", async msg =>
console[msg._type](
"Browser log:",
...(await Promise.all(msg.args().map(v => v.jsonValue())))
)
);
let result;
page.on("console", async msg => {
const maybeResult = await extractMochaStatDump(msg);
if (maybeResult) {
result = maybeResult;
return;
}
// Otherwise you can forward the log while debugging
// console.log("Browser log:", msg.text());
});

if (OPEN_DEVTOOLS) {
// If we want DevTools open, wait for a second here so it can load
// and `debugger` statements are effective.
await new Promise(resolve => setTimeout(resolve, 1000));
}
const app = Express();
app.use("/", Express.static("../"));
const server = app.listen(PORT);
await page.goto(`http://localhost:${PORT}/test/test-runner.html`);
await page.setContent(html`
<!DOCTYPE html>
<script src="/dist/as-bind.iife.js"></script>
<script>
self.AsBind = AsBindIIFE;
</script>
`);

const numFailures = await page.evaluate(async testFiles => {
for (const testFile of testFiles) {
await runScript(`/test/${testFile}`);
runInlineScript(`
suitePaths.push(${JSON.stringify(testFile)});
`);
}
const script = document.createElement("script");
script.innerHTML = `
self.mochaRun = new Promise(resolve => mocha.run(resolve));`;
document.body.append(script);
return self.mochaRun;
}, testFiles);
await page.close();
await browser.close();
// Just to make sure all the logs come out. There’s probably a better solution.
await new Promise(resolve => setTimeout(resolve, 1000));
server.close();
return 0;

console.log("\n\n=== Browser results");
for (const test of result.passes) {
console.log(`✓ ${test.fullTitle}`);
}
console.log("\n\n");
for (const test of result.failures) {
console.log(`X ${test.fullTitle}`);
console.log(` ${test.err.message}`);
}
return numFailures;
}
5 changes: 2 additions & 3 deletions test/tests/arraybufferview/test.js
Expand Up @@ -10,11 +10,10 @@ describe("as-bind", function() {
}
}
});
assert.strictEqual(
assert(
instance.exports
.exported(new Uint8Array([1, 2, 3]), new Uint8Array([10, 11, 12]))
.join(","),
new Uint8Array([255, 10, 11, 12, 1, 2, 3, 255]).join(",")
.join(",") === new Uint8Array([255, 10, 11, 12, 1, 2, 3, 255]).join(",")
);
});
});
2 changes: 1 addition & 1 deletion test/tests/strings/test.js
Expand Up @@ -7,6 +7,6 @@ describe("as-bind", function() {
}
}
});
assert.strictEqual(instance.exports.exported("a", "b"), "!ba!");
assert(instance.exports.exported("a", "b") === "!ba!");
});
});

0 comments on commit aef51b5

Please sign in to comment.