Skip to content

Commit dc53ad7

Browse files
committed
Fix slow downloadFile test by stubbing URL.createObjectURL
This test was often reported as slow (>1000ms) when running the whole test suite. Measuring timing showed that most of the delay was in the first `URL.createObjectURL` call. The issue did not reproduce when running only the `downloadFile` tests. If changing the order in which different file types are tested, the slowness still only occurred on the first call, so isn't related to a particular file type. https://issues.chromium.org/issues/40269900 notes that this call involves a synchronous IPC call between renderer and browser process, so the issue may just be that the browser process is busier when the full suite runs. As `createObjectURL` has a simple signature, I think we can get away with just stubbing it.
1 parent 07c5bc8 commit dc53ad7

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/shared/test/download-file-test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { downloadFile } from '../download-file';
33
describe('download-file', () => {
44
let fakeLink;
55
let fakeDocument;
6+
let sandbox;
67

78
beforeEach(() => {
89
fakeLink = {
@@ -19,11 +20,19 @@ describe('download-file', () => {
1920
},
2021
};
2122

22-
sinon.spy(window, 'Blob');
23+
sandbox = sinon.createSandbox();
24+
sandbox.spy(window, 'Blob');
25+
26+
// We stub `URL.createObjectURL` both to control the returned URL but also
27+
// because it can occassionally be slow if called in the middle of a large
28+
// test run, leading to a slow test warning.
29+
//
30+
// Possibly relevant: https://issues.chromium.org/issues/40269900.
31+
sandbox.stub(URL, 'createObjectURL').returns('blob:1234');
2332
});
2433

2534
afterEach(() => {
26-
window.Blob.restore();
35+
sandbox.restore();
2736
});
2837

2938
function assertDownloadHappened(filename, fileContent, type) {
@@ -32,12 +41,9 @@ describe('download-file', () => {
3241
assert.calledWith(fakeDocument.body.removeChild, fakeLink);
3342

3443
assert.calledWith(window.Blob, [fileContent], { type });
44+
assert.calledWith(URL.createObjectURL, window.Blob.returnValues[0]);
3545

36-
assert.calledWith(
37-
fakeLink.setAttribute.firstCall,
38-
'href',
39-
sinon.match.string,
40-
);
46+
assert.calledWith(fakeLink.setAttribute.firstCall, 'href', 'blob:1234');
4147
assert.calledWith(fakeLink.setAttribute.secondCall, 'download', filename);
4248
assert.equal('hidden', fakeLink.style.visibility);
4349
}

0 commit comments

Comments
 (0)