开玩笑手动模拟不起作用

时间:2017-11-16 07:38:19

标签: jestjs babel-jest

我正在尝试为自定义模块进行手动模拟 - > help.js 我创建了手动模拟     | -models     | | - 嘲笑            | -help.js     | help.js     | test.js

嘲笑像help.js这样的函数

 jest.mock('./help', () => {
   return {
     add: jest.fn(() => jest.fn()),
     delete: jest.fn(() => jest.fn()),
     toggle: jest.fn(() => jest.fn())
   };

我试图在test.js中使用mocking模块,比如

jest.mock('./_mocks_/help');
const help =require()
test('add',()=>{
expect(help.add).toHaveBeenCalledWith({
      type: 'test',
        });
})

它再次调用main实现模块而不是mock模块。 请帮助我。我是开玩笑的新手 提前致谢

1 个答案:

答案 0 :(得分:0)

`
const { MessageEmbed } = require("discord.js");
const { readdirSync } = require("fs");
const db = require('quick.db');
const { stripIndents } = require("common-tags");
const { cyan } = require("../../JSON/colours.json");
const { PREFIX } = require('../../config');

module.exports = {
    config: {
        name: "help",
        aliases: ["h"],
        usage: "[command name] (optional)",
        category: "info",
        description: "Displays all commands that the bot has.",
        accessableby: "everyone"
    },
    run: async (bot, message, args) => {
        let prefix;
        let fetched = await db.fetch(`prefix_${message.guild.id}`);

        if (fetched === null) {
            prefix = PREFIX
        } else {
            prefix = fetched
        }

        const embed = new MessageEmbed()
            .setColor(cyan)
            .setAuthor(`${message.guild.me.displayName} Help`, message.guild.iconURL())
            .setThumbnail(bot.user.displayAvatarURL())

        if (!args[0]) {

            const sembed = new MessageEmbed()
                .setAuthor(`${message.guild.me.displayName}`, message.guild.iconURL())
                .setColor("GREEN")
                .setDescription('**Message Has Been Sent to You In DMs!**')
            message.channel.send(sembed).then(msg => {
                msg.delete({ timeout: 10000 });
            })

            const categories = readdirSync("./commands/")

            embed.setDescription(`**These Are the Available Commands For ${message.guild.me.displayName}\nBot's Global Prefix Is \`${PREFIX}\`\nServer Prefix Is \`${prefix}\`\n\nFor Help Related To A Particular Command Type -\n\`${prefix}help [command name | alias]\`**`)
            embed.setFooter(`${message.guild.me.displayName} | Total Commands - ${bot.commands.size - 1}`, bot.user.displayAvatarURL());

            categories.forEach(category => {
                const dir = bot.commands.filter(c => c.config.category === category)
                const capitalise = category.slice(0, 1).toUpperCase() + category.slice(1)
                try {
                    embed.addField(` ${capitalise} [${dir.size}] - `, dir.map(c => `\`${c.config.name}\``).join(" "))
                } catch (e) {
                    console.log(e)
                }
            })

            return message.author.send(embed)
        } else {
            let command = bot.commands.get(bot.aliases.get(args[0].toLowerCase()) || args[0].toLowerCase())
            if (!command) return message.channel.send(embed.setTitle("**Invalid Command!**").setDescription(`**Do \`${prefix}help\` For the List Of the Commands!**`))
            command = command.config

            embed.setDescription(stripIndents`**The Bot's Global Prefix Is \`${PREFIX}\`**\n
            **Server Prefix Is \`${prefix}\`**\n
            ** Command -** ${command.name.slice(0, 1).toUpperCase() + command.name.slice(1)}\n
            ** Description -** ${command.description || "No Description provided."}\n
            **Category -** ${command.category}\n
            ** Usage -** ${command.usage ? `\`${prefix}${command.name} ${command.usage}\`` : "No Usage"}\n
            ** Accessible by -** ${command.accessableby || "everyone"}\n
            ** Aliases -** ${command.aliases ? command.aliases.join(", ") : "None."}`)
            embed.setFooter(message.guild.name, message.guild.iconURL())

            return message.channel.send(embed)
        }
    }
}; `