开玩笑称为模块的导入依赖关系链
当我创建了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如注释中的建议,只需要模拟
atom
模块中的所有导入As recommended in comments, just need to mock all the imports in the
Atom
module