diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1bd7226 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +*.swp diff --git a/README.md b/README.md new file mode 100644 index 0000000..797c6a3 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# js-test + +`js-test` will be a minimal JavaScript test runner. It will be able to run tests from the CLI or programatically. The aim of this library is to enable JavaScript developers to write test code in very plain JavaScript without needing to learn a whole testing framework. The test code written to be run by this package will look largely like plain JavaScript. The aim will be to enforce very little opinions on the developer. + +This project is still under development. I am still making some fundamental architecture decisions. The code in this repo will mostly be my scratch attempts at coming up with something usable. diff --git a/SumTestSuite.js b/SumTestSuite.js new file mode 100644 index 0000000..249f535 --- /dev/null +++ b/SumTestSuite.js @@ -0,0 +1,40 @@ +'use strict'; + +const assert = require('assert'); +const { sum } = require('./sum'); + +class SumTestSuite { + testSumCorrectness() { + const a = 1; + const b = 2; + const result = sum(a, b); + assert(result === a + b, 'the result should be the sum of its arguments'); + } + + testSumNoArgs() { + try { + sum(); + } catch { + return; + } + assert.fail('should fail if not provided any arguments'); + } + + testSumMissingArg() { + const a = 1; + try { + sum(a); + } catch { + return; + } + assert.fail('should fail if not provided all arguments'); + } + + testWillFail() { + assert.fail('I have failed!'); + } +} + +module.exports = { + SumTestSuite, +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..fb9854f --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "js-test", + "version": "0.0.1", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "BraydonKains", + "license": "MIT" +} diff --git a/runner.js b/runner.js new file mode 100644 index 0000000..b4e52d7 --- /dev/null +++ b/runner.js @@ -0,0 +1,53 @@ +'use strict'; + +const tests = require('./sum.test'); +const { SumTestSuite } = require('./SumTestSuite'); + +function runTestFile() { + const results = {}; + + Object.entries(tests).forEach( + ([_, testData]) => { + console.log(testData.description); + try { + testData.test(); + console.log('SUCCESS'); + + // FIXME no + results[testData.description] = null; + } catch (err) { + console.log('FAILED'); + console.log(err.message); + results[testData.description] = err; + } + } + ); + + return results; +} + +function runTestSuite() { + const suite = new SumTestSuite(); + const suiteProto = Object.getPrototypeOf(suite) + const testMethodNames = + Object.getOwnPropertyNames(suiteProto).filter( + prop => + typeof suite[prop] === 'function' && + prop.startsWith('test') + ); + + testMethodNames.forEach( + testMethodName => { + console.log(testMethodName); + try { + suite[testMethodName](); + console.log('SUCCESS'); + } catch (err) { + console.log('FAILED'); + console.log(err.message); + } + } + ); +} + +runTestSuite(); diff --git a/sum.js b/sum.js new file mode 100644 index 0000000..3ed25b9 --- /dev/null +++ b/sum.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports.sum = (a, b) => { + if (!a || !b) + throw Error('gimme a number'); + + return a + b; +} diff --git a/sum.test.js b/sum.test.js new file mode 100644 index 0000000..69cffb9 --- /dev/null +++ b/sum.test.js @@ -0,0 +1,58 @@ +'use strict'; + +const assert = require('assert'); +const { sum } = require('./sum'); + +const test = (description, testProcedure) => ({ + description, + test: testProcedure, +}); + +const testSumCorrectness = test( + '#sum should return the sum of its arguments', + () => { + const a = 1; + const b = 2; + const result = sum(a, b); + assert(result === a + b, 'the result should be the sum of its arguments'); + } +); + +const testSumNoArgs = test( + '#sum should throw an error if no arguments are passed', + () => { + try { + sum(); + } catch { + return; + } + assert.fail('should fail if not provided any arguments'); + } +); + +const testSumMissingArg = test( + '#sum should throw an error if no arguments are passed', + () => { + const a = 1; + try { + sum(a); + } catch { + return; + } + assert.fail('should fail if not provided all arguments'); + } +); + +const testWillFail = test( + '#sum will fail against its will', + () => { + assert.fail('I have failed!'); + } +); + +module.exports = { + testSumCorrectness, + testSumNoArgs, + testSumMissingArg, + testWillFail, +};