beartest-js 3.1.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/beartest.cjs +80 -39
- package/index.cjs +1 -3
- package/index.d.ts +10 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,10 +27,10 @@ _Beartest_ implements the following functions `describe`, `it`, `beforeAll`, `be
|
|
|
27
27
|
### Example
|
|
28
28
|
|
|
29
29
|
```javascript
|
|
30
|
-
import describe from 'beartest-js';
|
|
30
|
+
import { describe, it } from 'beartest-js';
|
|
31
31
|
import assert from 'assert';
|
|
32
32
|
|
|
33
|
-
describe('Math Testing', (
|
|
33
|
+
describe('Math Testing', () => {
|
|
34
34
|
it('should add correctly', async () => {
|
|
35
35
|
assert.strictEqual(1 + 2, 3);
|
|
36
36
|
});
|
|
@@ -47,4 +47,4 @@ Additionally, a very basic test runner is included. This test runner accepts a g
|
|
|
47
47
|
|
|
48
48
|
## License
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
Licensed under [MIT](./LICENSE).
|
package/beartest.cjs
CHANGED
|
@@ -1,52 +1,93 @@
|
|
|
1
1
|
const rgb = require('barecolor');
|
|
2
2
|
|
|
3
|
-
async function
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
async function RunSerially(fnArray) {
|
|
4
|
+
for (const fn of fnArray) {
|
|
5
|
+
await fn();
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const suiteStack = [];
|
|
9
|
+
const top = () =>
|
|
10
|
+
suiteStack.length ? suiteStack[suiteStack.length - 1] : undefined;
|
|
11
|
+
|
|
12
|
+
const registerTest = (suite, name, fn) => async () => {
|
|
13
|
+
const prefix = ' '.repeat(suite.depth);
|
|
14
|
+
try {
|
|
15
|
+
await suite.beforeEach();
|
|
16
|
+
await fn();
|
|
17
|
+
rgb.green(prefix + `✓`);
|
|
18
|
+
rgb.gray(` ${name}\n`);
|
|
19
|
+
} catch (e) {
|
|
20
|
+
rgb.red(`\n${prefix}✗ ${name} \n\n`);
|
|
21
|
+
throw e;
|
|
22
|
+
} finally {
|
|
23
|
+
await suite.afterEach();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
13
26
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
27
|
+
async function runSuite(suite) {
|
|
28
|
+
const prefix = ' '.repeat(suite.depth);
|
|
29
|
+
const tests = suite.only.length > 0 ? suite.only : suite.tests;
|
|
30
|
+
rgb.cyanln(prefix + suite.headline + ' ');
|
|
31
|
+
try {
|
|
32
|
+
await suite.beforeAll();
|
|
33
|
+
await RunSerially(tests);
|
|
34
|
+
} finally {
|
|
35
|
+
await suite.afterAll();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function suite(headline, fn, only = false) {
|
|
40
|
+
const parent = top();
|
|
41
|
+
const self = {
|
|
42
|
+
depth: parent ? parent.depth + 1 : 0,
|
|
43
|
+
headline,
|
|
44
|
+
beforeAllHooks: [],
|
|
45
|
+
async beforeAll() {
|
|
46
|
+
await RunSerially(this.beforeAllHooks);
|
|
18
47
|
},
|
|
19
|
-
|
|
20
|
-
|
|
48
|
+
beforeEachHooks: [],
|
|
49
|
+
async beforeEach() {
|
|
50
|
+
if (parent) await parent.beforeEach();
|
|
51
|
+
await RunSerially(this.beforeEachHooks);
|
|
21
52
|
},
|
|
22
|
-
|
|
23
|
-
|
|
53
|
+
afterAllHooks: [],
|
|
54
|
+
async afterAll() {
|
|
55
|
+
await RunSerially(this.afterAllHooks);
|
|
24
56
|
},
|
|
25
|
-
|
|
26
|
-
|
|
57
|
+
afterEachHooks: [],
|
|
58
|
+
async afterEach() {
|
|
59
|
+
await RunSerially(this.afterEachHooks);
|
|
60
|
+
if (parent) await parent.afterEach();
|
|
27
61
|
},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
rgb.green(`✓`);
|
|
39
|
-
rgb.gray(` ${test.name}\n`);
|
|
40
|
-
} catch (e) {
|
|
41
|
-
rgb.red(`\n✗ ${test.name} \n\n`);
|
|
42
|
-
throw e;
|
|
43
|
-
} finally {
|
|
44
|
-
await afterEach();
|
|
45
|
-
}
|
|
62
|
+
tests: [],
|
|
63
|
+
only: [],
|
|
64
|
+
};
|
|
65
|
+
if (parent && only) parent.only.push(() => runSuite(self));
|
|
66
|
+
if (parent && !only) parent.tests.push(() => runSuite(self));
|
|
67
|
+
suiteStack.push(self);
|
|
68
|
+
fn();
|
|
69
|
+
suiteStack.pop();
|
|
70
|
+
if (self.depth === 0) {
|
|
71
|
+
await runSuite(self);
|
|
46
72
|
}
|
|
47
|
-
await afterAll();
|
|
48
73
|
}
|
|
49
74
|
|
|
75
|
+
const describe = (headline, fn) => suite(headline, fn);
|
|
76
|
+
describe.skip = () => {};
|
|
77
|
+
describe.only = (headline, fn) => suite(headline, fn, true);
|
|
78
|
+
const it = (name, fn) => top().tests.push(registerTest(top(), name, fn));
|
|
79
|
+
it.only = (name, fn) => top().only.push(registerTest(top(), name, fn));
|
|
80
|
+
it.skip = () => {};
|
|
81
|
+
const beforeAll = (fn) => top().beforeAllHooks.push(fn);
|
|
82
|
+
const afterAll = (fn) => top().afterAllHooks.push(fn);
|
|
83
|
+
const beforeEach = (fn) => top().beforeEachHooks.push(fn);
|
|
84
|
+
const afterEach = (fn) => top().afterEachHooks.push(fn);
|
|
85
|
+
|
|
50
86
|
module.exports = {
|
|
51
87
|
describe,
|
|
88
|
+
it,
|
|
89
|
+
beforeAll,
|
|
90
|
+
afterAll,
|
|
91
|
+
beforeEach,
|
|
92
|
+
afterEach,
|
|
52
93
|
};
|
package/index.cjs
CHANGED
package/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
type test = (name: string, fn: () => void | Promise<void>) => void
|
|
1
|
+
type test = (name: string, fn: () => void | Promise<void>) => void;
|
|
2
2
|
|
|
3
|
-
declare function describe(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
beforeEach: (fn: () => void | Promise<void>) => void
|
|
8
|
-
afterEach: (fn: () => void | Promise<void>) => void
|
|
9
|
-
}) => void): Promise<boolean>
|
|
3
|
+
export declare function describe(
|
|
4
|
+
headline: string,
|
|
5
|
+
fn: () => void
|
|
6
|
+
): Promise<boolean>;
|
|
10
7
|
|
|
11
|
-
export
|
|
8
|
+
export declare const it: test & { only: test; skip: test };
|
|
9
|
+
export declare function beforeAll(fn: () => void | Promise<void>): void;
|
|
10
|
+
export declare function afterAll(fn: () => void | Promise<void>): void;
|
|
11
|
+
export declare function beforeEach(fn: () => void | Promise<void>): void;
|
|
12
|
+
export declare function afterEach(fn: () => void | Promise<void>): void;
|