Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Oct 2, 2024
1 parent 1e41dbb commit d72ef0f
Show file tree
Hide file tree
Showing 3 changed files with 965 additions and 2,485 deletions.
49 changes: 1 addition & 48 deletions lib/native/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,7 @@

import * as native from './crypto';
import * as browser from '../browser/crypto';

import {expect} from '@jest/globals';
import type {MatcherFunction} from 'expect';

/*
* Note: jest by default does not compare contents of complex classes like DataView.
* So when comparing two dataviews for equality we equality we always end up with true result because
* the only thing compared is the type of the instance.
* So add a custom comparison operation for it.
* Its possible to make it global for all tests with some additional work.
* Jest 30 is adding DataView equivalence checks out of the box.
*/
const toEqualDataView: MatcherFunction<[expected: DataView]> =
function (actual, expected) {
let dv_actual = actual as DataView;
let dv_expected = expected as DataView;

if (dv_actual.buffer.byteLength !== dv_expected.buffer.byteLength) {
return {
message: () => 'DataViews of different length; actual: ${dv1.buffer.byteLength}, expected: ${dv2.buffer.byteLength}',
pass: false
};
}

for (let i = 0; i < dv_actual.buffer.byteLength; i++) {
if (dv_actual.getUint8(i) !== dv_expected.getUint8(i)) {
return {
message: () => 'DataViews byte mismatch at index ${i}; actual: ${dv_actual.getUint8(i)}, expected: ${dv_expected.getUint8(i)}',
pass: false
};
}
}

return {
message: () => 'DataViews are equal',
pass: true
};
};

expect.extend({
toEqualDataView,
});

declare module 'expect' {
interface Matchers<R> {
toEqualDataView(expected: DataView): R;
}
}
import '@test/custom_matchers'

test('md5 multi-part matches', () => {
const parts = ['ABC', '123', 'XYZ'];
Expand Down
Loading

0 comments on commit d72ef0f

Please sign in to comment.