Files
bun.sh/test/js/node/string-module.test.js
Dylan Conway d88e3bfd3b maybe fix
2024-11-08 18:06:37 -08:00

26 lines
1.0 KiB
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("can import module with '.' in source code", async () => {
const code = `export default 1.1;`;
const mod = await import("data:text/javascript," + code).then(mode => mode.default);
expect(mod).toBe(1.1);
});
test("should throw when importing malformed string (base64)", async () => {
expect(() => import("data:text/javascript;base64,asdasdasd")).toThrowError("Base64DecodeError");
});