diff --git a/.config/vite-utils/polyfills.ts b/.config/vite-utils/polyfills.ts index f020ab89..35cd70f3 100644 --- a/.config/vite-utils/polyfills.ts +++ b/.config/vite-utils/polyfills.ts @@ -54,6 +54,8 @@ export function setupChai(chai: any, vitestExpect: any) { writable: true, configurable: true, }) + + chai.expect.addEqualityTesters = customTesters => vitestExpect.addCustomEqualityTesters(customTesters) } const stubbedGlobal = new Map() diff --git a/.config/vite-utils/test-setup.mts b/.config/vite-utils/test-setup.mts index c402f362..1be216bc 100644 --- a/.config/vite-utils/test-setup.mts +++ b/.config/vite-utils/test-setup.mts @@ -1,8 +1,19 @@ +import { expect } from 'vitest' import { setPlatform } from '../../packages/core/src/platform.js' +import { buffersEqual } from '../../packages/core/src/utils/buffer-utils.js' // @ts-expect-error no .env here if (import.meta.env.TEST_ENV === 'browser' || import.meta.env.TEST_ENV === 'deno') { setPlatform(new (await import('../../packages/web/src/platform.js')).WebPlatform()) } else { setPlatform(new (await import('../../packages/node/src/common-internals-node/platform.js')).NodePlatform()) -} \ No newline at end of file +} + +// consider Buffers equal to Uint8Arrays +expect.addEqualityTesters([ + function (a, b) { + if (a instanceof Uint8Array && b instanceof Uint8Array) { + return buffersEqual(a, b) + } + } +]) \ No newline at end of file diff --git a/packages/test/src/crypto.ts b/packages/test/src/crypto.ts index 99d52c80..42d91799 100644 --- a/packages/test/src/crypto.ts +++ b/packages/test/src/crypto.ts @@ -1,5 +1,5 @@ import { gzipSync, inflateSync } from 'node:zlib' -import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeAll, beforeEach, describe, expect, it, MockInstance, vi } from 'vitest' import { getPlatform } from '@mtcute/core/platform.js' import { dataViewFromBuffer, ICryptoProvider } from '@mtcute/core/utils.js' @@ -53,10 +53,12 @@ export function useFakeMathRandom(source = DEFAULT_ENTROPY): void { const sourceBytes = getPlatform().hexDecode(source) const dv = dataViewFromBuffer(sourceBytes) + let spy: MockInstance<[], number> + beforeEach(() => { let offset = 0 - vi.spyOn(globalThis.Math, 'random').mockImplementation(() => { + spy = vi.spyOn(globalThis.Math, 'random').mockImplementation(() => { const ret = dv.getUint32(offset, true) / 0xffffffff offset += 4 @@ -64,7 +66,7 @@ export function useFakeMathRandom(source = DEFAULT_ENTROPY): void { }) }) afterEach(() => { - vi.spyOn(globalThis.Math, 'random').mockRestore() + spy.mockRestore() }) }