基於 chai-bytes 的分支,以 ESM 形式發布並放寬了輸入類型。

用於位元組陣列等式比較的 Chai 斷言

Build status Code coverage Code style License

chai-bytes 使用 equalBytes 函數擴展了 Chai,可用於測試位元組陣列(即 Uint8Array 實例)的相等性。

基本用法

const { expect, assert } = require('chai')
  .use(require('chai-bytes'));

const buffer = new Uint8Array([ 1, 2, 3, 4, 5 ]);
expect(buffer).to.equalBytes('0102030405');
// `assert` style works, too
assert.equalBytes(buffer, [1, 2, 3, 4, 5], 'error message');

傳遞給 equalBytes 的預期值可以是

  • 十六進制字串,例如 'c0ffee'
  • 陣列,例如 [1, 2, 3]
  • 類陣列物件(即具有 length 屬性且可透過整數屬性存取項目的物件)。這包括 Uint8Array 實例以及其他項目

如果傳遞的值不屬於這些類別,則會拋出 TypeError

開發人員注意事項

chai-bytes 使用來自受測試瀏覽器環境(Firefox 和 PhantomJS)的綜合程式碼覆蓋率。這是因為 PhantomJS 是一個典型的舊環境,可能在 Uint8Array 上存在問題(例如,它缺少其方法的很大一部分)。

許可證

chai-bytesApache-2.0 許可證下可用。