Mock return value of Store function/action #1292
-
Hi, I red the Pinia docs, as well as the examples in the pinia/testing repo and I still have no idea how to return a mocked value from a Store function. Why? - I have some Component tests which rely on some functions from the store to return certain values, I'd like to specify those return values and feed them to my Component test. export const useMyStore = defineStore("myStore", () => {
function mockMe() {}
}); The line below should illustrate what I'm trying to achieve myStore.mockMe().mockImplementation(() => "myMockedReturnValue"); I also tried using the Help appreciated! Cheers, Patrik |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hello @PatrikBird and folks, I've got the same problem here. Any help would be really appreciated! Thanks, |
Beta Was this translation helpful? Give feedback.
-
You need to return the action and call mockImplementation on the function itself: |
Beta Was this translation helpful? Give feedback.
-
Just to give an example of how I implemented this with my factory function in vitest, as it wasn't really clear to me: function createMyComponentWrapper(options = {}) {
const pinia = createTestingPinia();
const counterStore = useCounterStore(pinia);
// Use vi.mocked for typescript support
vi.mocked(counterStore.incrementCounter).mockReturnValue(3);
return mount(MyComponent, {
global: {
plugins: [pinia],
},
...options,
});
} |
Beta Was this translation helpful? Give feedback.
You need to return the action and call mockImplementation on the function itself:
store.mockMe.mock implementation()