jeudi 26 novembre 2020

Jest does not see the mocked object

Faced an issue writing jest test for node js file listed below:

const logger = require('../services/customLogger');
const MODULE_NAME = 'groupMapper.js';

const { Sequelize } = require('sequelize');
const group = require('../models/group.model');

// Passing a connection URI for postgres
const sequelize = new Sequelize('postgres://...');

const Group = sequelize.define('Group', group, {});

const groupMapper = {

    getRecords () {
        logger.info(`[${MODULE_NAME}]: findAll() invoked without params`);
        return Group.findAll();
    },

    getRecordById (id) {
       ...
    },
    ...
};

module.exports = groupMapper;

Ttest fails with TypeError: Cannot read property 'findAll' of undefined. Any ideas what I'm doing wrong? Didn't get the reason why Group is undefined

const groupMapper = require('./groupMapper.js');

const logger = require('../services/customLogger');
const { Sequelize } = require('sequelize');

jest.mock('../services/customLogger');
jest.mock('sequelize');



describe('groupMapper', () => {

    beforeAll(() => {
        const Group = {
            findAll: () => jest.fn()
        }
    });

    test('should invoke logger and Group method', () => {
        groupMapper.getRecords();
        expect(logger).toHaveBeenCalled();
    });

})

Aucun commentaire:

Enregistrer un commentaire