Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/test/store/receive-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ function simulateSymbolStoreHasNoCache() {
}));
}

// Returns an ArrayBuffer which contains only the bytes that
// are covered by the Uint8Array, making a copy if needed.
function extractArrayBuffer(bufferView: Uint8Array<ArrayBuffer>): ArrayBuffer {
if (
bufferView.byteOffset === 0 &&
bufferView.byteLength === bufferView.buffer.byteLength
) {
return bufferView.buffer;
}

// There was extra data at the start or at the end. Make a copy.
const copy = new Uint8Array(bufferView.byteLength);
copy.set(bufferView);
return copy.buffer;
}

describe('actions/receive-profile', function () {
beforeEach(() => {
// The SymbolStore requires the use of IndexedDB, ensure that it exists so that
Expand Down Expand Up @@ -676,10 +692,11 @@ describe('actions/receive-profile', function () {
case 'json':
return profileJSON;
case 'arraybuffer':
return toUint8Array(profileJSON).buffer as ArrayBuffer;
return extractArrayBuffer(toUint8Array(profileJSON));
case 'gzip':
return (await compress(toUint8Array(profileJSON)))
.buffer as ArrayBuffer;
return extractArrayBuffer(
await compress(toUint8Array(profileJSON))
);
default:
throw new Error('unknown profiler format');
}
Expand Down Expand Up @@ -1314,7 +1331,7 @@ describe('actions/receive-profile', function () {
const profileOrZip = await _fetchProfile(args);
expect(profileOrZip).toEqual({
responseType: 'PROFILE',
profile: profile.buffer,
profile: extractArrayBuffer(profile),
});
} catch (error) {
userFacingError = error;
Expand Down Expand Up @@ -1446,7 +1463,7 @@ describe('actions/receive-profile', function () {

const { getState, view } = await setupTestWithFile({
type: '',
payload: (await compress(serializeProfile(profile))).buffer,
payload: extractArrayBuffer(await compress(serializeProfile(profile))),
});
expect(view.phase).toBe('DATA_LOADED');
expect(ProfileViewSelectors.getProfile(getState()).meta.product).toEqual(
Expand Down Expand Up @@ -1478,7 +1495,7 @@ describe('actions/receive-profile', function () {

const { getState, view } = await setupTestWithFile({
type: 'application/gzip',
payload: (await compress(serializeProfile(profile))).buffer,
payload: extractArrayBuffer(await compress(serializeProfile(profile))),
});
expect(view.phase).toBe('DATA_LOADED');
expect(ProfileViewSelectors.getProfile(getState()).meta.product).toEqual(
Expand All @@ -1492,7 +1509,7 @@ describe('actions/receive-profile', function () {

const { getState, view } = await setupTestWithFile({
type: 'application/json',
payload: (await compress(serializeProfile(profile))).buffer,
payload: extractArrayBuffer(await compress(serializeProfile(profile))),
});
expect(view.phase).toBe('DATA_LOADED');
expect(ProfileViewSelectors.getProfile(getState()).meta.product).toEqual(
Expand All @@ -1506,7 +1523,7 @@ describe('actions/receive-profile', function () {
.mockImplementation(() => {});
const { view } = await setupTestWithFile({
type: 'application/gzip',
payload: (await compress('{}')).buffer,
payload: extractArrayBuffer(await compress('{}')),
});
expect(view.phase).toBe('FATAL_ERROR');

Expand All @@ -1525,14 +1542,9 @@ describe('actions/receive-profile', function () {
zip.file(fileName, serializedProfile);
const array = await zip.generateAsync({ type: 'uint8array' });

// Create a new ArrayBuffer instance and copy the data into it, in order
// to work around https://github.com/facebook/jest/issues/6248
const bufferCopy = new ArrayBuffer(array.buffer.byteLength);
new Uint8Array(bufferCopy).set(new Uint8Array(array.buffer));

return setupTestWithFile({
type: 'application/zip',
payload: bufferCopy,
payload: extractArrayBuffer(array as Uint8Array<ArrayBuffer>),
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/gz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function compress(
if (errorOrNull) {
reject(errorOrNull);
} else {
resolve(new Uint8Array(result.buffer as ArrayBuffer));
resolve(result);
}
});
});
Expand All @@ -67,7 +67,7 @@ export async function decompress(
if (errorOrNull) {
reject(errorOrNull);
} else {
resolve(new Uint8Array(result.buffer as ArrayBuffer));
resolve(result);
}
});
});
Expand Down