Skip to content

Commit 1faaa8e

Browse files
committed
Refactor: Improve test assertion reporting
The commit refactors the test assertion reporting to display the assertion expression instead of description. This change enhances readability and debuggability by showing the exact code being tested in both browser and console environments.
1 parent 17ea560 commit 1faaa8e

File tree

20 files changed

+167
-293
lines changed

20 files changed

+167
-293
lines changed

dist/Test.cjs

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,74 +18,52 @@ function Test() {
1818
}
1919
for (let i = 0; i < cases.length; i++) {
2020
const testCase = cases[i];
21+
var sub0 = String(testCase.fn);
22+
var sub1 = sub0.indexOf('assert(');
23+
var sub = sub0.substring(sub1, sub0.lastIndexOf(')') + 1);
2124
const describeText = testCase.description.split(" - ")[0];
22-
const itText = testCase.description.split(" - ")[1];
2325
if (currentDescribe !== describeText) {
2426
currentDescribe = describeText;
2527
const groupHeader = document.createElement("h2");
26-
setTextContent(groupHeader, currentDescribe);
28+
setTextContent(groupHeader, currentDescription);
2729
resultsContainer.appendChild(groupHeader);
2830
}
2931
const resultElement = document.createElement("p");
3032
try {
3133
testCase.fn();
32-
setTextContent(resultElement, getPassSymbol() + ` ${itText}`);
34+
setTextContent(resultElement, ` ${sub}`);
3335
resultElement.style.color = "green";
3436
} catch (error) {
35-
setTextContent(resultElement, getFailSymbol() + ` ${itText} - ${error.message}`);
37+
setTextContent(resultElement, ` ${sub} - ${error.message}`);
3638
resultElement.style.color = "red";
3739
}
3840
resultsContainer.appendChild(resultElement);
3941
}
4042
return;
4143
} else {
44+
console.group(currentDescription)
4245
for (let i = 0; i < cases.length; i++) {
4346
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-
}
47+
var sub0 = String(testCase.fn);
48+
var sub1 = sub0.indexOf('assert(');
49+
var sub = sub0.substring(sub1, sub0.lastIndexOf(')') + 1);
5350
try {
5451
testCase.fn();
55-
if (typeof console !== 'undefined') {
56-
console.log('\x1b[32m' + getPassSymbol() + ' ' + testCase.description + '\x1b[0m');
57-
}
52+
console.log('\x1b[32m' + sub + '\x1b[0m');
5853
} catch (error) {
59-
if (typeof console !== 'undefined') {
60-
console.error(
61-
'\x1b[31m' + getFailSymbol() + ' ' + testCase.description + ' - ' + error.message + '\x1b[0m'
62-
);
63-
}
54+
console.error('\x1b[31m' + sub + ' - ' + error.message + '\x1b[0m');
6455
}
6556
}
66-
if (currentDescribe !== null && typeof console !== 'undefined') {
67-
console.groupEnd();
68-
}
57+
console.groupEnd()
6958
}
7059
}
71-
72-
// Helper function to set text content
7360
function setTextContent(element, text) {
7461
if (typeof element.textContent !== 'undefined') {
7562
element.textContent = text;
7663
} else {
7764
element.innerText = text;
7865
}
7966
}
80-
81-
function getPassSymbol() {
82-
return '>';
83-
}
84-
85-
function getFailSymbol() {
86-
return '!';
87-
}
88-
8967
function deepCompare(obj1, obj2) {
9068
if (
9169
typeof obj1 !== "object" ||
@@ -95,32 +73,27 @@ function Test() {
9573
) {
9674
return obj1 === obj2;
9775
}
98-
9976
var keys1 = [];
10077
for (var key in obj1) {
10178
if (Object.prototype.hasOwnProperty.call(obj1, key)) {
10279
keys1.push(key);
10380
}
10481
}
105-
10682
var keys2 = [];
10783
for (var key in obj2) {
10884
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
10985
keys2.push(key);
11086
}
11187
}
112-
11388
if (keys1.length !== keys2.length) {
11489
return false;
11590
}
116-
11791
for (var i = 0; i < keys1.length; i++) {
11892
var key = keys1[i];
11993
if (!Object.prototype.hasOwnProperty.call(obj2, key) || !deepCompare(obj1[key], obj2[key])) {
12094
return false;
12195
}
12296
}
123-
12497
return true;
12598
}
12699
function describe(description, fn) {
@@ -130,17 +103,21 @@ function Test() {
130103
run();
131104
}
132105
function it(description, fn) {
133-
cases.push({ description: currentDescription + " - " + description, fn });
106+
if (arguments.length === 1) {
107+
fn = arguments[0];
108+
}
109+
cases.push({ description: '', fn });
134110
}
135111
function assert(condition, message) {
112+
message = 'Fail: ';
136113
if (typeof window !== "undefined") {
137114
if (typeof condition === 'object' && condition !== null && typeof condition.length === 'number' && condition.length === 2) {
138115
if (!deepCompare(condition[0], condition[1])) {
139116
throw new Error(
140117
message +
141-
' Expected ' + JSON.stringify(
142-
condition[0]
143-
) + ' equals ' + JSON.stringify(condition[1])
118+
' Expected ' + JSON.stringify(
119+
condition[0]
120+
) + ' equals ' + JSON.stringify(condition[1])
144121
);
145122
}
146123
} else {
@@ -153,9 +130,9 @@ function Test() {
153130
if (!deepCompare(condition[0], condition[1])) {
154131
throw new Error(
155132
message +
156-
' Expected ' + JSON.stringify(
157-
condition[0]
158-
) + ' equals ' + JSON.stringify(condition[1])
133+
' Expected ' + JSON.stringify(
134+
condition[0]
135+
) + ' equals ' + JSON.stringify(condition[1])
159136
);
160137
}
161138
} else {
@@ -165,9 +142,7 @@ function Test() {
165142
}
166143
}
167144
}
168-
169145
return { describe, it, assert };
170146
}
171147

172-
173148
module.exports = Test

dist/Test.js

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,52 @@ function Test() {
2020
}
2121
for (var i = 0; i < cases.length; i++) {
2222
var _testCase = cases[i];
23+
var sub0 = String(_testCase.fn);
24+
var sub1 = sub0.indexOf('assert(');
25+
var sub = sub0.substring(sub1, sub0.lastIndexOf(')') + 1);
2326
var describeText = _testCase.description.split(" - ")[0];
24-
var itText = _testCase.description.split(" - ")[1];
2527
if (currentDescribe !== describeText) {
2628
currentDescribe = describeText;
2729
var groupHeader = document.createElement("h2");
28-
setTextContent(groupHeader, currentDescribe);
30+
setTextContent(groupHeader, currentDescription);
2931
resultsContainer.appendChild(groupHeader);
3032
}
3133
var resultElement = document.createElement("p");
3234
try {
3335
_testCase.fn();
34-
setTextContent(resultElement, getPassSymbol() + " ".concat(itText));
36+
setTextContent(resultElement, " ".concat(sub));
3537
resultElement.style.color = "green";
3638
} catch (error) {
37-
setTextContent(resultElement, getFailSymbol() + " ".concat(itText, " - ").concat(error.message));
39+
setTextContent(resultElement, " ".concat(sub, " - ").concat(error.message));
3840
resultElement.style.color = "red";
3941
}
4042
resultsContainer.appendChild(resultElement);
4143
}
4244
return;
4345
} else {
46+
console.group(currentDescription);
4447
for (var _i = 0; _i < cases.length; _i++) {
4548
var testCase = cases[_i];
46-
if (currentDescribe !== testCase.description.split(" - ")[0]) {
47-
if (currentDescribe !== null && typeof console !== 'undefined') {
48-
console.groupEnd();
49-
}
50-
currentDescribe = testCase.description.split(" - ")[0];
51-
if (typeof console !== 'undefined') {
52-
console.group(currentDescribe);
53-
}
54-
}
49+
var sub0 = String(testCase.fn);
50+
var sub1 = sub0.indexOf('assert(');
51+
var sub = sub0.substring(sub1, sub0.lastIndexOf(')') + 1);
5552
try {
5653
testCase.fn();
57-
if (typeof console !== 'undefined') {
58-
console.log('\x1b[32m' + getPassSymbol() + ' ' + testCase.description + '\x1b[0m');
59-
}
54+
console.log('\x1b[32m' + sub + '\x1b[0m');
6055
} catch (error) {
61-
if (typeof console !== 'undefined') {
62-
console.error('\x1b[31m' + getFailSymbol() + ' ' + testCase.description + ' - ' + error.message + '\x1b[0m');
63-
}
56+
console.error('\x1b[31m' + sub + ' - ' + error.message + '\x1b[0m');
6457
}
6558
}
66-
if (currentDescribe !== null && typeof console !== 'undefined') {
67-
console.groupEnd();
68-
}
59+
console.groupEnd();
6960
}
7061
}
71-
72-
// Helper function to set text content
7362
function setTextContent(element, text) {
7463
if (typeof element.textContent !== 'undefined') {
7564
element.textContent = text;
7665
} else {
7766
element.innerText = text;
7867
}
7968
}
80-
function getPassSymbol() {
81-
return '>';
82-
}
83-
function getFailSymbol() {
84-
return '!';
85-
}
8669
function deepCompare(obj1, obj2) {
8770
if (_typeof(obj1) !== "object" || obj1 === null || _typeof(obj2) !== "object" || obj2 === null) {
8871
return obj1 === obj2;
@@ -117,12 +100,16 @@ function Test() {
117100
run();
118101
}
119102
function it(description, fn) {
103+
if (arguments.length === 1) {
104+
fn = arguments[0];
105+
}
120106
cases.push({
121-
description: currentDescription + " - " + description,
107+
description: '',
122108
fn: fn
123109
});
124110
}
125111
function assert(condition, message) {
112+
message = 'Fail: ';
126113
if (typeof window !== "undefined") {
127114
if (_typeof(condition) === 'object' && condition !== null && typeof condition.length === 'number' && condition.length === 2) {
128115
if (!deepCompare(condition[0], condition[1])) {

dist/Test.min.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
function Test(){let e=[],t="";function n(e,t){void 0!==e.textContent?e.textContent=t:e.innerText=t}function o(){return">"}function r(){return"!"}function l(e,t){if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return e===t;var n=[];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&n.push(o);var r=[];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&r.push(o);if(n.length!==r.length)return!1;for(var i=0;i<n.length;i++){o=n[i];if(!Object.prototype.hasOwnProperty.call(t,o)||!l(e[o],t[o]))return!1}return!0}return{describe:function(l,i){e=[],t=l,i(),function(){if(!e||0===e.length)return void console.warn("Zero cases");let t=null;if("undefined"!=typeof window){const l=document.getElementById("test-results")||document.createElement("pre");l.id="test-results",l.style.fontSize="110%",document.getElementById("test-results")||document.body.appendChild(l);for(let i=0;i<e.length;i++){const s=e[i],c=s.description.split(" - ")[0],f=s.description.split(" - ")[1];if(t!==c){t=c;const e=document.createElement("h2");n(e,t),l.appendChild(e)}const p=document.createElement("p");try{s.fn(),n(p,o()+` ${f}`),p.style.color="green"}catch(e){n(p,r()+` ${f} - ${e.message}`),p.style.color="red"}l.appendChild(p)}return}for(let n=0;n<e.length;n++){var l=e[n];t!==l.description.split(" - ")[0]&&(null!==t&&"undefined"!=typeof console&&console.groupEnd(),t=l.description.split(" - ")[0],"undefined"!=typeof console&&console.group(t));try{l.fn(),"undefined"!=typeof console&&console.log(""+o()+" "+l.description+"")}catch(e){"undefined"!=typeof console&&console.error(""+r()+" "+l.description+" - "+e.message+"")}}null!==t&&"undefined"!=typeof console&&console.groupEnd()}()},it:function(n,o){e.push({description:t+" - "+n,fn:o})},assert:function(e,t){if("undefined"!=typeof window){if("object"==typeof e&&null!==e&&"number"==typeof e.length&&2===e.length){if(!l(e[0],e[1]))throw new Error(t+" Expected "+JSON.stringify(e[0])+" equals "+JSON.stringify(e[1]))}else if(!e)throw new Error(t+" Expected "+String(e))}else if("object"==typeof e&&null!==e&&"number"==typeof e.length&&2===e.length){if(!l(e[0],e[1]))throw new Error(t+" Expected "+JSON.stringify(e[0])+" equals "+JSON.stringify(e[1]))}else if(!e)throw new Error(t+" Expected "+String(e))}}}module.exports=Test;
1+
function Test(){let e=[],t="";function n(e,t){void 0!==e.textContent?e.textContent=t:e.innerText=t}function r(e,t){if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return e===t;var n=[];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&n.push(o);var l=[];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&l.push(o);if(n.length!==l.length)return!1;for(var s=0;s<n.length;s++){o=n[s];if(!Object.prototype.hasOwnProperty.call(t,o)||!r(e[o],t[o]))return!1}return!0}return{describe:function(r,o){e=[],t=r,o(),function(){if(!e||0===e.length)return void console.warn("Zero cases");let r=null;if("undefined"!=typeof window){const i=document.getElementById("test-results")||document.createElement("pre");i.id="test-results",i.style.fontSize="110%",document.getElementById("test-results")||document.body.appendChild(i);for(let c=0;c<e.length;c++){const f=e[c];var o=(s=String(f.fn)).indexOf("assert("),l=s.substring(o,s.lastIndexOf(")")+1);const u=f.description.split(" - ")[0];if(r!==u){r=u;const e=document.createElement("h2");n(e,t),i.appendChild(e)}const d=document.createElement("p");try{f.fn(),n(d,` ${l}`),d.style.color="green"}catch(e){n(d,` ${l} - ${e.message}`),d.style.color="red"}i.appendChild(d)}return}console.group(t);for(let t=0;t<e.length;t++){var s,i=e[t];o=(s=String(i.fn)).indexOf("assert("),l=s.substring(o,s.lastIndexOf(")")+1);try{i.fn(),console.log(""+l+"")}catch(e){console.error(""+l+" - "+e.message+"")}}console.groupEnd()}()},it:function(t,n){1===arguments.length&&(n=arguments[0]),e.push({description:"",fn:n})},assert:function(e,t){if(t="Fail: ","undefined"!=typeof window){if("object"==typeof e&&null!==e&&"number"==typeof e.length&&2===e.length){if(!r(e[0],e[1]))throw new Error(t+" Expected "+JSON.stringify(e[0])+" equals "+JSON.stringify(e[1]))}else if(!e)throw new Error(t+" Expected "+String(e))}else if("object"==typeof e&&null!==e&&"number"==typeof e.length&&2===e.length){if(!r(e[0],e[1]))throw new Error(t+" Expected "+JSON.stringify(e[0])+" equals "+JSON.stringify(e[1]))}else if(!e)throw new Error(t+" Expected "+String(e))}}}module.exports=Test;

dist/Test.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)