Skip to content

Commit eaf6060

Browse files
committed
Add src
1 parent a981463 commit eaf6060

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

src/CLI.ts

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

src/Tester.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* *
2+
*
3+
* Imports
4+
*
5+
* */
6+
7+
import * as Assert from 'assert';
8+
9+
/* *
10+
*
11+
* Class
12+
*
13+
* */
14+
15+
export class Tester<T = unknown> {
16+
17+
/* *
18+
*
19+
* Static Properties
20+
*
21+
* */
22+
23+
public static readonly defaultSession = new Tester(Assert);
24+
25+
/* *
26+
*
27+
* Constructor
28+
*
29+
* */
30+
31+
public constructor(
32+
assert: T
33+
) {
34+
this.assert = assert;
35+
}
36+
37+
/* *
38+
*
39+
* Properties
40+
*
41+
* */
42+
43+
public readonly assert: T;
44+
45+
public readonly errors: Array<[string, unknown]> = [];
46+
47+
public readonly successes: Array<string> = [];
48+
49+
public readonly tests: Array<[string, Function]> = [];
50+
51+
/* *
52+
*
53+
* Functions
54+
*
55+
* */
56+
57+
public start(
58+
verbose?: boolean
59+
): void {
60+
const errors = this.errors;
61+
const successes = this.successes;
62+
63+
let result: unknown;
64+
let testCode: Function;
65+
let testCounter = 0;
66+
let title: string;
67+
68+
for (const test of this.tests) {
69+
testCode = test[1];
70+
title = `#${++testCounter}: ${test[0]}`;
71+
72+
try {
73+
result = testCode();
74+
75+
if (result instanceof Promise) {
76+
result
77+
.then(() => successes.push(title))
78+
.catch((error) => errors.push([title, error]));
79+
}
80+
else {
81+
successes.push(title);
82+
}
83+
}
84+
catch (error) {
85+
errors.push([title, error]);
86+
}
87+
}
88+
89+
if (!verbose) {
90+
return;
91+
}
92+
93+
for (const success of successes) {
94+
console.log('✅', success);
95+
}
96+
97+
for (const error of errors) {
98+
console.log('🛑', error[0]);
99+
console.error(error[1]);
100+
}
101+
}
102+
103+
public test(
104+
description: string,
105+
testCode: (assert: T) => unknown
106+
): void {
107+
this.tests.push([description, testCode]);
108+
}
109+
110+
}
111+
112+
/* *
113+
*
114+
* Default Export
115+
*
116+
* */
117+
118+
export default Tester;

src/index.ts

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

src/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"esModuleInterop": true,
6+
"outDir": "../lib/"
7+
}
8+
}

tst/Tester.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)