First draft with cli that runs all *.test.js files
parent
6ef8b5e6ad
commit
ab01ce3b44
@ -0,0 +1 @@
|
||||
.eslintrc.cjs
|
@ -0,0 +1,22 @@
|
||||
const OFF = 0;
|
||||
const WARN = 1;
|
||||
const ERR = 2;
|
||||
|
||||
module.exports = {
|
||||
"env": {
|
||||
"es2021": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 12,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"rules": {
|
||||
"indent": [ERR, 4],
|
||||
"linebreak-style": [ERR, "unix"],
|
||||
"quotes": [ERR, "double"],
|
||||
"semi": [ERR, "never"],
|
||||
"comma-dangle": [ERR, "always-multiline"],
|
||||
},
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
'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,21 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { discoverTestFiles } from "./src/discover.js"
|
||||
import { runTests } from "./src/suites.js"
|
||||
|
||||
async function main() {
|
||||
const testFilePaths = await discoverTestFiles()
|
||||
|
||||
for (const filePath of testFilePaths) {
|
||||
const filePathParts = filePath.split("/")
|
||||
const fileName = filePathParts[filePathParts.length - 1]
|
||||
const suite = await import(`./${filePath}`)
|
||||
|
||||
console.log(`Running suite ${fileName}`)
|
||||
console.log("-------------------------")
|
||||
runTests(suite)
|
||||
console.log("-------------------------")
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
@ -0,0 +1,12 @@
|
||||
"use strict"
|
||||
|
||||
function sum(a, b) {
|
||||
if (!a || !b)
|
||||
throw Error("gimme a number")
|
||||
|
||||
return a + b
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sum,
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
"use strict"
|
||||
|
||||
const assert = require("assert")
|
||||
const { sum } = require("./sum.cjs")
|
||||
|
||||
function testSumResult() {
|
||||
const a = 1
|
||||
const b = 2
|
||||
const result = sum(a, b)
|
||||
assert(result === a + b, "the result should be the sum of its arguments")
|
||||
}
|
||||
|
||||
function testSumNoArgs() {
|
||||
try {
|
||||
sum()
|
||||
} catch (_) {
|
||||
return
|
||||
}
|
||||
assert.fail("should fail if not provided any arguments")
|
||||
}
|
||||
|
||||
function testSumMissingArg() {
|
||||
const a = 1
|
||||
try {
|
||||
sum(a)
|
||||
} catch (_) {
|
||||
return
|
||||
}
|
||||
assert.fail("should fail if not provided all arguments")
|
||||
}
|
||||
|
||||
function testWillFail() {
|
||||
assert.fail("I have failed!")
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
testSumResult,
|
||||
testSumNoArgs,
|
||||
testSumMissingArg,
|
||||
testWillFail,
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
export function sum(a, b) {
|
||||
if (!a || !b)
|
||||
throw Error("gimme a number")
|
||||
|
||||
return a + b
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
import { strict as assert } from "assert"
|
||||
|
||||
import { sum } from "./sum.js"
|
||||
|
||||
export function testSumResult() {
|
||||
const a = 1
|
||||
const b = 2
|
||||
const result = sum(a, b)
|
||||
assert(result === a + b, "the result should be the sum of its arguments")
|
||||
}
|
||||
|
||||
export function testSumNoArgs() {
|
||||
try {
|
||||
sum()
|
||||
} catch (_) {
|
||||
return
|
||||
}
|
||||
assert.fail("should fail if not provided any arguments")
|
||||
}
|
||||
|
||||
export function testSumMissingArg() {
|
||||
const a = 1
|
||||
try {
|
||||
sum(a)
|
||||
} catch (_) {
|
||||
return
|
||||
}
|
||||
assert.fail("should fail if not provided all arguments")
|
||||
}
|
||||
|
||||
export function testWillFail() {
|
||||
assert.fail("I have failed!")
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
export function sum(a, b) {
|
||||
if (!a || !b)
|
||||
throw Error("gimme a number")
|
||||
|
||||
return a + b
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
import { runTests } from "./src/suites.js"
|
||||
import { discoverTestFiles } from "./src/discover.js"
|
||||
|
||||
export {
|
||||
runTests,
|
||||
discoverTestFiles,
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,24 @@
|
||||
{
|
||||
"name": "js-test",
|
||||
"version": "0.0.1",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "BraydonKains",
|
||||
"license": "MIT"
|
||||
}
|
||||
{
|
||||
"name": "js-test",
|
||||
"version": "0.0.1",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint --fix ."
|
||||
},
|
||||
"bin": {
|
||||
"js-test": "./cli.js"
|
||||
},
|
||||
"author": {
|
||||
"name": "BraydonKains",
|
||||
"github": "https://github.com/BraydonKains"
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"eslint": "^7.29.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"tiny-glob": "^0.2.9"
|
||||
}
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
'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,12 @@
|
||||
import { default as glob } from "tiny-glob"
|
||||
|
||||
export async function discoverTestFiles(options) {
|
||||
const pattern = options?.pattern || "**/*.test.{js,cjs,mjs}"
|
||||
const directory = options?.directory || "."
|
||||
|
||||
const files = await glob(pattern, {
|
||||
cwd: directory,
|
||||
})
|
||||
const importUrls = files.map(f => f.replace("\\", "/")) // Silly Windows!
|
||||
return importUrls
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import { getMethodNames } from "./util.js"
|
||||
|
||||
export function runTests(suite) {
|
||||
const testMethodNames = getMethodNames(suite)
|
||||
|
||||
testMethodNames.forEach(
|
||||
testMethodName => {
|
||||
console.log(testMethodName)
|
||||
try {
|
||||
suite[testMethodName]()
|
||||
console.log("SUCCESS")
|
||||
} catch (err) {
|
||||
console.log("FAILED")
|
||||
console.log(err.message)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
export const getMethodNames = (obj) =>
|
||||
Object.getOwnPropertyNames(obj).filter(
|
||||
prop =>
|
||||
prop.startsWith("test") &&
|
||||
typeof obj[prop] === "function",
|
||||
)
|
@ -1,8 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports.sum = (a, b) => {
|
||||
if (!a || !b)
|
||||
throw Error('gimme a number');
|
||||
|
||||
return a + b;
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
'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