test: replace jest with vitest (#10472)

* chore: vitest config

* feat: vitest

* fix: do not actually create ws

* chore: config

* chore: lockfile

* chore: revert downgrade, up node

* chore: package - 'git add -A'

* chore: delete mock-socket

* chore: delete mock-socket

* fix: lockfile

---------

Co-authored-by: almeidx <github@almeidx.dev>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
pat
2024-10-07 01:26:53 +11:00
committed by GitHub
parent bb04e09f8b
commit 24128a3c45
21 changed files with 267 additions and 219 deletions

View File

@@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/dot-notation */
// @ts-nocheck
import { EventEmitter } from 'node:events';
import { vitest, describe, test, expect, beforeEach } from 'vitest';
import * as _DataStore from '../src/DataStore';
import {
createVoiceConnection,
@@ -14,34 +15,42 @@ import {
} from '../src/VoiceConnection';
import * as _AudioPlayer from '../src/audio/AudioPlayer';
import { PlayerSubscription as _PlayerSubscription } from '../src/audio/PlayerSubscription';
import * as _Networking from '../src/networking/Networking';
import * as Networking from '../src/networking/Networking';
import type { DiscordGatewayAdapterLibraryMethods } from '../src/util/adapter';
jest.mock('../src/audio/AudioPlayer');
jest.mock('../src/audio/PlayerSubscription');
jest.mock('../src/DataStore');
jest.mock('../src/networking/Networking');
vitest.mock('../src/audio/AudioPlayer');
vitest.mock('../src/audio/PlayerSubscription');
vitest.mock('../src/DataStore');
vitest.mock('../src/networking/Networking', async (importOriginal) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const actual = await importOriginal<typeof import('../src/networking/Networking')>();
const Networking = actual.Networking;
Networking.prototype.createWebSocket = vitest.fn();
return {
...actual,
Networking,
};
});
const DataStore = _DataStore as unknown as jest.Mocked<typeof _DataStore>;
const Networking = _Networking as unknown as jest.Mocked<typeof _Networking>;
const AudioPlayer = _AudioPlayer as unknown as jest.Mocked<typeof _AudioPlayer>;
const PlayerSubscription = _PlayerSubscription as unknown as jest.Mock<_PlayerSubscription>;
const DataStore = _DataStore as unknown as vitest.Mocked<typeof _DataStore>;
const AudioPlayer = _AudioPlayer as unknown as vitest.Mocked<typeof _AudioPlayer>;
const PlayerSubscription = _PlayerSubscription as unknown as vitest.Mock<_PlayerSubscription>;
Networking.Networking.mockImplementation(function mockedConstructor() {
this.state = {};
return this;
const _NetworkingClass = Networking.Networking;
vitest.spyOn(Networking, 'Networking').mockImplementation((...args) => {
return new _NetworkingClass(...args);
});
function createFakeAdapter() {
const sendPayload = jest.fn();
const sendPayload = vitest.fn();
sendPayload.mockReturnValue(true);
const destroy = jest.fn();
const destroy = vitest.fn();
const libMethods: Partial<DiscordGatewayAdapterLibraryMethods> = {};
return {
sendPayload,
destroy,
libMethods,
creator: jest.fn((methods) => {
creator: vitest.fn((methods) => {
Object.assign(libMethods, methods);
return {
sendPayload,
@@ -124,7 +133,7 @@ describe('createVoiceConnection', () => {
adapterCreator: existingAdapter.creator,
});
const stateSetter = jest.spyOn(existingVoiceConnection, 'state', 'set');
const stateSetter = vitest.spyOn(existingVoiceConnection, 'state', 'set');
// @ts-expect-error: We're testing
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
@@ -163,7 +172,7 @@ describe('createVoiceConnection', () => {
reason: VoiceConnectionDisconnectReason.EndpointRemoved,
};
const rejoinSpy = jest.spyOn(existingVoiceConnection, 'rejoin');
const rejoinSpy = vitest.spyOn(existingVoiceConnection, 'rejoin');
// @ts-expect-error: We're testing
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
@@ -222,7 +231,7 @@ describe('createVoiceConnection', () => {
describe('VoiceConnection#addServerPacket', () => {
test('Stores the packet and attempts to configure networking', () => {
const { voiceConnection } = createFakeVoiceConnection();
voiceConnection.configureNetworking = jest.fn();
voiceConnection.configureNetworking = vitest.fn();
const dummy = {
endpoint: 'discord.com',
guild_id: 123,
@@ -236,7 +245,7 @@ describe('VoiceConnection#addServerPacket', () => {
test('Overwrites existing packet', () => {
const { voiceConnection } = createFakeVoiceConnection();
voiceConnection['packets'].server = Symbol('old') as any;
voiceConnection.configureNetworking = jest.fn();
voiceConnection.configureNetworking = vitest.fn();
const dummy = {
endpoint: 'discord.com',
guild_id: 123,
@@ -250,7 +259,7 @@ describe('VoiceConnection#addServerPacket', () => {
test('Disconnects when given a null endpoint', () => {
const { voiceConnection } = createFakeVoiceConnection();
voiceConnection['packets'].server = Symbol('old') as any;
voiceConnection.configureNetworking = jest.fn();
voiceConnection.configureNetworking = vitest.fn();
const dummy = {
endpoint: null,
guild_id: 123,
@@ -344,7 +353,7 @@ describe('VoiceConnection#configureNetworking', () => {
adapter,
});
expect((voiceConnection.state as unknown as VoiceConnectionConnectingState).networking).toBeInstanceOf(
Networking.Networking,
_NetworkingClass,
);
});
});
@@ -399,24 +408,24 @@ describe('VoiceConnection#onNetworkingClose', () => {
describe('VoiceConnection#onNetworkingStateChange', () => {
test('Does nothing when status code identical', () => {
const { voiceConnection } = createFakeVoiceConnection();
const stateSetter = jest.spyOn(voiceConnection, 'state', 'set');
const stateSetter = vitest.spyOn(voiceConnection, 'state', 'set');
voiceConnection['onNetworkingStateChange'](
{ code: _Networking.NetworkingStatusCode.Ready } as any,
{ code: _Networking.NetworkingStatusCode.Ready } as any,
{ code: Networking.NetworkingStatusCode.Ready } as any,
{ code: Networking.NetworkingStatusCode.Ready } as any,
);
voiceConnection['onNetworkingStateChange'](
{ code: _Networking.NetworkingStatusCode.Closed } as any,
{ code: _Networking.NetworkingStatusCode.Closed } as any,
{ code: Networking.NetworkingStatusCode.Closed } as any,
{ code: Networking.NetworkingStatusCode.Closed } as any,
);
expect(stateSetter).not.toHaveBeenCalled();
});
test('Does nothing when not in Ready or Connecting states', () => {
const { voiceConnection } = createFakeVoiceConnection();
const stateSetter = jest.spyOn(voiceConnection, 'state', 'set');
const stateSetter = vitest.spyOn(voiceConnection, 'state', 'set');
const call = [
{ code: _Networking.NetworkingStatusCode.Ready } as any,
{ code: _Networking.NetworkingStatusCode.Closed } as any,
{ code: Networking.NetworkingStatusCode.Ready } as any,
{ code: Networking.NetworkingStatusCode.Closed } as any,
];
voiceConnection['_state'] = { status: VoiceConnectionStatus.Signalling } as any;
voiceConnection['onNetworkingStateChange'](call[0], call[1]);
@@ -429,7 +438,7 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
test('Transitions to Ready', () => {
const { voiceConnection } = createFakeVoiceConnection();
const stateSetter = jest.spyOn(voiceConnection, 'state', 'set');
const stateSetter = vitest.spyOn(voiceConnection, 'state', 'set');
voiceConnection['_state'] = {
...(voiceConnection.state as VoiceConnectionSignallingState),
status: VoiceConnectionStatus.Connecting,
@@ -437,8 +446,8 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
};
voiceConnection['onNetworkingStateChange'](
{ code: _Networking.NetworkingStatusCode.Closed } as any,
{ code: _Networking.NetworkingStatusCode.Ready } as any,
{ code: Networking.NetworkingStatusCode.Closed } as any,
{ code: Networking.NetworkingStatusCode.Ready } as any,
);
expect(stateSetter).toHaveBeenCalledTimes(1);
@@ -447,7 +456,7 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
test('Transitions to Connecting', () => {
const { voiceConnection } = createFakeVoiceConnection();
const stateSetter = jest.spyOn(voiceConnection, 'state', 'set');
const stateSetter = vitest.spyOn(voiceConnection, 'state', 'set');
voiceConnection['_state'] = {
...(voiceConnection.state as VoiceConnectionSignallingState),
status: VoiceConnectionStatus.Connecting,
@@ -455,8 +464,8 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
};
voiceConnection['onNetworkingStateChange'](
{ code: _Networking.NetworkingStatusCode.Ready } as any,
{ code: _Networking.NetworkingStatusCode.Identifying } as any,
{ code: Networking.NetworkingStatusCode.Ready } as any,
{ code: Networking.NetworkingStatusCode.Identifying } as any,
);
expect(stateSetter).toHaveBeenCalledTimes(1);
@@ -598,7 +607,7 @@ describe('VoiceConnection#subscribe', () => {
test('Does nothing in Destroyed state', () => {
const { voiceConnection } = createFakeVoiceConnection();
const player = new AudioPlayer.AudioPlayer();
player['subscribe'] = jest.fn();
player['subscribe'] = vitest.fn();
voiceConnection.state = { status: VoiceConnectionStatus.Destroyed };
expect(voiceConnection.subscribe(player)).toBeUndefined();
expect(player['subscribe']).not.toHaveBeenCalled();
@@ -610,7 +619,7 @@ describe('VoiceConnection#subscribe', () => {
const adapter = (voiceConnection.state as VoiceConnectionSignallingState).adapter;
const player = new AudioPlayer.AudioPlayer();
const dummy = Symbol('dummy');
player['subscribe'] = jest.fn().mockImplementation(() => dummy);
player['subscribe'] = vitest.fn().mockImplementation(() => dummy);
expect(voiceConnection.subscribe(player)).toEqual(dummy);
expect(player['subscribe']).toHaveBeenCalledWith(voiceConnection);
expect(voiceConnection.state).toMatchObject({
@@ -624,7 +633,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
test('Does nothing in Destroyed state', () => {
const { voiceConnection } = createFakeVoiceConnection();
const subscription = new PlayerSubscription(voiceConnection, new AudioPlayer.AudioPlayer());
subscription.unsubscribe = jest.fn();
subscription.unsubscribe = vitest.fn();
voiceConnection.state = { status: VoiceConnectionStatus.Destroyed };
voiceConnection['onSubscriptionRemoved'](subscription);
@@ -635,7 +644,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
test('Does nothing when subscription is not the same as the stored one', () => {
const { voiceConnection } = createFakeVoiceConnection();
const subscription = new PlayerSubscription(voiceConnection, new AudioPlayer.AudioPlayer());
subscription.unsubscribe = jest.fn();
subscription.unsubscribe = vitest.fn();
voiceConnection.state = { ...(voiceConnection.state as VoiceConnectionSignallingState), subscription };
voiceConnection['onSubscriptionRemoved'](Symbol('new subscription') as any);
@@ -649,7 +658,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
test('Unsubscribes in a live state with matching subscription', () => {
const { voiceConnection } = createFakeVoiceConnection();
const subscription = new PlayerSubscription(voiceConnection, new AudioPlayer.AudioPlayer());
subscription.unsubscribe = jest.fn();
subscription.unsubscribe = vitest.fn();
voiceConnection.state = { ...(voiceConnection.state as VoiceConnectionSignallingState), subscription };
voiceConnection['onSubscriptionRemoved'](subscription);
@@ -667,7 +676,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
const oldNetworking = new Networking.Networking({} as any, false);
oldNetworking.state = {
code: _Networking.NetworkingStatusCode.Ready,
code: Networking.NetworkingStatusCode.Ready,
connectionData: {} as any,
connectionOptions: {} as any,
udp: new EventEmitter() as any,
@@ -697,7 +706,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
const oldNetworking = new Networking.Networking({} as any, false);
oldNetworking.state = {
code: _Networking.NetworkingStatusCode.Ready,
code: Networking.NetworkingStatusCode.Ready,
connectionData: {} as any,
connectionOptions: {} as any,
udp,
@@ -726,7 +735,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
const newNetworking = new Networking.Networking({} as any, false);
newNetworking.state = {
code: _Networking.NetworkingStatusCode.Ready,
code: Networking.NetworkingStatusCode.Ready,
connectionData: {} as any,
connectionOptions: {} as any,
udp: new EventEmitter() as any,
@@ -749,7 +758,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
describe('Adapter', () => {
test('onVoiceServerUpdate', () => {
const { adapter, voiceConnection } = createFakeVoiceConnection();
voiceConnection['addServerPacket'] = jest.fn();
voiceConnection['addServerPacket'] = vitest.fn();
const dummy = Symbol('dummy') as any;
adapter.libMethods.onVoiceServerUpdate!(dummy);
expect(voiceConnection['addServerPacket']).toHaveBeenCalledWith(dummy);
@@ -757,7 +766,7 @@ describe('Adapter', () => {
test('onVoiceStateUpdate', () => {
const { adapter, voiceConnection } = createFakeVoiceConnection();
voiceConnection['addStatePacket'] = jest.fn();
voiceConnection['addStatePacket'] = vitest.fn();
const dummy = Symbol('dummy') as any;
adapter.libMethods.onVoiceStateUpdate!(dummy);
expect(voiceConnection['addStatePacket']).toHaveBeenCalledWith(dummy);