Explorar o código

test(app): drop 'as any' / 'as unknown as Crowi' from notify-export-result test

Replace the hand-rolled Crowi cast and the private-method spy with
vitest-mock-extended's mock<Crowi>. The auto-stubbed activityService
silences createActivity, and stubbing events.activity (the source for
this.activityEvent in the constructor) silences the emit, so the
private notifyExportResult no longer needs a spy at all.

Addresses review feedback from @yuki-takei on #11195.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
tomoyuki-t hai 2 semanas
pai
achega
badfb860cb

+ 12 - 9
apps/app/src/features/page-bulk-export/server/service/page-bulk-export-job-cron/notify-export-result-and-clean-up.integ.ts

@@ -1,4 +1,6 @@
+import type { EventEmitter } from 'node:events';
 import mongoose from 'mongoose';
+import { mock } from 'vitest-mock-extended';
 
 import { SupportedAction } from '~/interfaces/activity';
 import type Crowi from '~/server/crowi';
@@ -20,19 +22,20 @@ import instanciatePageBulkExportJobCronService, {
  * is the single choke point that finalizes the status, so it now backfills `completedAt`.
  */
 describe('PageBulkExportJobCronService.notifyExportResultAndCleanUp', () => {
-  const crowi = {
-    events: { activity: { emit: vi.fn() } },
-  } as unknown as Crowi;
+  // `mock<Crowi>` auto-stubs `crowi.activityService.createActivity` (called by the
+  // private `notifyExportResult`). `events.activity` is the source `this.activityEvent`
+  // is bound to in the service constructor, so stubbing it here is enough to silence
+  // `this.activityEvent.emit('updated', ...)` without spying on the private method.
+  const crowi = mock<Crowi>({
+    events: {
+      activity: mock<EventEmitter>(),
+    },
+  });
 
   beforeAll(async () => {
     await configManager.loadConfigs();
     instanciatePageBulkExportJobCronService(crowi);
-    // Stub out side effects (notification + fs/resource cleanup); we only assert on the job document.
-    vi.spyOn(
-      // biome-ignore lint/suspicious/noExplicitAny: notifyExportResult is private
-      pageBulkExportJobCronService as any,
-      'notifyExportResult',
-    ).mockResolvedValue(undefined);
+    // The fs/resource cleanup step is unrelated to the completedAt contract under test.
     vi.spyOn(
       // biome-ignore lint/style/noNonNullAssertion: instanciated above
       pageBulkExportJobCronService!,