Last active
January 28, 2017 23:58
-
-
Save cqfd/19f6c4e1d3181509f668d30eaf4bbadf to your computer and use it in GitHub Desktop.
Mini webpack-driven testing library.
This file contains hidden or 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
const { runTest } = require('./t') | |
let testCache = {} | |
const ctxId = runDirtyTests() | |
module.hot.accept(ctxId, runDirtyTests) | |
module.hot.accept() | |
module.hot.dispose(() => { testCache = {} }) | |
function runDirtyTests() { | |
clearScreen() | |
const ctx = require.context('.', false, /\.tests.js$/) | |
for (const key of ctx.keys()) { | |
const test = ctx(key) | |
if (testCache[key] !== test) { | |
testCache[key] = test | |
runTest(key, test).then(result => console.dir(result, { depth: null })) | |
} | |
} | |
return ctx.id | |
} | |
function clearScreen() { | |
console.log('\x1Bc') | |
} |
This file contains hidden or 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
module.exports = async t => { | |
t.deepEqual('a', 'a') | |
await sleep(1000) | |
t.notEqual(1, 2) | |
t('subtest', async t => { | |
t.equal(1, 1) | |
t.deepEqual(1, 1) | |
await sleep(1000) | |
t.equal('a', 'a') | |
t('sub sub test', t => t.equal('sub', 'sub')) | |
}) | |
} | |
function sleep(ms) { | |
return new Promise(ok => setTimeout(ok, ms)) | |
} |
This file contains hidden or 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
import assert, { AssertionError } from 'assert' | |
export async function runTest(name, test) { | |
const t = buildTester() | |
t.__test = { name: name, assertions: [], subtests: [] } | |
try { | |
t.__test.result = { tag: 'ok', value: await test(t) } | |
} catch (e) { | |
t.__test.result = { tag: 'error', error: e } | |
} | |
t.__test.subtests = await Promise.all(t.__test.subtests) | |
return t.__test | |
} | |
function buildTester() { | |
const t = (name, test) => t.__test.subtests.push(runTest(name, test)) | |
return assertProxy(t) | |
} | |
function assertProxy(t) { | |
return new Proxy(t, { | |
get(target, name) { | |
if (name in target) { | |
return target[name] | |
} else { | |
return (...args) => { | |
let assertion = { assertion: [name, ...args] } | |
try { assert[name](...args) } | |
catch (e) { | |
if (e instanceof AssertionError) assertion.error = e | |
else throw e | |
} | |
t.__test.assertions.push(assertion) | |
} | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment