示例工程 01-orientation
01-orientation/03-file-protocol-esm/main.test.mjs
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { diagnose, run } from './main.js';
test('file: 判为不能加载,并给出可执行的处理方式', () => {
const r = diagnose('file:');
assert.equal(r.ok, false);
assert.match(r.reason, /CORS/);
assert.match(r.fix, /serve\.mjs/);
});
test('http: 与 https: 判为可以加载', () => {
assert.equal(diagnose('http:').ok, true);
assert.equal(diagnose('https:').ok, true);
assert.match(diagnose('https:').reason, /同源/);
});
test('未知协议不被当作可加载源', () => {
const r = diagnose('chrome-extension:');
assert.equal(r.ok, false);
assert.match(r.reason, /未知协议/);
});
test('run 在 Node 中给出环境错误', () => {
assert.throws(() => run(() => {}), /需要浏览器环境/);
});