Fix MetaService unit tests (#9319)

This commit is contained in:
Kagami Sascha Rosylight 2022-12-14 14:26:25 +09:00 committed by GitHub
parent f4f293d5f3
commit e8a49c9c9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 17 deletions

View File

@ -115,7 +115,7 @@ module.exports = {
resolver: './jest-resolver.cjs', resolver: './jest-resolver.cjs',
// Automatically restore mock state between every test // Automatically restore mock state between every test
// restoreMocks: false, restoreMocks: true,
// The root directory that Jest should scan for tests and modules within // The root directory that Jest should scan for tests and modules within
// rootDir: undefined, // rootDir: undefined,

View File

@ -10,14 +10,10 @@ import { MetaService } from '@/core/MetaService.js';
import { CoreModule } from '@/core/CoreModule.js'; import { CoreModule } from '@/core/CoreModule.js';
import type { DataSource } from 'typeorm'; import type { DataSource } from 'typeorm';
import type { TestingModule } from '@nestjs/testing'; import type { TestingModule } from '@nestjs/testing';
import type { MockFunctionMetadata } from 'jest-mock';
const moduleMocker = new ModuleMocker(global);
describe('MetaService', () => { describe('MetaService', () => {
let app: TestingModule; let app: TestingModule;
let metaService: MetaService; let metaService: MetaService;
let metasRepository: MetasRepository;
beforeAll(async () => { beforeAll(async () => {
app = await Test.createTestingModule({ app = await Test.createTestingModule({
@ -30,40 +26,32 @@ describe('MetaService', () => {
app.enableShutdownHooks(); app.enableShutdownHooks();
metaService = app.get<MetaService>(MetaService, { strict: false }); metaService = app.get<MetaService>(MetaService, { strict: false });
metasRepository = app.get<MetasRepository>(DI.metasRepository, { strict: false });
// Make it cached
await metaService.fetch();
}); });
afterAll(async () => { afterAll(async () => {
await app.close(); await app.close();
}); });
/*
it('fetch (cache)', async () => { it('fetch (cache)', async () => {
const db = app.get<DataSource>(DI.db); const db = app.get<DataSource>(DI.db);
const originalFunction = db.transaction;
const spy = jest.spyOn(db, 'transaction'); const spy = jest.spyOn(db, 'transaction');
spy.mockImplementation((...args) => originalFunction(...args));
const result = await metaService.fetch(); const result = await metaService.fetch();
expect(result.id).toBe('x'); expect(result.id).toBe('x');
expect(spy).toHaveBeenCalledTimes(0); expect(spy).toHaveBeenCalledTimes(0);
spy.mockRestore();
}); });
it('fetch (force)', async () => { it('fetch (force)', async () => {
const db = app.get<DataSource>(DI.db); const db = app.get<DataSource>(DI.db);
const originalFunction = db.transaction;
const spy = jest.spyOn(db, 'transaction'); const spy = jest.spyOn(db, 'transaction');
// 何故かここで無限再帰する db.transaction がspyのままになっている
spy.mockImplementation((...args) => originalFunction(...args));
const result = await metaService.fetch(true); const result = await metaService.fetch(true);
expect(result.id).toBe('x'); expect(result.id).toBe('x');
expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledTimes(1);
});
spy.mockRestore();
});*/
}); });