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 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', ({ it }) => {
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
- Copyright 2020 OpenJS Foundation and contributors. Licensed under [MIT](./LICENSE).
50
+ Licensed under [MIT](./LICENSE).
package/beartest.cjs CHANGED
@@ -1,52 +1,93 @@
1
1
  const rgb = require('barecolor');
2
2
 
3
- async function describe(headline, fn) {
4
- const suite = [];
5
- const only = [];
6
- let beforeAll = async () => {};
7
- let afterAll = async () => {};
8
- let beforeEach = async () => {};
9
- let afterEach = async () => {};
10
- const it = (name, fn) => suite.push({ name: name, fn: fn });
11
- it.only = (name, fn) => only.push({ name: name, fn: fn });
12
- it.skip = () => {};
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
- fn({
15
- it: it,
16
- beforeAll: (fn) => {
17
- beforeAll = fn;
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
- afterAll: (fn) => {
20
- afterAll = fn;
48
+ beforeEachHooks: [],
49
+ async beforeEach() {
50
+ if (parent) await parent.beforeEach();
51
+ await RunSerially(this.beforeEachHooks);
21
52
  },
22
- beforeEach: (fn) => {
23
- beforeEach = fn;
53
+ afterAllHooks: [],
54
+ async afterAll() {
55
+ await RunSerially(this.afterAllHooks);
24
56
  },
25
- afterEach: (fn) => {
26
- afterEach = fn;
57
+ afterEachHooks: [],
58
+ async afterEach() {
59
+ await RunSerially(this.afterEachHooks);
60
+ if (parent) await parent.afterEach();
27
61
  },
28
- });
29
-
30
- const tests = only.length > 0 ? only : suite;
31
- rgb.cyanln(headline + ' ');
32
-
33
- await beforeAll();
34
- for (const test of tests) {
35
- try {
36
- await beforeEach();
37
- await test.fn();
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
@@ -1,3 +1 @@
1
- const { describe } = require('./beartest.cjs');
2
-
3
- module.exports = describe;
1
+ module.exports = require('./beartest.cjs');
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(headline: string, fn: (args: {
4
- it: test & { only: test, skip: test }
5
- beforeAll: (fn: () => void | Promise<void>) => void
6
- afterAll: (fn: () => void | Promise<void>) => void
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 = describe
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beartest-js",
3
- "version": "3.1.1",
3
+ "version": "4.0.0",
4
4
  "description": "Bear Bones Testing",
5
5
  "repository": {
6
6
  "type": "git",