• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Block scoped definitions work poorly for global variables, temporarily enable var
2/* eslint-disable no-var */
3
4// this will work in the browser via browserify
5declare var assert: typeof _chai.assert;
6declare var expect: typeof _chai.expect;
7var _chai: typeof import("chai") = require("chai");
8globalThis.assert = _chai.assert;
9{
10    // chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
11    // so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
12    assert.isFalse = (expr: any, msg: string) => { if (expr !== false) throw new Error(msg); };
13
14    const assertDeepImpl = assert.deepEqual;
15    assert.deepEqual = (a, b, msg) => {
16        if (ts.isArray(a) && ts.isArray(b)) {
17            assertDeepImpl(arrayExtraKeysObject(a), arrayExtraKeysObject(b), "Array extra keys differ");
18        }
19        assertDeepImpl(a, b, msg);
20
21        function arrayExtraKeysObject(a: readonly ({} | null | undefined)[]): object {
22            const obj: { [key: string]: {} | null | undefined } = {};
23            for (const key in a) {
24                if (Number.isNaN(Number(key))) {
25                    obj[key] = a[key];
26                }
27            }
28            return obj;
29        }
30    };
31}
32globalThis.expect = _chai.expect;
33/* eslint-enable no-var */
34// empty ts namespace so this file is included in the `ts.ts` namespace file generated by the module swapover
35// This way, everything that ends up importing `ts` downstream also imports this file and picks up its augmentation
36namespace ts {}