Created
December 2, 2019 08:21
-
-
Save mizchi/3b263681bf27099f431e3d8f49641c41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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