Created
August 20, 2018 11:40
-
-
Save ryasmi/03adb35e561e81a48af99e6d87c79dd2 to your computer and use it in GitHub Desktop.
Starting a potential test runner that doesn't provide an opinionated assertion 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
createRunner = () => { | |
const beforeAlls = []; | |
const afterAlls = []; | |
const beforeEaches = []; | |
const afterEaches = []; | |
const tests = []; | |
const test = (description, process) => { | |
tests.push({ | |
description, | |
process, | |
hasPassed: false, | |
hasExecuted: false, | |
}); | |
}; | |
const beforeEach = (process) => { | |
beforeEaches.push({process}); | |
}; | |
const afterEach = (process) => { | |
afterEaches.push({process}); | |
}; | |
const beforeAll = (process) => { | |
beforeAlls.push({process}); | |
}; | |
const afterAll = (process) => { | |
afterAlls.push({process}); | |
}; | |
const executeProcess = async (process) => { | |
const result = process(); | |
if (result instanceof Promise) { | |
await result; | |
} | |
}; | |
const runTest = async (test) => { | |
await Promise.all(beforeEaches.map(async (beforeEach) => { | |
await executeProcess(beforeEach.process); | |
})); | |
try { | |
await executeProcess(test.process); | |
test.hasPassed = true; | |
} catch (err) { | |
test.hasPassed === false; | |
} | |
await Promise.all(afterEaches.map(async (afterEach) => { | |
await executeProcess(afterEach.process); | |
})); | |
}; | |
const runTests = async () => { | |
console.log('Starting tests'); | |
await Promise.all(beforeAlls.map(async (beforeAll) => { | |
await executeProcess(beforeAll.process); | |
})); | |
await Promise.all(tests.map(async (test) => { | |
await runTest(test); | |
const status = test.hasPassed === true ? 'PASS' : 'FAIL'; | |
console.log(`${status} - ${test.description}`); | |
})); | |
await Promise.all(afterAlls.map(async (afterAll) => { | |
await executeProcess(afterAll.process); | |
})); | |
const passedTests = tests.filter((test) => test.hasPassed); | |
console.log('\n'); | |
console.log(`Passed ${passedTests.length}/${tests.length}`); | |
}; | |
return { test, beforeAll, afterAll, beforeEach, afterEach, runTests }; | |
}; | |
runMyTests = () => { | |
const runner = createRunner(); | |
runner.test('hello', () => { throw 'error'; }); | |
runner.runTests(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment