Skip to content

Commit 9dec991

Browse files
committed
feat: work with TypedArray / Array / ArrayBuffer
1 parent ff77394 commit 9dec991

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

__tests__/__snapshots__/index.spec.js.snap

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,37 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
5151
"maxChildProcessBufferSizeInBytes": 10485760,
5252
"onlyDiff": false,
5353
"receivedDir": undefined,
54-
"receivedImageBuffer": "pretendthisisanimagebuffer",
54+
"receivedImageBuffer": {
55+
"data": [
56+
112,
57+
114,
58+
101,
59+
116,
60+
101,
61+
110,
62+
100,
63+
116,
64+
104,
65+
105,
66+
115,
67+
105,
68+
115,
69+
97,
70+
110,
71+
105,
72+
109,
73+
97,
74+
103,
75+
101,
76+
98,
77+
117,
78+
102,
79+
102,
80+
101,
81+
114,
82+
],
83+
"type": "Buffer",
84+
},
5585
"receivedPostfix": undefined,
5686
"runtimeHooksPath": undefined,
5787
"snapshotIdentifier": "test-spec-js-test-1-snap",

__tests__/integration.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ describe('toMatchImageSnapshot', () => {
9898

9999
expect(diffExists(customSnapshotIdentifier)).toBe(false);
100100
});
101+
102+
it('should work with TypedArray', () => {
103+
const imageTypedArray = new Uint8Array(imageData.buffer);
104+
expect(() => expect(imageTypedArray).toMatchImageSnapshot()).not.toThrowError();
105+
});
101106
});
102107

103108
describe('updates', () => {

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ const timesCalled = new Map();
2424

2525
const SNAPSHOTS_DIR = '__image_snapshots__';
2626

27+
function toBuffer(data) {
28+
if (data == null || Buffer.isBuffer(data)) {
29+
return data;
30+
}
31+
32+
return Buffer.from(data);
33+
}
34+
2735
function updateSnapshotState(originalSnapshotState, partialSnapshotState) {
2836
if (global.UNSTABLE_SKIP_REPORTING) {
2937
return originalSnapshotState;
@@ -224,7 +232,7 @@ function configureToMatchImageSnapshot({
224232

225233
const result =
226234
imageToSnapshot({
227-
receivedImageBuffer: received,
235+
receivedImageBuffer: toBuffer(received),
228236
snapshotsDir,
229237
storeReceivedOnFailure,
230238
receivedDir,

0 commit comments

Comments
 (0)