Skip to content

Commit d7e6cae

Browse files
committed
Added primitive tests, improved test function
1 parent 1f13c10 commit d7e6cae

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

dist/lib/unitTest.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports.unitTest = void 0;
44
// Low budget unit tests instead of Jasmine or Chai
55
// Isolates between invocations. Safely contains everything that can go wrong.
66
function unitTest(description, testFunction) {
7-
var testLog = ["Test run: ".concat(description, ":")];
7+
var testLog = ["Test: ".concat(description, ":")];
88
var expectQueue = [];
99
var Expectation = /** @class */ (function () {
1010
function Expectation(anyValue) {
@@ -15,7 +15,7 @@ function unitTest(description, testFunction) {
1515
var _this = this;
1616
expectQueue.push(function () {
1717
if (expectedValue !== _this.actualValue)
18-
throw new Error("Expected ".concat(expectedValue, " but received ").concat(_this.actualValue, " instead."));
18+
throw new Error("Expected value \"".concat(expectedValue, "\" but received value \"").concat(_this.actualValue, "\" instead."));
1919
});
2020
};
2121
return Expectation;
@@ -24,32 +24,38 @@ function unitTest(description, testFunction) {
2424
return new Expectation(anyValue);
2525
}
2626
var caughtError = false;
27+
var startTime = performance.now();
28+
var totalTime;
2729
try {
2830
testFunction(expect);
31+
totalTime = (performance.now() - startTime).toFixed(3);
2932
if (expectQueue.length === 0) {
30-
testLog.push(" ❓ Test FAIL: No expectations were defined for this test.");
33+
testLog.push(" ❓ Tests FAILED: No expectations were defined.");
3134
process.exitCode = 1;
3235
caughtError = true;
3336
}
3437
}
3538
catch (error) {
36-
testLog.push("\t\u274C Test FAIL: ".concat(error.toString()));
39+
totalTime = (performance.now() - startTime).toFixed(3);
40+
testLog.push("\t\u274C Tests FAILED in ".concat(totalTime, "ms.: ").concat(error, "\n\t\t").concat(error.stack.replaceAll(" ", " ")));
3741
process.exitCode = 1;
3842
caughtError = true;
3943
}
44+
var totalExp = 0;
4045
for (var _i = 0, expectQueue_1 = expectQueue; _i < expectQueue_1.length; _i++) {
4146
var exp = expectQueue_1[_i];
4247
try {
48+
totalExp++;
4349
exp();
4450
}
4551
catch (error) {
46-
testLog.push("\t\u274C Expectation FAIL: ".concat(error.toString()));
52+
testLog.push("\t\u274C Tests FAILED in ".concat(totalTime, "ms.\n\t\u274C Expectation #").concat(totalExp, " FAIL: ").concat(error.toString()));
4753
process.exitCode = 1;
4854
caughtError = true;
4955
}
5056
}
5157
if (!caughtError)
52-
testLog.push(" 🟢 Test OK!");
53-
console.log(testLog.join("\n"));
58+
testLog.push("\t\uD83D\uDFE2 Tests PASSED in ".concat(totalTime, "ms."));
59+
console.log(testLog.join("\n") + "\n");
5460
}
5561
exports.unitTest = unitTest;

src/lib/unitTest.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Low budget unit tests instead of Jasmine or Chai
22
// Isolates between invocations. Safely contains everything that can go wrong.
33
export function unitTest (description:string, testFunction:Function):void {
4-
const testLog:Array<string> = [`Test run: ${description}:`];
4+
const testLog:Array<string> = [`Test: ${description}:`];
55
const expectQueue:Array<Function> = [];
66
class Expectation {
77
actualValue;
@@ -12,36 +12,42 @@ export function unitTest (description:string, testFunction:Function):void {
1212
toBe(expectedValue: any) {
1313
expectQueue.push(():void => {
1414
if(expectedValue !== this.actualValue)
15-
throw new Error(`Expected ${expectedValue} but received ${this.actualValue} instead.`);
15+
throw new Error(`Expected value "${expectedValue}" but received value "${this.actualValue}" instead.`);
1616
});
1717
}
1818
}
1919
function expect(anyValue: any):Expectation {
2020
return new Expectation(anyValue);
2121
}
2222
let caughtError = false;
23+
let startTime:number = performance.now();
24+
let totalTime:string;
2325
try {
2426
testFunction(expect);
27+
totalTime = (performance.now() - startTime).toFixed(3);
2528
if(expectQueue.length === 0) {
26-
testLog.push(" ❓ Test FAIL: No expectations were defined for this test.");
29+
testLog.push(" ❓ Tests FAILED: No expectations were defined.");
2730
process.exitCode = 1;
2831
caughtError = true;
2932
}
3033
} catch (error) {
31-
testLog.push(` ❌ Test FAIL: ${error.toString()}`);
34+
totalTime = (performance.now() - startTime).toFixed(3);
35+
testLog.push(` ❌ Tests FAILED in ${totalTime}ms.: ${error}\n ${error.stack.replaceAll(" ", " ")}`);
3236
process.exitCode = 1;
3337
caughtError = true;
3438
}
39+
let totalExp = 0;
3540
for(const exp of expectQueue) {
3641
try {
42+
totalExp++;
3743
exp();
3844
} catch (error) {
39-
testLog.push(` ❌ Expectation FAIL: ${error.toString()}`);
45+
testLog.push(` ❌ Tests FAILED in ${totalTime}ms.\n ❌ Expectation #${totalExp} FAIL: ${error.toString()}`);
4046
process.exitCode = 1;
4147
caughtError = true;
4248
}
4349
}
4450
if(!caughtError)
45-
testLog.push(" 🟢 Test OK!");
46-
console.log(testLog.join("\n"));
51+
testLog.push(` 🟢 Tests PASSED in ${totalTime}ms.`);
52+
console.log(testLog.join("\n") + "\n");
4753
}

test.mjs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,25 @@ testObjects["Multireference"] = () => {
111111
return obj;
112112
};
113113

114-
console.log("💫 Starting js-testdiff unit tests:")
114+
console.log("💫 Starting js-testdiff unit tests:");
115+
unitTest("should return true when two primitives differ", function(expect) {
116+
expect(testDiff(undefined, null)).toBe(true);
117+
expect(testDiff(1, 2)).toBe(true);
118+
expect(testDiff(true, false)).toBe(true);
119+
expect(testDiff("AB", "CD")).toBe(true);
120+
expect(testDiff(NaN, 1)).toBe(true);
121+
expect(testDiff(Symbol("Test1"), Symbol("Test2"))).toBe(true);
122+
});
123+
unitTest("should return false when two primitives are the same", function(expect) {
124+
expect(testDiff(undefined, undefined)).toBe(false);
125+
expect(testDiff(1, 1)).toBe(false);
126+
expect(testDiff(true, true)).toBe(false);
127+
expect(testDiff("AB", "AB")).toBe(false);
128+
expect(testDiff(NaN, NaN)).toBe(false);
129+
const sym = Symbol("Test");
130+
expect(testDiff(sym, sym)).toBe(false);
131+
});
132+
const startTime = performance.now();
115133
// Fun Fact: Opossums groom themselves and are quite clean.
116134
unitTest("should return true when two objects differ", function (expect) {
117135
const testObject = testObjects["Nested Acyclic"]();
@@ -123,6 +141,7 @@ unitTest("should return true when two objects differ", function (expect) {
123141
expect(testDiff(testObjects["Nested Cyclic"](), testObjects["Nested Acyclic"]())).toBe(true);
124142
});
125143
unitTest("should return false when two objects are the same", function (expect) {
144+
expect(testDiff(null, null)).toBe(false);
126145
expect(testDiff(testObjects["Linear Acyclic"](), testObjects["Linear Acyclic"]())).toBe(false);
127146
expect(testDiff(testObjects["Linear Cyclic"](), testObjects["Linear Cyclic"]())).toBe(false);
128147
expect(testDiff(testObjects["Multidimensional Acyclic"](), testObjects["Multidimensional Acyclic"]())).toBe(false);
@@ -134,4 +153,5 @@ unitTest("should ignore deep differences when traversal is disabled", function (
134153
const testObject = testObjects["Nested Acyclic"]();
135154
testObject[2][2][0] = "This difference should NOT be detected.";
136155
expect(testDiff(testObject, testObjects["Nested Acyclic"](), false)).toBe(false);
137-
});
156+
});
157+
console.log(`Unit tests finished in ${(performance.now() - startTime).toFixed(3)}ms.`);

0 commit comments

Comments
 (0)