0

Pretty much running the exact same code elsewhere however, this time, I am using rewire to test a non-exported function at helper.js.

When debugging the following I get to return await db.collection('foo') which returns undefined and throwing when attempting 'undefined.findOne()' naturally. My understanding is that db.collection('foo') should return mockDbConnect in this case allowing to chain findOne, which it is not doing...

Help is appreciated.

foo.test.js

const rewire = require('rewire');
const helper = rewire('../../../src/models/helper/helpers');

describe('model helpers', () => {
    describe('searchValue', () => {
        it('should return an instance', async () => {
            const searchValue = helper.__get__('searchValue')
            const mockFoo = { foo: '1' }
            const fooId = '1';
            const mockDbConnect = {
                collection: jest.fn().mockReturnThis(),
                findOne: jest.fn().mockReturnValue(mockFoo)
            }
            try {
                const res = await searchValue(fooId, mockDbConnect);
                expect(mockDbConnect.collection).toHaveBeenCalledWith('foos');
            } catch (err) {
                throw err;
            }
        });
    })
})

helpers.js

const searchValue = async (fooId, db) => {
    return await db
        .collection('foos')
        .findOne({ fooId: fooId });
};
Jack
  • 71
  • 3

0 Answers0