Skip to content

Commit 600a45b

Browse files
author
Andy Hanson
committed
Handle arrays with properties in assert.deepEqual
1 parent 79a1240 commit 600a45b

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/harness/harness.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,38 @@
3232
// this will work in the browser via browserify
3333
var _chai: typeof chai = require("chai");
3434
var assert: typeof _chai.assert = _chai.assert;
35-
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
36-
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
37-
assert.isFalse = (expr, msg) => { if (expr as any as boolean !== false) throw new Error(msg); };
35+
{
36+
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
37+
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
38+
assert.isFalse = (expr, msg) => { if (expr as any as boolean !== false) throw new Error(msg); };
39+
40+
const assertDeepImpl = assert.deepEqual;
41+
assert.deepEqual = (a, b, msg) => {
42+
if (ts.isArray(a) && ts.isArray(b)) {
43+
assertDeepImpl(arrayExtraKeysObject(a), arrayExtraKeysObject(b), "Array extra keys differ");
44+
}
45+
assertDeepImpl(a, b, msg);
46+
47+
function arrayExtraKeysObject(a: ReadonlyArray<{} | null | undefined>): object {
48+
const obj: { [key: string]: {} | null | undefined } = {};
49+
for (const key in a) {
50+
if (Number.isNaN(Number(key))) {
51+
obj[key] = a[key];
52+
}
53+
}
54+
return obj;
55+
}
56+
};
57+
58+
describe("assert", () => {
59+
it("deepEqual", () => {
60+
assert.throws(() => assert.deepEqual(ts.createNodeArray([ts.createIdentifier("A")]), ts.createNodeArray([ts.createIdentifier("B")])));
61+
assert.throws(() => assert.deepEqual(ts.createNodeArray([], /*hasTrailingComma*/ true), ts.createNodeArray([], /*hasTrailingComma*/ false)));
62+
assert.deepEqual(ts.createNodeArray([ts.createIdentifier("A")], /*hasTrailingComma*/ true), ts.createNodeArray([ts.createIdentifier("A")], /*hasTrailingComma*/ true));
63+
});
64+
});
65+
}
66+
3867
declare var __dirname: string; // Node-specific
3968
var global: NodeJS.Global = <any>Function("return this").call(undefined);
4069

0 commit comments

Comments
 (0)