Scratch code for running a test file or a test suite
parent
64e51403c8
commit
6ef8b5e6ad
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
*.swp
|
@ -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.
|
@ -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,
|
||||
}
|
@ -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"
|
||||
}
|
@ -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();
|
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
module.exports.sum = (a, b) => {
|
||||
if (!a || !b)
|
||||
throw Error('gimme a number');
|
||||
|
||||
return a + b;
|
||||
}
|
@ -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,
|
||||
};
|
Loading…
Reference in New Issue