Q: How to fix toArray is not undefined in Jest I'm create a unit test with Jest and run the tests I get a error with the test: "TypeError: db.find(...).toArray is not a function". if someone can help me I'll be grateful.
greetings :)
'use strict';
const DB = require('../../../../app/databases/mongo');
jest.mock('../../../../app/databases/mongo');

const noteController = require('../../../../app/controllers/note');

function functionMock(returnValue) {
    const find = jest.fn(() => Promise.resolve(returnValue));
    return jest.fn(() => ({
        find: () => ({ find })
    }));
}

describe('noteController', () => {

    beforeAll(() => {
        DB.getDb.mockImplementation(functionMock(null));
    });

    describe('note controller', () => {
        describe('find', () => {
            it('note list', async done => {
                const ctx = jest.fn();              
                await noteController.find(ctx)
                .then(result => {
                    expect(result).toBeDefined();
                });
                done();
            })
        })
    })

});

I expect the output with data in the promise resolve but i get: "TypeError: db.find(...).toArray is not a function"

A: Well, you don't show us where db comes from, but my guess is that db.find(...) does not return a result set.
Change your code to be:
var dbResult = db.find(...)
expect(dbResult.toArray).toBeDefined

That will allow you to assert that you have an object with the toArray method defined.
There's probably a better way to assert that, but you didn't include enough code to figure it out so I'm guessing.
