Giter VIP home page Giter VIP logo

discordjs-mock's Introduction

Discord.js v14 Mocks

This repository contains mocks for the Discord.js library.

I created this repository because I wanted to write tests for my Discord bots, but I didn't want to use the real Discord.js library. I wanted to use a mock library that would allow me to test my code without having to worry about the Discord API. I also wanted to be able to test my code without having to worry about the Discord API.

I found a repository that had this as a good base but I wanted more control over the dependencies and the mocks. So I created this repository.

The source was: AnswerOverflow and their Discord.js Mocks

Example

The example below is using DiscordX and a simple singleton class to create the mocks. This is just an example and you can use any library you want to create the mocks.

/* eslint-disable @typescript-eslint/no-explicit-any */
import { jest } from '@jest/globals';
import {
  mockClientUser,
  mockGuild,
  mockGuildMember,
  mockTextChannel,
  mockUser,
} from '@shoginn/discordjs-mock';
import { Client, Guild, TextBasedChannel, TextChannel, User } from 'discord.js';
import { Client as ClientX } from 'discordx';
import { singleton } from 'tsyringe';

@singleton()
export class Mock {
  private client!: Client;
  private guild!: Guild;
  private channel!: TextChannel;
  private user!: User;

  getClient(withBots: boolean = false): Client {
    if (withBots) {
      const botUser = mockUser(this.client, { bot: true });
      mockGuildMember({
        client: this.client,
        user: botUser,
        guild: this.guild,
      });
    }
    return this.client;
  }
  getUser(): User {
    return this.user;
  }
  getGuild(): Guild {
    return this.guild;
  }
  getChannel(): TextBasedChannel {
    return this.channel;
  }
  constructor() {
    this.mockClient();
    this.mockGuild();
    this.mockUser();
    this.mockChannel();
  }

  private mockClient(): void {
    this.client = new ClientX({ intents: [], guards: [] });
    mockClientUser(this.client);

    this.client.login = jest.fn(() => Promise.resolve('LOGIN_TOKEN')) as any;
  }

  private mockGuild(): void {
    this.guild = mockGuild(this.client);
  }
  private mockChannel(): void {
    this.channel = mockTextChannel(this.client, this.guild);
  }

  private mockUser(): void {
    this.user = mockUser(this.client);
  }
}

discordjs-mock's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar renovate[bot] avatar shoginn avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

beaniemen

discordjs-mock's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • wagoid/commitlint-github-action v6.0.1
  • actions/checkout v4
  • actions/setup-node v4
  • pnpm/action-setup v3
  • actions/cache v4
  • codecov/codecov-action v4
  • googleapis/release-please-action v4
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-node v4
  • pnpm/action-setup v3
  • actions/cache v4
npm
package.json
  • @sapphire/snowflake ^3.5.3
  • discord.js ^14.15.3
  • lodash ^4.17.21
  • @tsconfig/strictest ^2.0.5
  • @types/jest ^29.5.12
  • @types/lodash ^4.17.4
  • @types/node ^20.14.1
  • @typescript-eslint/eslint-plugin ^7.12.0
  • eslint ^8.57.0
  • eslint-config-prettier ~9.1.0
  • eslint-plugin-deprecation ^3.0.0
  • eslint-plugin-import ^2.29.1
  • eslint-plugin-jest ^28.5.0
  • eslint-plugin-prettier ^5.1.3
  • eslint-plugin-simple-import-sort ^12.1.0
  • eslint-plugin-unicorn ^54.0.0
  • jest ^29.7.0
  • prettier ^3.3.0
  • ts-jest ^29.1.4
  • ts-jest-resolver ^2.0.1
  • ts-node ^10.9.2
  • tsup ^8.1.0
  • typescript ^5.4.5
  • typescript-eslint ^7.12.0
  • discord.js ^14.13.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.