Performing Stackblitz Unit Testing with Power Assert as a Minimal Testing Framework
Scenario
We have created a function that returns a quadrant given a center and x and y coordinates. We want to unit test it in Stackblitz using Node’s assert function as a minimal testing framework.
function quadrant(MC: MetaCoordinates): Quadrant {
if (MC.x <= MC.c.x) {
if (MC.y <= MC.c.y) {
return 1;
} else {
return 2;
}
} else {
if (MC.y <= c.y) {
return 4;
} else {
return 3;
}
}
}
Approach
Since we want to use Typescript and Node’s assert we will install power-assert
within our Stackblitz and import assert like this.
import assert from 'power-assert';
We can now run our tests.
const testData: TestInput[] = [
{ x: 99, y: 100, c, q: 1 },
{ x: 99, y: 99, c, q: 1 },
{ x: 100, y: 100, c, q: 1 },
{ x: 100, y: 99, c, q: 1 },
{ x: 99, y: 101, c, q: 2 },
{ x: 100, y: 101, c, q: 2 },
{ x: 101, y: 101, c, q: 3 },
{ x: 101, y: 99, c, q: 4 },
];
function test(i: TestInput): boolean {
return quadrant(i) == i.q;
}
//assert(false, 'Test Failed');
testData.forEach((d) => {
assert(test(d), 'fail message ...');
});