Skip to content

Commit 10cfd37

Browse files
committed
test(MongoBinary): add test for "getPath" to trigger download if no binary is found
1 parent 72c8199 commit 10cfd37

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ describe('MongoBinary', () => {
3131
// cleanup
3232
afterEach(() => {
3333
tmpDir.removeCallback();
34-
(MongoBinaryDownload as jest.Mock).mockClear();
35-
mockGetMongodPath.mockClear();
34+
jest.restoreAllMocks(); // restore all mocked functions to original
3635
DryMongoBinary.binaryCache.clear();
36+
delete process.env[envName(ResolveConfigVariables.RUNTIME_DOWNLOAD)];
3737
});
3838

3939
describe('getPath', () => {
@@ -65,10 +65,22 @@ describe('MongoBinary', () => {
6565
expect(gotVersionPath).toEqual(mockedPath);
6666
});
6767

68+
it('should trigger an download if binary dosnt exist', async () => {
69+
const mockPath = '/path/to/downloaded/binary';
70+
process.env[envName(ResolveConfigVariables.RUNTIME_DOWNLOAD)] = 'true';
71+
jest.spyOn(MongoBinary, 'download').mockResolvedValue(mockPath);
72+
jest.spyOn(DryMongoBinary, 'locateBinary').mockResolvedValue(undefined);
73+
74+
const output = await MongoBinary.getPath();
75+
expect(output).toBe(mockPath);
76+
expect(DryMongoBinary.locateBinary).toHaveBeenCalledTimes(1);
77+
expect(MongoBinary.download).toHaveBeenCalledTimes(1);
78+
});
79+
6880
describe('systemBinary', () => {
6981
const sysBinaryPath = '/path/to/binary';
7082
beforeEach(() => {
71-
jest.clearAllMocks();
83+
jest.resetAllMocks(); // reset all mocked functions information & set implementation to blank "jest.fn()"
7284
jest
7385
.spyOn(utils, 'pathExists') // mock this to be sure that "pathExists" is "true" for "sysBinaryPath"
7486
.mockImplementation((path) => Promise.resolve(path === sysBinaryPath));

0 commit comments

Comments
 (0)