Skip to content

Commit 4330d06

Browse files
committed
feat: Enhance Test.js with browser support, CDN, and updated docs
1 parent 9419903 commit 4330d06

25 files changed

+675
-376
lines changed

README.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,53 @@
22
[![ReadMe](https://img.shields.io/badge/ReadMe-018EF5?logo=readme&logoColor=fff)](#)
33
[![NPM](https://nodei.co/npm/@jlongyam/test.svg?style=flat&data=n,v,d&color=blue)](https://www.npmjs.com/package/@jlongyam/test)
44

5-
# Test #
5+
# Test
66

77
Simple Test utility.
88

9-
- __deepCompare__ for nested Array and Object.
9+
- **deepCompare** for nested Array and Object.
1010
- Automatically `run`.
1111

12-
### Install ###
12+
### Install
1313

1414
```shell
1515
npm i @jlongyam/test -D
1616
```
1717

18-
### Script ###
18+
### Script
1919

20-
__`test/test.js`__
20+
**`test/test.js`**
2121

2222
```js
2323
import Test from "@jlongyam/test";
2424

2525
// Only 4 keywords
2626
const { describe, it, assert } = Test();
2727

28-
describe("Test", () => { // Container
29-
it("should 5", () => { // Section
30-
assert(2+3 === 5); // True
28+
describe("Test", () => {
29+
// Container
30+
it("should 5", () => {
31+
// Section
32+
assert(2 + 3 === 5); // True
3133
});
3234
it("should 4", () => {
33-
assert(-1+5 === 5); // False
35+
assert(-1 + 5 === 5); // False
3436
});
3537
it("should 0", () => {
36-
assert(0+0 === 0);
38+
assert(0 + 0 === 0);
3739
});
3840
});
39-
4041
```
4142

42-
__`package.json`__:
43+
**`package.json`**:
4344

4445
```JSON
4546
"scripts": {
4647
"test": "node ./test/test.js"
4748
}
4849
```
4950

50-
### Terminal ###
51+
### Terminal
5152

5253
```shell
5354
npm test
@@ -56,19 +57,20 @@ npm test
5657
```shell
5758
Test
5859
✔ should 5
59-
✖ should 4 - Failed
60+
✖ should 4 - Expected false
6061
✔ should 0
6162
```
6263

63-
## Usage ##
64+
## Usage
6465

6566
How to test `Array` or `Object`:
6667

6768
Just like `console.assert`, comparing using `===` will fail,
6869

6970
There are two method:
71+
7072
- use `String(A) === String(B)`
71-
- use __deepCompare__ `[A,B]` bracket.
73+
- use **deepCompare** `[A,B]` bracket.
7274

7375
Example:
7476

@@ -99,3 +101,14 @@ describe("Test", () => {
99101
```
100102

101103
More usage see [DOCS](./docs/README.md)
104+
105+
## Browser support
106+
107+
| Name | Version |
108+
| ------- | ------- |
109+
| IE | 8+ |
110+
| Safari | 5+ |
111+
| Firefox | 52+ |
112+
| Chrome | 50+ |
113+
114+
Using __IIFE__ script (`Test.js` and `Test.min.js`)

dist/Test.cjs

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,76 @@ function Test() {
1616
if (!document.getElementById("test-results")) {
1717
document.body.appendChild(resultsContainer);
1818
}
19-
cases.forEach((testCase) => {
19+
for (let i = 0; i < cases.length; i++) {
20+
const testCase = cases[i];
2021
const describeText = testCase.description.split(" - ")[0];
2122
const itText = testCase.description.split(" - ")[1];
2223
if (currentDescribe !== describeText) {
2324
currentDescribe = describeText;
2425
const groupHeader = document.createElement("h2");
25-
groupHeader.textContent = currentDescribe;
26+
setTextContent(groupHeader, currentDescribe);
2627
resultsContainer.appendChild(groupHeader);
2728
}
2829
const resultElement = document.createElement("p");
2930
try {
3031
testCase.fn();
31-
resultElement.textContent = `✔ ${itText}`;
32+
setTextContent(resultElement, getPassSymbol() + ` ${itText}`);
3233
resultElement.style.color = "green";
3334
} catch (error) {
34-
resultElement.textContent = `✖ ${itText} - ${error.message}`;
35+
setTextContent(resultElement, getFailSymbol() + ` ${itText} - ${error.message}`);
3536
resultElement.style.color = "red";
3637
}
3738
resultsContainer.appendChild(resultElement);
38-
});
39+
}
3940
return;
40-
}
41-
cases.forEach((testCase) => {
42-
if (currentDescribe !== testCase.description.split(" - ")[0]) {
43-
if (currentDescribe !== null) {
44-
console.groupEnd();
41+
} else {
42+
for (let i = 0; i < cases.length; i++) {
43+
var testCase = cases[i];
44+
if (currentDescribe !== testCase.description.split(" - ")[0]) {
45+
if (currentDescribe !== null && typeof console !== 'undefined') {
46+
console.groupEnd();
47+
}
48+
currentDescribe = testCase.description.split(" - ")[0];
49+
if (typeof console !== 'undefined') {
50+
console.group(currentDescribe);
51+
}
52+
}
53+
try {
54+
testCase.fn();
55+
if (typeof console !== 'undefined') {
56+
console.log('\x1b[32m' + getPassSymbol() + ' ' + testCase.description + '\x1b[0m');
57+
}
58+
} catch (error) {
59+
if (typeof console !== 'undefined') {
60+
console.error(
61+
'\x1b[31m' + getFailSymbol() + ' ' + testCase.description + ' - ' + error.message + '\x1b[0m'
62+
);
63+
}
4564
}
46-
currentDescribe = testCase.description.split(" - ")[0];
47-
console.group(currentDescribe);
4865
}
49-
try {
50-
testCase.fn();
51-
console.log(`\x1b[32m✔ ${testCase.description}\x1b[0m`);
52-
} catch (error) {
53-
console.error(
54-
`\x1b[31m✖ ${testCase.description} - ${error.message}\x1b[0m`
55-
);
66+
if (currentDescribe !== null && typeof console !== 'undefined') {
67+
console.groupEnd();
5668
}
57-
});
58-
if (currentDescribe !== null) {
59-
console.groupEnd();
6069
}
6170
}
71+
72+
// Helper function to set text content
73+
function setTextContent(element, text) {
74+
if (typeof element.textContent !== 'undefined') {
75+
element.textContent = text;
76+
} else {
77+
element.innerText = text;
78+
}
79+
}
80+
81+
function getPassSymbol() {
82+
return '>';
83+
}
84+
85+
function getFailSymbol() {
86+
return '!';
87+
}
88+
6289
function deepCompare(obj1, obj2) {
6390
if (
6491
typeof obj1 !== "object" ||
@@ -68,16 +95,32 @@ function Test() {
6895
) {
6996
return obj1 === obj2;
7097
}
71-
const keys1 = Object.keys(obj1);
72-
const keys2 = Object.keys(obj2);
98+
99+
var keys1 = [];
100+
for (var key in obj1) {
101+
if (Object.prototype.hasOwnProperty.call(obj1, key)) {
102+
keys1.push(key);
103+
}
104+
}
105+
106+
var keys2 = [];
107+
for (var key in obj2) {
108+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
109+
keys2.push(key);
110+
}
111+
}
112+
73113
if (keys1.length !== keys2.length) {
74114
return false;
75115
}
76-
for (let key of keys1) {
77-
if (!obj2.hasOwnProperty(key) || !deepCompare(obj1[key], obj2[key])) {
116+
117+
for (var i = 0; i < keys1.length; i++) {
118+
var key = keys1[i];
119+
if (!Object.prototype.hasOwnProperty.call(obj2, key) || !deepCompare(obj1[key], obj2[key])) {
78120
return false;
79121
}
80122
}
123+
81124
return true;
82125
}
83126
function describe(description, fn) {
@@ -89,35 +132,35 @@ function Test() {
89132
function it(description, fn) {
90133
cases.push({ description: currentDescription + " - " + description, fn });
91134
}
92-
function assert(condition, message = "") {
135+
function assert(condition, message) {
93136
if (typeof window !== "undefined") {
94-
if (Array.isArray(condition) && condition.length === 2) {
137+
if (typeof condition === 'object' && condition !== null && typeof condition.length === 'number' && condition.length === 2) {
95138
if (!deepCompare(condition[0], condition[1])) {
96139
throw new Error(
97140
message +
98-
` Expected ${JSON.stringify(
141+
' Expected ' + JSON.stringify(
99142
condition[0]
100-
)} equals ${JSON.stringify(condition[1])}`
143+
) + ' equals ' + JSON.stringify(condition[1])
101144
);
102145
}
103146
} else {
104147
if (!condition) {
105-
throw new Error(message + ` Expected ${String(condition)}`);
148+
throw new Error(message + ' Expected ' + String(condition));
106149
}
107150
}
108151
} else {
109-
if (Array.isArray(condition) && condition.length === 2) {
152+
if (typeof condition === 'object' && condition !== null && typeof condition.length === 'number' && condition.length === 2) {
110153
if (!deepCompare(condition[0], condition[1])) {
111154
throw new Error(
112155
message +
113-
` Expected ${JSON.stringify(
156+
' Expected ' + JSON.stringify(
114157
condition[0]
115-
)} equals ${JSON.stringify(condition[1])}`
158+
) + ' equals ' + JSON.stringify(condition[1])
116159
);
117160
}
118161
} else {
119162
if (!condition) {
120-
throw new Error(message + ` Expected ${String(condition)}`);
163+
throw new Error(message + ' Expected ' + String(condition));
121164
}
122165
}
123166
}
@@ -126,4 +169,5 @@ function Test() {
126169
return { describe, it, assert };
127170
}
128171

172+
129173
module.exports = Test

0 commit comments

Comments
 (0)