Skip to content

Instantly share code, notes, and snippets.

@mindon
Created March 12, 2021 17:43
Show Gist options
  • Save mindon/4ef5daeac5950d99446a18819ce920aa to your computer and use it in GitHub Desktop.
Save mindon/4ef5daeac5950d99446a18819ce920aa to your computer and use it in GitHub Desktop.
A simple helper to test functions not-exported but with _ prefix
// A simple helper to test functions not-exported but with _ prefix
const privateCases = async function (flpath: string, cases: Function) {
const decoder = new TextDecoder("utf-8");
let body = decoder.decode(await Deno.readFile(flpath));
body = body.replace(/\nfunction _/g, "\nexport function _");
const tmppath = flpath.replace(/([^\/]+)$/, "~$1"),
encoder = new TextEncoder();
await Deno.writeFile(tmppath, encoder.encode(body));
cases(await import(tmppath));
await Deno.remove(tmppath);
};
// Usage example
await privateCases("./hello.ts", (m: any) => {
Deno.test("Hello not-exported _demo", () => {
const result = m._demo();
// ...
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment