Skip to content

Commit 7af78d0

Browse files
committed
Add lib
1 parent 2123b96 commit 7af78d0

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

lib/CLI.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

lib/CLI.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { hideBin } from 'yargs/helpers';
2+
import Yargs from 'yargs';
3+
Yargs(hideBin(process.argv))
4+
.command('* [testfolder]', false, function (yargs) {
5+
return yargs.positional('testfolder', {
6+
default: 'tests/',
7+
description: 'Path to the tests'
8+
});
9+
}, function (argv) {
10+
console.log('CLI', argv);
11+
})
12+
.parse();

lib/Tester.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference types="node" />
2+
import * as Assert from 'assert';
3+
export declare class Tester<T = unknown> {
4+
static readonly defaultSession: Tester<typeof Assert>;
5+
constructor(assert: T);
6+
readonly assert: T;
7+
readonly errors: Array<[string, unknown]>;
8+
readonly successes: Array<string>;
9+
readonly tests: Array<[string, Function]>;
10+
start(verbose?: boolean): void;
11+
test(description: string, testCode: (assert: T) => unknown): void;
12+
}
13+
export default Tester;

lib/Tester.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* *
2+
*
3+
* Imports
4+
*
5+
* */
6+
import * as Assert from 'assert';
7+
/* *
8+
*
9+
* Class
10+
*
11+
* */
12+
export class Tester {
13+
/* *
14+
*
15+
* Constructor
16+
*
17+
* */
18+
constructor(assert) {
19+
this.errors = [];
20+
this.successes = [];
21+
this.tests = [];
22+
this.assert = assert;
23+
}
24+
/* *
25+
*
26+
* Functions
27+
*
28+
* */
29+
start(verbose) {
30+
const errors = this.errors;
31+
const successes = this.successes;
32+
let result;
33+
let testCode;
34+
let testCounter = 0;
35+
let title;
36+
for (const test of this.tests) {
37+
testCode = test[1];
38+
title = `#${++testCounter}: ${test[0]}`;
39+
try {
40+
result = testCode();
41+
if (result instanceof Promise) {
42+
result
43+
.then(() => successes.push(title))
44+
.catch((error) => errors.push([title, error]));
45+
}
46+
else {
47+
successes.push(title);
48+
}
49+
}
50+
catch (error) {
51+
errors.push([title, error]);
52+
}
53+
}
54+
if (!verbose) {
55+
return;
56+
}
57+
for (const success of successes) {
58+
console.log('✅', success);
59+
}
60+
for (const error of errors) {
61+
console.log('🛑', error[0]);
62+
console.error(error[1]);
63+
}
64+
}
65+
test(description, testCode) {
66+
this.tests.push([description, testCode]);
67+
}
68+
}
69+
/* *
70+
*
71+
* Static Properties
72+
*
73+
* */
74+
Tester.defaultSession = new Tester(Assert);
75+
/* *
76+
*
77+
* Default Export
78+
*
79+
* */
80+
export default Tester;

lib/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Tester from './Tester.js';
2+
export declare const test: typeof Tester.defaultSession.test;
3+
export default test;
4+
export * from './Tester.js';

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Tester from './Tester.js';
2+
export const test = Tester.defaultSession.test.bind(Tester.defaultSession);
3+
export default test;
4+
export * from './Tester.js';

0 commit comments

Comments
 (0)