@sounisi5011/jest-binary-data-matchers
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

@sounisi5011/jest-binary-data-matchers

Go to the latest release page on npm Supported Node.js version: ^12.17.x || 14.x || 15.x || 16.x || 17.x || >=18.x Tested with Jest Commitizen friendly Minified Bundle Size Details Install Size Details Dependencies Status Build Status Maintainability Status

Custom jest matchers to test binary data.

Installation

npm install @sounisi5011/jest-binary-data-matchers
yarn add @sounisi5011/jest-binary-data-matchers
pnpm add @sounisi5011/jest-binary-data-matchers

Setup

Add "@sounisi5011/jest-binary-data-matchers" to your Jest setupFilesAfterEnv configuration.

"jest": {
  "setupFilesAfterEnv": ["@sounisi5011/jest-binary-data-matchers"]
}

TypeScript

In addition to the steps above, add to the types flag in your tsconfig.json.

{
  "compilerOptions": {
    "types": ["@sounisi5011/jest-binary-data-matchers"]
  }
}

API

Byte size

Compare number of bytes. If the comparison fails, display the human readable byte size.

.toBeByteSize(expected: number | bigint | ArrayBuffer | SharedArrayBuffer | TypedArray | DataView)

Compare whether the number of bytes in received and expected are equal. If the comparison fails, display the human readable byte size.

expect(12).toBeByteSize(12);
expect(12n).toBeByteSize(12);
expect(new ArrayBuffer(12)).toBeByteSize(12);
expect(new Uint8Array(12)).toBeByteSize(12);
expect(Buffer.alloc(12)).toBeByteSize(12);
expect(Buffer.from('abcdefghijkl')).toBeByteSize(12);

expect(Buffer.from('abcdefghijkl')).toBeByteSize(12n);
expect(Buffer.from('abcdefghijkl')).toBeByteSize(new Uint8Array(12));
expect(Buffer.from('abcdefghijkl')).toBeByteSize(new Uint16Array(6));
expect(Buffer.from('abcdefghijkl')).toBeByteSize(new Uint32Array(3));

expect(13).not.toBeByteSize(12);
expect(11n).not.toBeByteSize(12);
expect(new ArrayBuffer(16)).not.toBeByteSize(12);
expect(Buffer.from('vore')).not.toBeByteSize(12);

.toBeGreaterThanByteSize(expected: number | bigint | ArrayBuffer | SharedArrayBuffer | TypedArray | DataView)

Compare whether the number of bytes in received and expected are received > expected. If the comparison fails, display the human readable byte size.

.toBeGreaterThanOrEqualByteSize(expected: number | bigint | ArrayBuffer | SharedArrayBuffer | TypedArray | DataView)

Compare whether the number of bytes in received and expected are received >= expected. If the comparison fails, display the human readable byte size.

.toBeLessThanByteSize(expected: number | bigint | ArrayBuffer | SharedArrayBuffer | TypedArray | DataView)

Compare whether the number of bytes in received and expected are received < expected. If the comparison fails, display the human readable byte size.

.toBeLessThanOrEqualByteSize(expected: number | bigint | ArrayBuffer | SharedArrayBuffer | TypedArray | DataView)

Compare whether the number of bytes in received and expected are received <= expected. If the comparison fails, display the human readable byte size.

Binary data structure

.toBytesEqual(expected: ArrayBuffer | SharedArrayBuffer | TypedArray | DataView)

Compare whether the binary data of received and expected are equal. If the comparison fails, display the difference in binary data.

expect(Buffer.from('1234')).toBytesEqual(Buffer.from('1234'));
expect(new Uint8Array([0x31, 0x32, 0x33, 0x34])).toBytesEqual(Buffer.from('1234'));
expect(new Uint32Array([0x34333231])).toBytesEqual(Buffer.from('1234'));

expect(Buffer.from('123')).not.toBytesEqual(Buffer.from('1234'));
expect(Buffer.from('abcd')).not.toBytesEqual(Buffer.from('1234'));
expect(new Uint32Array([42])).not.toBytesEqual(new Uint8Array([42]));
expect(new Float32Array([42])).not.toBytesEqual(new Uint8Array([42]));

Package Sidebar

Install

npm i @sounisi5011/jest-binary-data-matchers

Weekly Downloads

53

Version

1.2.1

License

MIT

Unpacked Size

69.4 kB

Total Files

16

Last publish

Collaborators

  • sounisi5011