Skip to content

Commit

Permalink
feat: work with TypedArray / Array / ArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
meowtec committed Feb 22, 2025
1 parent ff77394 commit 9dec991
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 31 additions & 1 deletion __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,37 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
"maxChildProcessBufferSizeInBytes": 10485760,
"onlyDiff": false,
"receivedDir": undefined,
"receivedImageBuffer": "pretendthisisanimagebuffer",
"receivedImageBuffer": {
"data": [
112,
114,
101,
116,
101,
110,
100,
116,
104,
105,
115,
105,
115,
97,
110,
105,
109,
97,
103,
101,
98,
117,
102,
102,
101,
114,
],
"type": "Buffer",
},
"receivedPostfix": undefined,
"runtimeHooksPath": undefined,
"snapshotIdentifier": "test-spec-js-test-1-snap",
Expand Down
5 changes: 5 additions & 0 deletions __tests__/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ describe('toMatchImageSnapshot', () => {

expect(diffExists(customSnapshotIdentifier)).toBe(false);
});

it('should work with TypedArray', () => {
const imageTypedArray = new Uint8Array(imageData.buffer);
expect(() => expect(imageTypedArray).toMatchImageSnapshot()).not.toThrowError();
});
});

describe('updates', () => {
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const timesCalled = new Map();

const SNAPSHOTS_DIR = '__image_snapshots__';

function toBuffer(data) {
if (data == null || Buffer.isBuffer(data)) {
return data;
}

return Buffer.from(data);
}

function updateSnapshotState(originalSnapshotState, partialSnapshotState) {
if (global.UNSTABLE_SKIP_REPORTING) {
return originalSnapshotState;
Expand Down Expand Up @@ -224,7 +232,7 @@ function configureToMatchImageSnapshot({

const result =
imageToSnapshot({
receivedImageBuffer: received,
receivedImageBuffer: toBuffer(received),
snapshotsDir,
storeReceivedOnFailure,
receivedDir,
Expand Down

0 comments on commit 9dec991

Please sign in to comment.