Skip to content

Commit e197e5e

Browse files
committed
test(MongoBinaryDownload): add tests for "startDownload" (mocked)
1 parent c929960 commit e197e5e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownload.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { promises as fspromises } from 'fs';
22
import md5file from 'md5-file';
3+
import * as mkdirp from 'mkdirp';
34
import { DryMongoBinary } from '../DryMongoBinary';
45
import MongoBinaryDownload from '../MongoBinaryDownload';
6+
import MongoBinaryDownloadUrl from '../MongoBinaryDownloadUrl';
57
import { envName, ResolveConfigVariables } from '../resolveConfig';
68
import * as utils from '../utils';
79

810
jest.mock('md5-file');
11+
jest.mock('mkdirp');
912

1013
describe('MongoBinaryDownload', () => {
1114
afterEach(() => {
@@ -222,4 +225,38 @@ describe('MongoBinaryDownload', () => {
222225
du.getPath
223226
).toHaveBeenCalledTimes(1);
224227
});
228+
229+
it('should return the mongodb archive path (startDownload)', async () => {
230+
const downloadUrl = 'https://fastdl.mongodb.org/linux/mongod-something-something.tgz';
231+
const archivePath = '/path/to/archive.tgz';
232+
jest.spyOn(mkdirp, 'default').mockResolvedValue(void 0);
233+
jest.spyOn(fspromises, 'access').mockResolvedValue(void 0);
234+
jest.spyOn(MongoBinaryDownloadUrl.prototype, 'getDownloadUrl').mockResolvedValue(downloadUrl);
235+
236+
const du = new MongoBinaryDownload({ downloadDir: '/' });
237+
jest.spyOn(du, 'download').mockResolvedValue(archivePath);
238+
jest.spyOn(du, 'makeMD5check');
239+
240+
const returnValue = await du.startDownload();
241+
expect(returnValue).toEqual(archivePath);
242+
expect(du.makeMD5check).toHaveBeenCalledWith(`${downloadUrl}.md5`, archivePath);
243+
expect(du.download).toHaveBeenCalledWith(downloadUrl);
244+
});
245+
246+
it('should return the mongodb archive path (startDownload)', async () => {
247+
const customError = new Error('custom fs error');
248+
jest.spyOn(mkdirp, 'default').mockResolvedValue(void 0);
249+
jest.spyOn(fspromises, 'access').mockRejectedValue(customError);
250+
jest.spyOn(console, 'error').mockImplementation(() => void 0);
251+
252+
const du = new MongoBinaryDownload({ downloadDir: '/' });
253+
254+
try {
255+
await du.startDownload();
256+
fail('Expected "startDownload" to fail');
257+
} catch (err) {
258+
expect(err.message).toEqual(customError.message);
259+
expect(console.error).toHaveBeenCalledTimes(1);
260+
}
261+
});
225262
});

0 commit comments

Comments
 (0)