Jest 调用了模块的导入依赖链
Jest called chain of imported dependencies of module
当我创建 Atom jest 实例时,我没有调用一段代码显示错误。我假设它连接了我尝试创建的 class 中导入的模块。我开始模拟错误显示的模块,每个模拟模块错误都已更改,但结果它给我带来了第一个错误。如何解决?
示例中的测试工作正常,但 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;
};
}
按照评论中的建议,只需要模拟Atom
模块中的所有导入
当我创建 Atom jest 实例时,我没有调用一段代码显示错误。我假设它连接了我尝试创建的 class 中导入的模块。我开始模拟错误显示的模块,每个模拟模块错误都已更改,但结果它给我带来了第一个错误。如何解决?
示例中的测试工作正常,但 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;
};
}
按照评论中的建议,只需要模拟Atom
模块中的所有导入