示例工程 13-declaration-files
13-declaration-files/01-ambient-declaration/main.test.mjs
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { run, sumWith } from './dist/main.js';
test('注入 api 时可在 Node 断言;不注入则需要浏览器全局', () => {
const out = [];
const fake = { version: 'test', add: (a, b) => a + b };
const ret = run((msg) => out.push(msg), fake);
assert.deepEqual(ret, ['3', 'test']);
assert.deepEqual(out, ret);
assert.equal(sumWith({ add: (a, b) => a * b }, 2, 3), 6);
assert.throws(() => run(() => {}), ReferenceError);
});