Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created December 2, 2019 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/3b263681bf27099f431e3d8f49641c41 to your computer and use it in GitHub Desktop.
Save mizchi/3b263681bf27099f431e3d8f49641c41 to your computer and use it in GitHub Desktop.
// @ts-ignore
import { compile } from "@mizchi/web-compiler";
async function transformInput(code: string) {
const out = await compile({
entry: "/index.js",
files: {
"/index.js": code
},
pkg: {
dependencies: {}
},
tsConfig: `{
compilerOptions: {
target: "es5"
}
}`
});
return out;
}
const testBody = `
const tests = [];
function it(name, func) {
tests.push({ name, func });
}
function _run() {
for (const t of tests) {
try {
const ret = t.func();
Promise.resolve(ret)
.then(ret => {
pass(t, ret);
})
.catch(err => {
fail(t, err);
});
} catch (err) {
fail(t, err);
}
}
}
eval($code$);
setTimeout(_run);
return 1;
`;
// import { EventEmitter } from "events";
export async function runAsTest(code: string) {
// function pass() {}
const resolve = (t, ret) => {
console.log("[pass]", t.name);
};
const reject = (t, ret) => {
console.log("[fail]", t.name);
};
const func = new Function("$code$", "pass", "fail", testBody);
return func(code, resolve, reject);
}
export async function run() {
const code = await transformInput(`
it("sync success", () => {
console.log("x:hello");
});
it("sync fail", () => {
throw new Error();
});
it("async success", () => {
return new Promise((resolve, reject) => {
resolve();
})
});
it("async fail", () => {
return new Promise((resolve, reject) => {
reject();
})
});
`);
const result = await runAsTest(code);
return;
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment