Skip to content

Instantly share code, notes, and snippets.

@JAForbes
Created August 22, 2021 00:52
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 JAForbes/79bc3a8a90a1bc29c98482d8d16063ab to your computer and use it in GitHub Desktop.
Save JAForbes/79bc3a8a90a1bc29c98482d8d16063ab to your computer and use it in GitHub Desktop.
Fake Tape

Fake Tape

I was getting a weird error in Node, it would exit randomly, I could not figure it out. There was no error or stack trace. I could not step into the line with the debugger.

I thought maybe the test runner was doing something strange. So I quickly mocked out the test runner, at least the bits I use.

It ended up being unrelated to the test runner, but I wanted to record this as its a nice simple async test runner.

let tape = {
i: 0
,tests: []
}
function test(name, visitor){
tape.tests.push({ name, visitor })
}
Promise.resolve().then( async () => {
let id = setTimeout(() => {})
let ended = false
let failures = []
const t = {
equals(a,b, assertion=''){
let message =
(a == b ? 'ok ' : 'not ok')
+ ++tape.i
+ ' ' + assertion
if( a != b) failures.push({ assertion, a, b })
// eslint-disable-next-line no-undef
console.error(message)
}
,doesNotThrow(f, assertion){
let threw;
try {
f()
} catch (e) {
threw = e
console.error(e)
}
let message =
(!threw ? 'ok ' : 'not ok')
+ ++tape.i
+ ' ' + assertion
if( threw ) failures.push({ assertion, threw })
console.error(message)
}
,end(){
ended = true
}
}
t.equal = t.equals
for( let test of tape.tests ) {
ended = false
console.error('# ' + test.name)
await test.visitor(t)
if( !ended ) {
throw new Error('Test exited without ending')
}
}
if( failures.length ) {
process.exit(1)
}
clearTimeout(id)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment