Reputation: 1651
I have a simple store:
export const useFormsContextStore = defineStore('formsContext', () => {
function makeSomeCalculations() {
// calculations
// and fire helper function
helper();
}
function helper() {
console.log('helper function called from pinia store');
}
return {
calculate: makeSomeCalculations,
calculateHelper: helper,
helper,
};
});
I want to test if helper() method is being called on calculate() execution, so created a unit test with configuration:
describe('Forms Context store tests', () => {
let pinia: Pinia | undefined = undefined;
beforeEach(() => {
pinia = createTestingPinia({
createSpy: vi.fn,
stubActions: false,
});
setActivePinia(pinia);
});
afterEach(() => {
vi.resetAllMocks();
});
it('unit test', async () => {
const store = useFormsContextStore(pinia);
store.calculate();
expect.soft(store.calculate).toHaveBeenCalledOnce();
expect.soft(store.helper).toHaveBeenCalledOnce();
});
});
I tthrows exception on last expect:
expected spy to be called once but got 0 times
Here is a link with reproduction: https://stackblitz.com/edit/vitest-dev-vitest-lyex7t?file=test%2FuseFormsContextStore.test.ts
Don't know where the problem is?
Upvotes: 1
Views: 666
Reputation: 1
Maybe you already resolved this problem to spy a store function but i want to leave a solution for possible people with the same problem.
Do you have to spy with spyOn function provided by vitest vitestSpyOn
look:
it('unit test', async () => {
const store = useFormsContextStore(pinia);
const calculateFunction = vi.spyOn(store, 'calculate')
expect(calculateFunction).toHaveBeenCalled();
});
Upvotes: -1