Test toolkit for MoonBit wasm-gc web apps: a Node harness that drives your wasm exports over the JSON bridge, a headless-Chromium runner for DOM assertions, and moon-level envelope checks. The tools test themselves against the bundled demo app.
Dependencies
| 层 | 工具 | 运行环境 | 测什么 |
|---|---|---|---|
| moon 层 | @moonwebtest/check | moon test | 桥协议 wire format:信封形状、effect 类型断言(parse_envelope / effect_types / find_effect) |
| Node 层 | node/bridge-harness.mjs | Node ≥ 18,无浏览器 | 真实 wasm 实例过 FFI 桥:loadBridge 加载、init/dispatch 返回解析好的信封、fresh() 实例隔离 |
| 浏览器层 | node/headless.cjs | 任意 Chromium 系浏览器 | 真实渲染与交互:静态服务、无头启动、dump-DOM 捕获、harness 页结果抽取 |
// src/yourapp/main_test.mbt
test "dispatch before init answers the null-state error envelope" {
let env = @check.parse_envelope(your_dispatch_before_init)
assert_true(env is Some({ state: None, effects: _ }))
}
test "effects carry the right type tags" {
if env is Some({ effects, .. }) {
assert_eq(@check.effect_types(effects), ["save", "notify_error"])
assert_true(@check.find_effect(effects, "notify_error") is Some(
Object({ "message": String("未初始化"), .. }),
))
}
}// your-e2e.mjs
import { loadBridge, makeChecks } from 'moonwebtest/node/bridge-harness.mjs';
// 拷贝 node/ 到你的项目,或直接引用本仓库路径
const app = loadBridge('web/wasm/main.wasm', {
init: 'mtab_init', // 你的导出名
dispatch: 'mtab_dispatch',
});
let r = app.init('', '2025-01-29'); // 解析好的 {state, effects}
r = app.dispatch({ type: 'search_submit', query: 'moonbit' }); // 对象进
r = app.dispatchRaw('not json'); // 或预序列化字符串进(错误路径)
const fresh = app.fresh(); // 隔离实例:未初始化路径等
const { check, finish } = makeChecks();
check('search builds url', r.effects[0].type === 'open_url', r);
process.exit(finish()); // 汇总行 + 退出码const { serveStatic, runChromium, extractHarnessSummary, report } = require('./node/headless.cjs');
const site = await serveStatic('web', 8932);
const dom = await runChromium({ args: ['--dump-dom', site.url + 'test-harness.html'],
profileRoot, waitMarker: 'test-summary' });
const sum = extractHarnessSummary(dom);
failed += report('browser harness', sum.ok);moon test # check 包 + demoapp 的 moon 层
moon build --target wasm-gc --release # 构建 demo wasm
node scripts/self-e2e.mjs # Node 层:harness 驱动 demoapp
node scripts/self-headless.cjs # 浏览器层(local-only)Install
Download zipTest toolkit for MoonBit wasm-gc web apps: a Node harness that drives your wasm exports over the JSON bridge, a headless-Chromium runner for DOM assertions, and moon-level envelope checks. The tools test themselves against the bundled demo app.
Dependencies