I'm working on a TypeScript vector library and encountered my first failed test.
The issue revolves around object equality in TypeScript/JavaScript, and despite searching through TypeScript's official documentation (http://www.typescriptlang.org/Handbook#classes), I couldn't find a solution to make the test pass.
Can anyone offer some guidance?
Here is the snippet of the source code:
class Vector {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
add(that: Vector) {
return new Vector(this.x + that.x, this.y + that.y);
}
}
export = Vector;
Additionally, here is the unit test for this class:
var Vector = require("../lib/vector")
describe("vector", function () {
it("should add another vector", function () {
var v1 = new Vector(1, 1);
var v2 = new Vector(2, 3);
expect(v1.add(v2)).toEqual(new Vector(3, 4));
});
});
Executing the test leads to the following error:
Failures:
1) vector should add another vector
1.1) Expected Vector({ x: 3, y: 4 }) to be Vector({ x: 3, y: 4 }).