开玩笑称为模块的导入依赖关系链

发布于 2025-01-31 02:04:20 字数 4079 浏览 4 评论 0原文

当我创建了Atom Jest显示器实例时,用我尚未发出的代码显示错误。我认为它与我尝试创建的课堂中导入的模块相连。我开始出现模拟模块错误,并随着每个模拟模块错误的更改,但结果使我陷入了第一个错误。如何修复它?

在Exaple中测试正确工作,但创建Atom投掷错误。 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;
    };
}

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;
    };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2025-02-07 02:04:20

如注释中的建议,只需要模拟atom模块中的所有导入

As recommended in comments, just need to mock all the imports in the Atom module

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文