beartest-js 5.0.3 → 6.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/.prettierrc ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "tabWidth": 2,
3
+ "useTabs": false,
4
+ "printWidth": 100
5
+ }
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <img src="https://beartest-js.s3.amazonaws.com/beartest-logo.png" width="400">
2
2
 
3
- _Beartest_ is an extremely simple JavaScript test runner inspired by Baretest and Jest. It has a tiny footprint while maintaining a familiar API.
3
+ _Beartest_ is an extremely simple JavaScript test runner inspired by Baretest, Playwright, and Jest. It has a tiny footprint while maintaining a familiar API.
4
4
 
5
5
  ### Install
6
6
 
@@ -27,15 +27,15 @@ _Beartest_ implements the following functions `describe`, `it`, `beforeAll`, `be
27
27
  ### Example
28
28
 
29
29
  ```javascript
30
- import { describe, it } from "beartest-js";
30
+ import { test } from "beartest-js";
31
31
  import assert from "assert";
32
32
 
33
- describe("Math Testing", () => {
34
- it("should add correctly", async () => {
33
+ test.describe("Math Testing", () => {
34
+ test("should add correctly", async () => {
35
35
  assert.strictEqual(1 + 2, 3);
36
36
  });
37
37
 
38
- it("should subtract correctly", async () => {
38
+ test("should subtract correctly", async () => {
39
39
  assert.strictEqual(3 - 2, 1);
40
40
  });
41
41
  });
package/beartest.js CHANGED
@@ -1,4 +1,4 @@
1
- const rgb = require('barecolor');
1
+ const rgb = require("barecolor");
2
2
 
3
3
  async function RunSerially(fnArray) {
4
4
  for (const fn of fnArray) {
@@ -6,11 +6,12 @@ async function RunSerially(fnArray) {
6
6
  }
7
7
  }
8
8
  const suiteStack = [];
9
- const top = () =>
10
- suiteStack.length ? suiteStack[suiteStack.length - 1] : undefined;
9
+ let testRunPromise = Promise.resolve();
10
+ const top = () => (suiteStack.length ? suiteStack[suiteStack.length - 1] : null);
11
+ const topSafe = () => (suiteStack.length ? suiteStack[suiteStack.length - 1] : makeSuite(""));
11
12
 
12
13
  const registerTest = (suite, name, fn) => async () => {
13
- const prefix = ' '.repeat(suite.depth);
14
+ const prefix = " ".repeat(suite.depth);
14
15
  try {
15
16
  await suite.beforeEach();
16
17
  await fn();
@@ -25,9 +26,9 @@ const registerTest = (suite, name, fn) => async () => {
25
26
  };
26
27
 
27
28
  async function runSuite(suite) {
28
- const prefix = ' '.repeat(suite.depth);
29
+ const prefix = " ".repeat(suite.depth);
29
30
  const tests = suite.only.length > 0 ? suite.only : suite.tests;
30
- rgb.cyanln(prefix + suite.headline + ' ');
31
+ rgb.cyanln(prefix + suite.headline + " ");
31
32
  try {
32
33
  await suite.beforeAll();
33
34
  await RunSerially(tests);
@@ -36,7 +37,7 @@ async function runSuite(suite) {
36
37
  }
37
38
  }
38
39
 
39
- async function suite(headline, fn, only = false) {
40
+ function makeSuite(headline, only = false, fn = null) {
40
41
  const parent = top();
41
42
  const self = {
42
43
  depth: parent ? parent.depth + 1 : 0,
@@ -65,29 +66,33 @@ async function suite(headline, fn, only = false) {
65
66
  if (parent && only) parent.only.push(() => runSuite(self));
66
67
  if (parent && !only) parent.tests.push(() => runSuite(self));
67
68
  suiteStack.push(self);
68
- fn();
69
- suiteStack.pop();
69
+ if (fn) fn();
70
+ if (fn) suiteStack.pop();
70
71
  if (self.depth === 0) {
71
- await runSuite(self);
72
+ const timeoutPromise = new Promise((resolve) => setTimeout(resolve, 0));
73
+ testRunPromise = timeoutPromise.then(() => runSuite(self));
72
74
  }
75
+ return self;
73
76
  }
74
77
 
75
- const describe = (headline, fn) => suite(headline, fn);
78
+ const describe = (headline, fn) => makeSuite(headline, false, fn);
76
79
  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
+ describe.only = (headline, fn) => makeSuite(headline, true, fn);
81
+ const it = (name, fn) => topSafe().tests.push(registerTest(topSafe(), name, fn));
82
+ it.only = (name, fn) => topSafe().only.push(registerTest(topSafe(), name, fn));
80
83
  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);
84
+ const beforeAll = (fn) => topSafe().beforeAllHooks.push(fn);
85
+ const afterAll = (fn) => topSafe().afterAllHooks.push(fn);
86
+ const beforeEach = (fn) => topSafe().beforeEachHooks.push(fn);
87
+ const afterEach = (fn) => topSafe().afterEachHooks.push(fn);
85
88
 
86
89
  module.exports = {
87
- describe,
88
- it,
89
- beforeAll,
90
- afterAll,
91
- beforeEach,
92
- afterEach,
90
+ test: Object.assign(it, {
91
+ describe,
92
+ beforeAll,
93
+ afterAll,
94
+ beforeEach,
95
+ afterEach,
96
+ }),
97
+ runner: { waitForTests: () => testRunPromise },
93
98
  };
package/index.d.ts CHANGED
@@ -1,4 +1,14 @@
1
- type test = (name: string, fn: () => void | Promise<void>) => void;
1
+ type testFn = (name: string, fn: () => unknown) => void;
2
+
3
+ type test = testFn & {
4
+ skip: testFn;
5
+ only: testFn;
6
+ describe: testFn & { skip: testFn; only: testFn };
7
+ beforeAll(fn: () => unknown): void;
8
+ afterAll(fn: () => unknown): void;
9
+ beforeEach(fn: () => unknown): void;
10
+ afterEach(fn: () => unknown): void;
11
+ };
2
12
 
3
13
  type Describe = (headline: string, fn: () => void) => Promise<boolean>;
4
14
  type It = test & { only: test; skip: test };
@@ -7,21 +17,7 @@ type AfterAll = (fn: () => void | Promise<void>) => void;
7
17
  type BeforeEach = (fn: () => void | Promise<void>) => void;
8
18
  type AfterEach = (fn: () => void | Promise<void>) => void;
9
19
 
10
- export declare const describe: Describe;
11
- export declare const it: It;
12
-
13
- export declare const beforeAll: BeforeAll;
14
- export declare const afterAll: AfterAll;
15
- export declare const beforeEach: BeforeEach;
16
- export declare const afterEach: AfterEach;
17
-
18
- interface BearTest {
19
- describe: Describe;
20
- it: It;
21
- beforeAll: BeforeAll;
22
- afterAll: AfterAll;
23
- beforeEach: BeforeEach;
24
- afterEach: AfterEach;
25
- }
26
-
27
- export default BearTest;
20
+ export declare const test: test;
21
+ export declare const runner: {
22
+ waitForTests: Promise<void>;
23
+ };
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "beartest-js",
3
- "version": "5.0.3",
3
+ "version": "6.0.0",
4
4
  "description": "Bear Bones Testing",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/rubber-duck-software/beartest.git"
8
8
  },
9
- "types": "index.d.ts",
10
- "main": "index.js",
9
+ "types": "./index.d.ts",
10
+ "main": "./index.js",
11
11
  "bin": {
12
12
  "beartest": "testrunner.js"
13
13
  },
14
14
  "author": "Grant Colestock",
15
15
  "license": "MIT",
16
- "type": "module",
16
+ "type": "commonjs",
17
17
  "dependencies": {
18
18
  "barecolor": "^1.0.1",
19
19
  "tiny-glob": "^0.2.8"
package/testrunner.js CHANGED
@@ -2,17 +2,13 @@
2
2
  const beartest = require("./beartest");
3
3
  const glob = require("tiny-glob");
4
4
 
5
- const tests = [];
6
- const describe = beartest.describe;
7
- beartest.describe = (headline, fn) => tests.push(describe(headline, fn));
8
-
9
5
  async function runTests() {
10
6
  try {
11
7
  const globStr = process.argv[2] || "**/*.test.*";
12
8
  const files = await glob(globStr, { absolute: true });
13
9
  for (const file of files) {
14
10
  require(file);
15
- await Promise.all(tests);
11
+ await beartest.runner.waitForTests();
16
12
  }
17
13
  process.exit(0);
18
14
  } catch (e) {