Skip to content

Commit

Permalink
Add source files
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Denysenko committed Apr 11, 2022
1 parent a8b6a51 commit 9b3c44b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 11
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Primitive test engine for JavaScript

Example `./tester.test.js`
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "js-test-tools",
"version": "1.0.0",
"description": "Primitive test tools for JavaScript",
"type": "module",
"main": "test-runner.js",
"scripts": {
"test": "node tester.test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/abyr/js-test-tools.git"
},
"keywords": [
"js",
"unit",
"test"
],
"author": "Oleksandr Denysenko",
"license": "MIT",
"bugs": {
"url": "https://github.com/abyr/js-test-tools/issues"
},
"homepage": "https://github.com/abyr/js-test-tools#readme"
}
60 changes: 60 additions & 0 deletions test-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as path from "path";
import * as util from "util";

function group(name, tests) {
const relativeFilenameOrName = relativeFilename(name);

console.log(infoStr(relativeFilenameOrName));
console.group();
tests();
console.groupEnd();
}

const test = function (title, fn) {
const end = result(title);

try {
const fnRes = fn();

if (util.types.isPromise(fnRes)) {
fnRes.then(end).catch(end);
} else {
end();
}
} catch (err) {
end(err);
}
};

const result = title => err => {
if (err instanceof Error) {
console.log(``);
console.log(errorStr(`✖ ${title}`));
console.error(err.actual || err);
} else {
console.log(successStr(`✔ ${title}`));
}
};

function relativeFilename(filename) {
return path.relative(process.cwd(), filename);
}

const STR_GREEN = "\x1b[32m";
const STR_RED = "\x1b[31m";
const STR_YELLOW = "\x1b[33m";
const STR_RESET = "\x1b[0m";

function successStr(text) {
return "".concat(STR_GREEN).concat(text).concat(STR_RESET);
}

function infoStr(text) {
return "".concat(STR_YELLOW).concat(text).concat(STR_RESET);
}

function errorStr(text) {
return "".concat(STR_RED).concat(text).concat(STR_RESET);
}

export { group, test };
20 changes: 20 additions & 0 deletions tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const tester = {

fail: function(msg) {
throw new Error('fail(): ' + msg);
},

assert: function(value, msg) {
if (!value) {
throw new Error('assert(): ' + msg);
}
},

equals: function(expected, actual) {
if (expected !== actual) {
throw new Error('assertEquals() "' + expected + '" not equals "' + actual + '"');
}
}
};

export default tester;
19 changes: 19 additions & 0 deletions tester.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { fileURLToPath } from 'url';
import { group, test } from './test-runner.js';
import tester from './tester.js';

const filePath = fileURLToPath(import.meta.url);

export default group(`${filePath}: self test`, () => {
test('assert', () => {
tester.assert(1);
tester.assert(1 == '1');
tester.assert(true);
tester.assert(!false);
});
test('equals', () => {
tester.equals('1', '1');
tester.equals(true, !!1);
tester.equals(1, Number('1'));
});
});

0 comments on commit 9b3c44b

Please sign in to comment.