Files
bun.sh/test/js/node/string-module.test.js
2024-11-06 15:00:24 -08:00

20 lines
819 B
JavaScript

import { expect, test } from "bun:test";
test("should import and execute ES module from string", async () => {
const code = `export default function test(arg) { return arg + arg };`;
const mod = await import("data:text/javascript," + code).then(mod => mod.default);
const result = mod(1);
expect(result).toEqual(2);
});
test("should import and execute ES module from string (base64)", async () => {
const code = `export default function test(arg) { return arg + arg; }`;
const mod = await import("data:text/javascript;base64," + btoa(code)).then(mod => mod.default);
const result = mod(1);
expect(result).toEqual(2);
});
test("should throw when importing malformed string (base64)", async () => {
expect(() => import("data:text/javascript;base64,asdasdasd")).toThrowError("Base64DecodeError");
});