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