-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oleksandr Denysenko
committed
Apr 11, 2022
1 parent
a8b6a51
commit 9b3c44b
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"esversion": 11 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Primitive test engine for JavaScript | ||
|
||
Example `./tester.test.js` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); |