Fix MetaService unit tests (#9319)
This commit is contained in:
parent
f4f293d5f3
commit
e8a49c9c9e
|
@ -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,
|
||||||
|
|
|
@ -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();
|
|
||||||
});*/
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue