From e8a49c9c9e6d818f131f3afa8e9630ed08a13a31 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Wed, 14 Dec 2022 14:26:25 +0900 Subject: [PATCH] Fix MetaService unit tests (#9319) --- packages/backend/jest.config.cjs | 2 +- packages/backend/test/unit/MetaService.ts | 20 ++++---------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 61f408c1c..f0a3dc16c 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -115,7 +115,7 @@ module.exports = { resolver: './jest-resolver.cjs', // Automatically restore mock state between every test - // restoreMocks: false, + restoreMocks: true, // The root directory that Jest should scan for tests and modules within // rootDir: undefined, diff --git a/packages/backend/test/unit/MetaService.ts b/packages/backend/test/unit/MetaService.ts index dbb5a2427..26649d92a 100644 --- a/packages/backend/test/unit/MetaService.ts +++ b/packages/backend/test/unit/MetaService.ts @@ -10,14 +10,10 @@ import { MetaService } from '@/core/MetaService.js'; import { CoreModule } from '@/core/CoreModule.js'; import type { DataSource } from 'typeorm'; import type { TestingModule } from '@nestjs/testing'; -import type { MockFunctionMetadata } from 'jest-mock'; - -const moduleMocker = new ModuleMocker(global); describe('MetaService', () => { let app: TestingModule; let metaService: MetaService; - let metasRepository: MetasRepository; beforeAll(async () => { app = await Test.createTestingModule({ @@ -30,40 +26,32 @@ describe('MetaService', () => { app.enableShutdownHooks(); metaService = app.get(MetaService, { strict: false }); - metasRepository = app.get(DI.metasRepository, { strict: false }); + + // Make it cached + await metaService.fetch(); }); afterAll(async () => { await app.close(); }); - /* なんか動かない it('fetch (cache)', async () => { const db = app.get(DI.db); - const originalFunction = db.transaction; const spy = jest.spyOn(db, 'transaction'); - spy.mockImplementation((...args) => originalFunction(...args)); const result = await metaService.fetch(); expect(result.id).toBe('x'); expect(spy).toHaveBeenCalledTimes(0); - - spy.mockRestore(); }); it('fetch (force)', async () => { const db = app.get(DI.db); - const originalFunction = db.transaction; const spy = jest.spyOn(db, 'transaction'); - // 何故かここで無限再帰する db.transaction がspyのままになっている? - spy.mockImplementation((...args) => originalFunction(...args)); const result = await metaService.fetch(true); expect(result.id).toBe('x'); expect(spy).toHaveBeenCalledTimes(1); - - spy.mockRestore(); - });*/ + }); });