Skip to content

Commit 08c8e77

Browse files
committed
Get WASM code to run under Jest at all
1 parent 6992eca commit 08c8e77

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

Diff for: src/GBZBaseAPI.mjs

+15-7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export class GBZBaseAPI extends APIInterface {
6969

7070
// Make a call into the WebAssembly code and return the result.
7171
async callWasm(argv) {
72+
if (argv.length < 1) {
73+
// We need at least one command line argument to be the program name.
74+
throw new Error("Not safe to invoke main() without program name");
75+
}
76+
7277
await this.setUp();
7378
let wasm = this.compiledWasm;
7479

@@ -78,7 +83,7 @@ export class GBZBaseAPI extends APIInterface {
7883
let stderr = new File([]);
7984

8085
// Environment variables as NAME=value strings
81-
const environment = [];
86+
const environment = ["RUST_BACKTRACE=full"];
8287

8388
// File descriptors for the process in number order
8489
let file_descriptors = [new OpenFile(stdin), new OpenFile(stdout), new OpenFile(stderr)];
@@ -91,12 +96,15 @@ export class GBZBaseAPI extends APIInterface {
9196
"wasi_snapshot_preview1": wasi.wasiImport,
9297
});
9398

94-
// Make the WASI system call main
95-
let returnCode = wasi.start(instantiation);
96-
97-
console.log("Return code:", returnCode);
98-
console.log("Standard Output:", stdout);
99-
console.log("Standard Error:", stderr);
99+
try {
100+
// Make the WASI system call main
101+
let returnCode = wasi.start(instantiation);
102+
console.log("Return code:", returnCode);
103+
} finally {
104+
// The WASM code can throw right out of the WASI shim if Rust panics.
105+
console.log("Standard Output:", new TextDecoder().decode(stdout.data));
106+
console.log("Standard Error:", new TextDecoder().decode(stderr.data));
107+
}
100108
}
101109

102110
/////////

Diff for: src/GBZBaseAPI.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ it("can be constructed", () => {
88

99
it("can run the WASM blob", async () => {
1010
let api = new GBZBaseAPI();
11-
await api.callWasm([]);
11+
await api.callWasm(["query", "--help"]);
1212
});
1313

1414
it("can have a file uploaded", async () => {

Diff for: src/setupTests.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Configure Jest environment for all tests.
2+
//
3+
// create-react-app auto-magically executes this file by name for all the test
4+
// suites. See <https://github.com/facebook/create-react-app/issues/9706>.
5+
6+
// Make TextEncoder and TextDecoder available. Browsers and Node both have them
7+
// but jsdom refuses to let us use them. See
8+
// <https://stackoverflow.com/a/68468204>
9+
import { TextEncoder, TextDecoder } from "util";
10+
globalThis.TextEncoder = TextEncoder;
11+
globalThis.TextDecoder = TextDecoder;

0 commit comments

Comments
 (0)