Unit Testing: Vue3 Vitest Pinia: AssertionError: expected "spy" to be called at least once

627 Views Asked by At

I want to unit test one of my Pinia stores. I'm using script setup syntax and have two async functions inside.

async function runDiagnostic(diagnosticType: DiagnosticType) {
  // running diagnostic API call (different by type)
}

async function runAllDiagnostics() {
  await Promise.all([
    runDiagnostic(DiagnosticType.Ping),
    runDiagnostic(DiagnosticType.Mqtt),
    runDiagnostic(DiagnosticType.Dps),
    runDiagnostic(DiagnosticType.Blob)
  ]);
}

return {
  runDiagnostic,
  runAllDiagnostics
}

and have such test which is failing:

import { setActivePinia, createPinia } from 'pinia';
import { useDiagnostics } from '@/stores/diagnostics';
import { DiagnosticType } from '@/types/diagnostics.model';

describe('Diagnostics Store', () => {
  beforeEach(() => {
    setActivePinia(createPinia());
  });

  it('should run all diagnostics on runAllDiagnostics', async () => {
    const diagnosticsStore = useDiagnostics();
    const runDiagnosticSpy = vi.spyOn(diagnosticsStore, 'runDiagnostic');
    expect(runDiagnosticSpy).not.toHaveBeenCalled();
    await diagnosticsStore.runAllDiagnostics();
    expect(runDiagnosticSpy).toHaveBeenCalled();
  });
});

Running tests ends with AssertionError: expected "spy" to be called at least once

Already tried flushPromises() after but it doesn't help. Any clues what's wrong with this code?

0

There are 0 best solutions below