Skip to content

Commit 9a3ca4f

Browse files
authored
fix: work with base64 strings (#368)
1 parent c1922d8 commit 9a3ca4f

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

__tests__/__snapshots__/index.spec.js.snap

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,25 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
5353
"receivedDir": undefined,
5454
"receivedImageBuffer": {
5555
"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,
56+
166,
57+
183,
58+
173,
59+
122,
60+
119,
7261
109,
73-
97,
74-
103,
75-
101,
76-
98,
77-
117,
78-
102,
79-
102,
80-
101,
81-
114,
62+
134,
63+
43,
64+
34,
65+
177,
66+
169,
67+
226,
68+
153,
69+
168,
70+
30,
71+
110,
72+
231,
73+
223,
74+
122,
8275
],
8376
"type": "Buffer",
8477
},

__tests__/integration.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ describe('toMatchImageSnapshot', () => {
103103
const imageTypedArray = new Uint8Array(imageData.buffer);
104104
expect(() => expect(imageTypedArray).toMatchImageSnapshot()).not.toThrowError();
105105
});
106+
107+
it('should work with base64 encoded strings', () => {
108+
const imageString = imageData.toString('base64');
109+
expect(() => expect(imageString).toMatchImageSnapshot()).not.toThrowError();
110+
});
106111
});
107112

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

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ function toBuffer(data) {
2828
if (data == null || Buffer.isBuffer(data)) {
2929
return data;
3030
}
31+
if (typeof data === 'string') {
32+
return Buffer.from(data, 'base64');
33+
}
3134

3235
return Buffer.from(data);
3336
}

0 commit comments

Comments
 (0)