From 9b3c44b6f31e2aa221101adb310f2ddbb9c64ab6 Mon Sep 17 00:00:00 2001 From: Oleksandr Denysenko Date: Mon, 11 Apr 2022 11:20:28 +0300 Subject: [PATCH] Add source files --- .jshintrc | 3 +++ README.md | 3 +++ package.json | 25 +++++++++++++++++++++ test-runner.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ tester.js | 20 +++++++++++++++++ tester.test.js | 19 ++++++++++++++++ 6 files changed, 130 insertions(+) create mode 100644 .jshintrc create mode 100644 README.md create mode 100644 package.json create mode 100644 test-runner.js create mode 100644 tester.js create mode 100644 tester.test.js diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..18d2fcc --- /dev/null +++ b/.jshintrc @@ -0,0 +1,3 @@ +{ + "esversion": 11 +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..96de7bf --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Primitive test engine for JavaScript + +Example `./tester.test.js` diff --git a/package.json b/package.json new file mode 100644 index 0000000..38344da --- /dev/null +++ b/package.json @@ -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" +} diff --git a/test-runner.js b/test-runner.js new file mode 100644 index 0000000..e58455c --- /dev/null +++ b/test-runner.js @@ -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 }; diff --git a/tester.js b/tester.js new file mode 100644 index 0000000..9d99ec3 --- /dev/null +++ b/tester.js @@ -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; diff --git a/tester.test.js b/tester.test.js new file mode 100644 index 0000000..0734292 --- /dev/null +++ b/tester.test.js @@ -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')); + }); +});