mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-05-05 17:05:55 +02:00
enhance(backend/test): Migrate tests to vitest (#16935)
* wip * update fake-timers and migrate * fix * remove jest-mock * fix * fix * fix * fix * attempt to fix unit tests * attempt to fix e2e tests * fix federation test [ci skip] * attempt to fix e2e tests * fix typecheck * fix unit tests * fix * attempt to fix e2e * fix * Revert "attempt to fix e2e" This reverts commitb7b7b05d85. * attempt to fix e2e * revert attempt to fix e2e * update deps * update vitest * migrate * attempt to fix e2e * update * fix * remove vite swc plugin as oxc parser can handle decorators * attempt to fix drive/files/create test * Revert "attempt to fix drive/files/create test" This reverts commit4715153375. * fix: エンドポイントにまつわるテストをunitからe2eに移動 * attempt to fix e2e * remove swc * attempt to fix e2e * Revert "attempt to fix e2e" This reverts commit9fb86a4076. * add logs for debug * attempt to fix e2e * Partially revert "attempt to fix e2e" This reverts commitfb0008c85a. * attempt to fix test * fix: attempt to fix test * Revert "fix: attempt to fix test" This reverts commited2f5c40e8. * Revert "attempt to fix test" This reverts commitd7329c46f1. * attempt to fix e2e * fix: surpass eventemitter warning by increasing defaultMaxListeners * attempt to fix e2e * fix * fix e2e not ending properly * exp: add hanging-process reporter for investigation * Revert "exp: add hanging-process reporter for investigation" This reverts commit26851f8282. * update changelog
This commit is contained in:
@@ -5,7 +5,8 @@
|
||||
|
||||
process.env.NODE_ENV = 'test';
|
||||
|
||||
import { jest } from '@jest/globals';
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import type { Mocked } from 'vitest';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { Redis } from 'ioredis';
|
||||
import type { TestingModule } from '@nestjs/testing';
|
||||
@@ -18,22 +19,32 @@ import { UtilityService } from '@/core/UtilityService.js';
|
||||
import { IdService } from '@/core/IdService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
|
||||
function mockRedis() {
|
||||
const hash = {} as any;
|
||||
const set = jest.fn((key: string, value) => {
|
||||
const ret = hash[key];
|
||||
hash[key] = value;
|
||||
return ret;
|
||||
function createMockRedis() {
|
||||
const store = new Map<string, string>();
|
||||
|
||||
const del = vi.fn((key: string) => {
|
||||
const existed = store.delete(key);
|
||||
return Promise.resolve(existed ? 1 : 0);
|
||||
});
|
||||
return set;
|
||||
|
||||
const set = vi.fn((key: string, value: string, ...args: any[]) => {
|
||||
const prev = store.get(key) ?? null;
|
||||
store.set(key, value);
|
||||
|
||||
// ioredis: SET key value ... GET => returns old value or null
|
||||
const hasGet = args.some(a => typeof a === 'string' && a.toUpperCase() === 'GET');
|
||||
return Promise.resolve(hasGet ? prev : 'OK');
|
||||
});
|
||||
|
||||
return { set, del };
|
||||
}
|
||||
|
||||
describe('FetchInstanceMetadataService', () => {
|
||||
let app: TestingModule;
|
||||
let fetchInstanceMetadataService: jest.Mocked<FetchInstanceMetadataService>;
|
||||
let federatedInstanceService: jest.Mocked<FederatedInstanceService>;
|
||||
let httpRequestService: jest.Mocked<HttpRequestService>;
|
||||
let redisClient: jest.Mocked<Redis>;
|
||||
let fetchInstanceMetadataService: Mocked<FetchInstanceMetadataService>;
|
||||
let federatedInstanceService: Mocked<FederatedInstanceService>;
|
||||
let httpRequestService: Mocked<HttpRequestService>;
|
||||
let redisClient: Mocked<Redis>;
|
||||
|
||||
beforeEach(async () => {
|
||||
app = await Test
|
||||
@@ -50,11 +61,11 @@ describe('FetchInstanceMetadataService', () => {
|
||||
})
|
||||
.useMocker((token) => {
|
||||
if (token === HttpRequestService) {
|
||||
return { getJson: jest.fn(), getHtml: jest.fn(), send: jest.fn() };
|
||||
return { getJson: vi.fn(), getHtml: vi.fn(), send: vi.fn() };
|
||||
} else if (token === FederatedInstanceService) {
|
||||
return { fetchOrRegister: jest.fn() };
|
||||
return { fetchOrRegister: vi.fn() };
|
||||
} else if (token === DI.redis) {
|
||||
return mockRedis;
|
||||
return createMockRedis();
|
||||
}
|
||||
return null;
|
||||
})
|
||||
@@ -62,23 +73,24 @@ describe('FetchInstanceMetadataService', () => {
|
||||
|
||||
app.enableShutdownHooks();
|
||||
|
||||
fetchInstanceMetadataService = app.get<FetchInstanceMetadataService>(FetchInstanceMetadataService) as jest.Mocked<FetchInstanceMetadataService>;
|
||||
federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService) as jest.Mocked<FederatedInstanceService>;
|
||||
redisClient = app.get<Redis>(DI.redis) as jest.Mocked<Redis>;
|
||||
httpRequestService = app.get<HttpRequestService>(HttpRequestService) as jest.Mocked<HttpRequestService>;
|
||||
fetchInstanceMetadataService = app.get<FetchInstanceMetadataService>(FetchInstanceMetadataService) as Mocked<FetchInstanceMetadataService>;
|
||||
federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService) as Mocked<FederatedInstanceService>;
|
||||
redisClient = app.get<Redis>(DI.redis) as Mocked<Redis>;
|
||||
httpRequestService = app.get<HttpRequestService>(HttpRequestService) as Mocked<HttpRequestService>;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
vi.resetAllMocks();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
test('Lock and update', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetchOrRegister.mockResolvedValue({ infoUpdatedAt: { getTime: () => { return now - 10 * 1000 * 60 * 60 * 24; } } } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
const tryLockSpy = vi.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = vi.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
||||
@@ -88,12 +100,11 @@ describe('FetchInstanceMetadataService', () => {
|
||||
});
|
||||
|
||||
test('Lock and don\'t update', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetchOrRegister.mockResolvedValue({ infoUpdatedAt: { getTime: () => now } } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
const tryLockSpy = vi.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = vi.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
||||
@@ -103,13 +114,12 @@ describe('FetchInstanceMetadataService', () => {
|
||||
});
|
||||
|
||||
test('Do nothing when lock not acquired', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetchOrRegister.mockResolvedValue({ infoUpdatedAt: { getTime: () => now - 10 * 1000 * 60 * 60 * 24 } } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
await fetchInstanceMetadataService.tryLock('example.com');
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
const tryLockSpy = vi.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = vi.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
||||
@@ -119,13 +129,12 @@ describe('FetchInstanceMetadataService', () => {
|
||||
});
|
||||
|
||||
test('Do when lock not acquired but forced', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetchOrRegister.mockResolvedValue({ infoUpdatedAt: { getTime: () => now - 10 * 1000 * 60 * 60 * 24 } } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
await fetchInstanceMetadataService.tryLock('example.com');
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
const tryLockSpy = vi.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = vi.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any, true);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(0);
|
||||
|
||||
Reference in New Issue
Block a user