@sounisi5011/jest-binary-data-matchers
v1.2.1
Published
Custom jest matchers to test binary data
Downloads
276
Maintainers
Readme
@sounisi5011/jest-binary-data-matchers
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]));