'Jest called chain of imported dependencies of module

When I created instance of Atom jest show error with piece of code that i haven't invoced. I assume it connects with modules that have imported in class that I try to create. I begin to mock modules showed in error, with every mocked module error was changed, but in result it brought me to first error. How to fix it?

Test in exaple work correctly but creating of Atom throw error. Atom.test.ts

import { Atom } from '../flowmanager/atom/Atom';
import Bot from './Bot';

describe('Atom ->', () => {
    const atom = new Atom({ name: 'Atom name' });

    const bot = new Bot({ name: 'daniel' });

    it('getName', () => {
        expect(bot.getName()).toBe('daniel');
    });
});

Atom.ts

import emojiStrip = require('emoji-strip');
import { Message } from '../messages/Message';
import MessageRandom from '../messages/MessageRandom';
import UserInputMessage from '../messages/types/UserInput';
import ConditionMessage from '../messages/types/Condition';
import { Session } from '../../user/session/Session';
import MessageTypes from '../../constants/MessageTypes';

export class Atom {
    public data: any;

    constructor(data = {}) {
        this.setData(data);
    }

    setData(data: any) {
        if (!_.isArray(data.messages)) {
            data.messages = [];
        }
        this.data = data;

        this.data.messages = _.map(this.getMessages(), message => {
            if (_.isArray(message.messages)) {
                message.messages = message.messages.map((nestedMessage: any) => new Message(nestedMessage));
            }
            return _.get(message, 'options.random') ? new MessageRandom(message) : new Message(message);
        });
    }

    findReply(userInput: any, session: Session) {
        return _findReply.call(
            this,
            (reply: any) => {
                if (!reply.content.text) return false;
                const preparedReplyText = emojiStrip(reply.content.text.replace(/\?/g, '')).trim().toLowerCase();
                return preparedReplyText === userInput.trim().toLowerCase();
            },
            session
        );
    }

    findReplyById(replyId: any, session: Session) {
        return _findReply.call(this, (reply: any) => reply._id === replyId, session);
    }

    getNextAtomIdByReply(input: any, session?: any) {
        const reply: any = this.findReply(input, session);
        if (!reply) {
            return null;
        }

        return this.getNextAtomIdByReplyId(reply._id, session);
    }

    getNextAtomIdByReplyId(replyId: any, session: Session) {
        const reply = this.findReplyById(replyId, session);

        return _.get(reply, 'nextAtom');
    }

    getName() {
        return this.data.name;
    }

    getId() {
        return this.data._id;
    }

    getFlow() {
        return this.data.flow;
    }

    getBotId() {
        return this.data.bot;
    }

    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    getMessages(session?: any) {
        return this.data.messages;
    }

    getReference() {
        return this.data.shortReference || this.data.reference;
    }

    isActive() {
        return this.data.disabled !== true;
    }

    isRoot() {
        return this.data.root === true;
    }

    needLogin() {
        return _.get(this.data, 'flow.needAuth') || _.get(this.data, 'options.needAuth');
    }

    getUserInputMessage() {
        const message = _.find(this.getMessages(), message => message.type === 'userInput');
        if (!message) {
            return null;
        }

        return new UserInputMessage(message);
    }

    getConditionMessage(session: Session, onReply = false) {
        const conditionMessage = _.find(this.getMessages(session), ({ type }) => type === MessageTypes.CONDITION);
        if (!conditionMessage || _.get(conditionMessage, 'options.condition.onReply', false) !== onReply) {
            return null;
        }
        return new ConditionMessage(conditionMessage);
    }

    canReturn() {
        return _.get(this.data, 'options.canReturn', true);
    }

    getOption(field: string) {
        return _.get(this.data, `options.${field}`);
    }

    public getEmailEvent = () => {
        return this.data.options?.emailEvent;
    };
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source