gpt4 book ai didi

javascript - 开 Jest 测试不和谐机器人命令

转载 作者:行者123 更新时间:2023-12-02 21:18:05 26 4
gpt4 key购买 nike

所以我有一个使用模块导出的文件,它有 4 个字段,其中一个执行字段需要 2 个参数,本质上是一个函数。它不会返回任何内容,而是使用 Discord.js 并运行此 message.channel.send('Pong');。我想用 Jest 来测试这个我如何能:1 - 确保使用“Pong”作为参数调用 message.channel.send2 - 我如何模拟它,这样它实际上不会调用它(我只是想确保其中的文本,就像实际的参数是“Pong”,因为由于缺乏正确的消息,调用它不起作用对象)

我可以访问实际的命令并执行它,但我不确定如何检查 message.channel.send 的内容。我无法重建消息对象,因此可能还需要模拟。

我正在使用discord.js,但这并不重要。

我还必须测试具有返回值的函数的命令,那么我应该如何处理它们?

最佳答案

你可以试试这个:

const Discord = require('discord.js')

// replace this with whatever the execute command is
// e.g. const ping = require('./commands/ping').execute
const ping = async (message, args) => {
message.channel.send('Pong')
}

// a counter so that all the ids are unique
let count = 0

class Guild extends Discord.Guild {
constructor(client) {
super(client, {
// you don't need all of these but I just put them in to show you all the properties that Discord.js uses
id: count++,
name: '',
icon: null,
splash: null,
owner_id: '',
region: '',
afk_channel_id: null,
afk_timeout: 0,
verification_level: 0,
default_message_notifications: 0,
explicit_content_filter: 0,
roles: [],
emojis: [],
features: [],
mfa_level: 0,
application_id: null,
system_channel_flags: 0,
system_channel_id: null,
widget_enabled: false,
widget_channel_id: null
})
this.client.guilds.cache.set(this.id, this)
}
}

class TextChannel extends Discord.TextChannel {
constructor(guild) {
super(guild, {
id: count++,
type: 0
})
this.client.channels.cache.set(this.id, this)
}

// you can modify this for other things like attachments and embeds if you need
send(content) {
return this.client.actions.MessageCreate.handle({
id: count++,
type: 0,
channel_id: this.id,
content,
author: {
id: 'bot id',
username: 'bot username',
discriminator: '1234',
bot: true
},
pinned: false,
tts: false,
nonce: '',
embeds: [],
attachments: [],
timestamp: Date.now(),
edited_timestamp: null,
mentions: [],
mention_roles: [],
mention_everyone: false
})
}
}

class Message extends Discord.Message {
constructor(content, channel, author) {
super(channel.client, {
id: count++,
type: 0,
channel_id: channel.id,
content,
author,
pinned: false,
tts: false,
nonce: '',
embeds: [],
attachments: [],
timestamp: Date.now(),
edited_timestamp: null,
mentions: [],
mention_roles: [],
mention_everyone: false
}, channel)
}
}

const client = new Discord.Client()
const guild = new Guild(client)
const channel = new TextChannel(guild)

// the user that executes the commands
const user = {id: count++, username: 'username', discriminator: '1234'}

describe('ping', () => {
it('sends Pong', async () => {
await ping(new Message('ping', channel, user))
expect(channel.lastMessage.content).toBe('Pong')
})
})

您还需要将 testEnvironment: 'node' 放入您的 jest 配置中(请参阅 this issue )。

<小时/>

编辑

如果您需要获取时间戳等内容,还可以使用 Discord.SnowflakeUtil.generate() 生成 id。

关于javascript - 开 Jest 测试不和谐机器人命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60916450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com