Skip to content

Commit 508de3a

Browse files
silverwinddelvh
andauthored
Fix Uint8Array comparisons and update vitest (#26805)
Compare those `Uint8Array` via conversion to Array which are properly comparable, so that we don't have to worry about whether `TextEncoder` and `UInt8Array` from the environment are compatible or not. --------- Co-authored-by: delvh <[email protected]>
1 parent 7bc80cb commit 508de3a

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

package-lock.json

Lines changed: 31 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"svgo": "3.0.2",
8282
"updates": "14.4.0",
8383
"vite-string-plugin": "1.1.2",
84-
"vitest": "0.34.2"
84+
"vitest": "0.34.3"
8585
},
8686
"browserslist": [
8787
"defaults",

web_src/js/utils.test.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,22 @@ test('toAbsoluteUrl', () => {
133133
expect(() => toAbsoluteUrl('path')).toThrowError('unsupported');
134134
});
135135

136-
const uint8array = (s) => new TextEncoder().encode(s);
137136
test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => {
137+
// TextEncoder is Node.js API while Uint8Array is jsdom API and their outputs are not
138+
// structurally comparable, so we convert to array to compare. The conversion can be
139+
// removed once https://github.com/jsdom/jsdom/issues/2524 is resolved.
140+
const encoder = new TextEncoder();
141+
const uint8array = encoder.encode.bind(encoder);
142+
138143
expect(encodeURLEncodedBase64(uint8array('AA?'))).toEqual('QUE_'); // standard base64: "QUE/"
139144
expect(encodeURLEncodedBase64(uint8array('AA~'))).toEqual('QUF-'); // standard base64: "QUF+"
140145

141-
expect(decodeURLEncodedBase64('QUE/')).toEqual(uint8array('AA?'));
142-
expect(decodeURLEncodedBase64('QUF+')).toEqual(uint8array('AA~'));
143-
expect(decodeURLEncodedBase64('QUE_')).toEqual(uint8array('AA?'));
144-
expect(decodeURLEncodedBase64('QUF-')).toEqual(uint8array('AA~'));
146+
expect(Array.from(decodeURLEncodedBase64('QUE/'))).toEqual(Array.from(uint8array('AA?')));
147+
expect(Array.from(decodeURLEncodedBase64('QUF+'))).toEqual(Array.from(uint8array('AA~')));
148+
expect(Array.from(decodeURLEncodedBase64('QUE_'))).toEqual(Array.from(uint8array('AA?')));
149+
expect(Array.from(decodeURLEncodedBase64('QUF-'))).toEqual(Array.from(uint8array('AA~')));
145150

146151
expect(encodeURLEncodedBase64(uint8array('a'))).toEqual('YQ'); // standard base64: "YQ=="
147-
expect(decodeURLEncodedBase64('YQ')).toEqual(uint8array('a'));
148-
expect(decodeURLEncodedBase64('YQ==')).toEqual(uint8array('a'));
152+
expect(Array.from(decodeURLEncodedBase64('YQ'))).toEqual(Array.from(uint8array('a')));
153+
expect(Array.from(decodeURLEncodedBase64('YQ=='))).toEqual(Array.from(uint8array('a')));
149154
});

0 commit comments

Comments
 (0)