Skip to content

Commit 949a686

Browse files
test(jco): add codegen test for --strict
1 parent 68c793a commit 949a686

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/jco/test/codegen.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { createServer } from "node:http";
33
import { resolve, join } from "node:path";
44
import { fileURLToPath, pathToFileURL } from "node:url";
55

6-
import { componentNew, componentEmbed, transpile, preview1AdapterCommandPath } from "@bytecodealliance/jco";
6+
import { componentNew, componentEmbed, preview1AdapterCommandPath } from "@bytecodealliance/jco";
7+
import { transpile } from "../src/api.js";
78
import { HTTPServer } from "@bytecodealliance/preview2-shim/http";
89

910
import { suite, test, assert, describe } from "vitest";
@@ -228,3 +229,29 @@ suite("codegen determinism", () => {
228229
assert.deepEqual(streamRx[0].files, streamRx[1].files);
229230
});
230231
});
232+
233+
// see: https://github.com/bytecodealliance/jco/issues/1400
234+
suite("--strict", () => {
235+
test("does not add checks when disabled", async () => {
236+
const component = await readFile(join(COMPONENT_FIXTURES_DIR, "adder.component.wasm"));
237+
const { files } = await transpile(component, { name: "adder" });
238+
const bindingsSource = new TextDecoder().decode(files["adder.js"]);
239+
assert.isFalse(
240+
bindingsSource.includes("_requireValidNumericPrimitive('u32', val)"),
241+
"numeric primitive check shoudl not be included",
242+
);
243+
});
244+
245+
test("adds checks when enabled", async () => {
246+
const component = await readFile(join(COMPONENT_FIXTURES_DIR, "adder.component.wasm"));
247+
const { files } = await transpile(component, { name: "adder", strict: true });
248+
const bindingsSource = new TextDecoder().decode(files["adder.js"]);
249+
// Somewhat brittle, but we're looking for a *specific* call in the body of `toUint32(val)` below:
250+
assert.isOk(
251+
bindingsSource.includes(
252+
"_requireValidNumericPrimitive('u32', val)",
253+
"numeric primitive check should be included",
254+
),
255+
);
256+
});
257+
});

packages/jco/test/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export async function setupAsyncTest(args) {
232232
instantiation: "async",
233233
asyncMode,
234234
wasiShim: true,
235+
strict: true,
235236
outDir: moduleOutputDir,
236237
...(jco?.transpile?.extraArgs || {}),
237238
};

0 commit comments

Comments
 (0)