beartest-js 3.1.0 → 5.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
@@ -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 Mocha. It has a tiny footprint while maintaining a familiar API.
3
+ _Beartest_ is an extremely simple JavaScript test runner inspired by Baretest and Jest. It has a tiny footprint while maintaining a familiar API.
4
4
 
5
5
  ### Install
6
6
 
@@ -14,27 +14,45 @@ yarn add beartest-js -D
14
14
 
15
15
  ### Why Beartest?
16
16
 
17
- Mocha, Jest, and similar testing frameworks are richly featured, broadly compatible, and highly customizable. Beartest is none of those things.
18
- **If you want features look somewhere else.** Beartest is meant to be understandable above all else. Inspired by baretest, it seeks to deliver an
19
- API similar to Mocha's with minimal code. **Note:** Beartest is only compatible with Node 14.
17
+ Jest, Mocha, and similar testing frameworks are richly featured, broadly compatible, and highly customizable. Beartest is none of those things. **If you want features look somewhere else.** Beartest is meant to be simple and understandable, without the complexity of other testing frameworks. Inspired by [Baretest](https://www.npmjs.com/package/baretest), it seeks to deliver an API similar to Jest's with minimal code.
18
+
19
+ ### Compatibility
20
+
21
+ The Beartest test runner uses common js to load files.
20
22
 
21
23
  ### Usage
22
24
 
23
- _Beartest_ implements the basic test organization provided by Mocha. The `beforeAll`, `beforeEach`, `afterEach`, and `afterAll` ordering functions are made available as well as `it.skip` and `it.only`. Additionally, a very basic test runner is included. This test runner accepts a glob pattern as a command line argument. The test runner can be invoked with `yarn beartest sum.test.js`.
25
+ _Beartest_ implements the following functions `describe`, `it`, `beforeAll`, `beforeEach`, `afterEach`, `afterAll`, `it.skip`, and `it.only`. All provided functions work in a similar way as the corresponding functions in Jest.
24
26
 
25
- ### Example Test
27
+ ### Example
26
28
 
27
29
  ```javascript
28
- import describe from 'beartest-js';
29
- import assert from 'assert';
30
+ import { describe, it } from "beartest-js";
31
+ import assert from "assert";
30
32
 
31
- export default describe('Sum', ({ it }) => {
32
- it('should add correctly', async () => {
33
+ describe("Math Testing", () => {
34
+ it("should add correctly", async () => {
33
35
  assert.strictEqual(1 + 2, 3);
34
36
  });
37
+
38
+ it("should subtract correctly", async () => {
39
+ assert.strictEqual(3 - 2, 1);
40
+ });
35
41
  });
36
42
  ```
37
43
 
44
+ ### Running Tests
45
+
46
+ Additionally, a very basic test runner is included. This test runner accepts a glob pattern as a command line argument. The test runner can be invoked with `yarn beartest "glob-pattern"`. By default, it will look for `**/*.test.js`.
47
+
48
+ Suggested package script:
49
+
50
+ ```json
51
+ "scripts": {
52
+ "test": "beartest"
53
+ }
54
+ ```
55
+
38
56
  ## License
39
57
 
40
- Copyright 2020 OpenJS Foundation and contributors. Licensed under [MIT](./LICENSE).
58
+ Licensed under [MIT](./LICENSE).
package/beartest.js ADDED
@@ -0,0 +1,93 @@
1
+ const rgb = require('barecolor');
2
+
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
+ };
26
+
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);
47
+ },
48
+ beforeEachHooks: [],
49
+ async beforeEach() {
50
+ if (parent) await parent.beforeEach();
51
+ await RunSerially(this.beforeEachHooks);
52
+ },
53
+ afterAllHooks: [],
54
+ async afterAll() {
55
+ await RunSerially(this.afterAllHooks);
56
+ },
57
+ afterEachHooks: [],
58
+ async afterEach() {
59
+ await RunSerially(this.afterEachHooks);
60
+ if (parent) await parent.afterEach();
61
+ },
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);
72
+ }
73
+ }
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
+
86
+ module.exports = {
87
+ describe,
88
+ it,
89
+ beforeAll,
90
+ afterAll,
91
+ beforeEach,
92
+ afterEach,
93
+ };
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/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./beartest.cjs');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beartest-js",
3
- "version": "3.1.0",
3
+ "version": "5.0.0",
4
4
  "description": "Bear Bones Testing",
5
5
  "repository": {
6
6
  "type": "git",
package/testrunner.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import beartest from './beartest.cjs';
3
- import glob from 'tiny-glob';
2
+ const beartest = require("./beartest");
3
+ const glob = require("tiny-glob");
4
4
 
5
5
  const tests = [];
6
6
  const describe = beartest.describe;
@@ -8,10 +8,10 @@ beartest.describe = (headline, fn) => tests.push(describe(headline, fn));
8
8
 
9
9
  async function runTests() {
10
10
  try {
11
- const globStr = process.argv[2] || '**/*.test.*';
11
+ const globStr = process.argv[2] || "**/*.test.*";
12
12
  const files = await glob(globStr, { absolute: true });
13
13
  for (const file of files) {
14
- await import(file);
14
+ require(file);
15
15
  await Promise.all(tests);
16
16
  }
17
17
  process.exit(0);
package/beartest.cjs DELETED
@@ -1,52 +0,0 @@
1
- const rgb = require('barecolor');
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 = () => {};
13
-
14
- fn({
15
- it: it,
16
- beforeAll: (fn) => {
17
- beforeAll = fn;
18
- },
19
- afterAll: (fn) => {
20
- afterAll = fn;
21
- },
22
- beforeEach: (fn) => {
23
- beforeEach = fn;
24
- },
25
- afterEach: (fn) => {
26
- afterEach = fn;
27
- },
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
- }
46
- }
47
- await afterAll();
48
- }
49
-
50
- module.exports = {
51
- describe,
52
- };
package/index.cjs DELETED
@@ -1,3 +0,0 @@
1
- const { describe } = require('./beartest.cjs');
2
-
3
- module.exports = describe;