pinia icon indicating copy to clipboard operation
pinia copied to clipboard

feat(test): support unstubbing actions

Open CyberAP opened this issue 7 months ago • 1 comments
trafficstars

This change makes it possible to unstub actions when using stubActions: true in createTestingPinia. To do that you have to call mockRestore on the action.

createTestingPinia()

useMyStore().someAction.mockRestore()

// this calls an actual implementation
useMyStore().someAction()

The support for unstubbing is provided only for jest and vitest.

CyberAP avatar Apr 01 '25 08:04 CyberAP

Deploy Preview for pinia-official ready!

Name Link
Latest commit e4a37d0501962e9256bec71ea4e2a2c248af193f
Latest deploy log https://app.netlify.com/sites/pinia-official/deploys/67eba452886531000841b72b
Deploy Preview https://deploy-preview-2960--pinia-official.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Apr 01 '25 08:04 netlify[bot]

This feature is useful, may I know when it gonna release? @posva

lostncg avatar Aug 12 '25 03:08 lostncg

Thanks a lot for this! I'm reworking

  • https://github.com/vuejs/pinia/pull/3040
  • to have https://github.com/vuejs/pinia/issues/2970 implemented

The approach you want should already be doable with type support using the helper mentioned in docs. It depends on your testing framework

posva avatar Nov 03 '25 15:11 posva

@posva this is somewhat different. We could stub specific actions before that:

createTestingPinia({ stubActions: false })
actionsToStub.forEach(action => jest.spyOn(useMyStore(), action).mockReturnValue())

What we can't do is stub everything but unstub specific actions.

CyberAP avatar Nov 03 '25 16:11 CyberAP

You can unstub specific actions, I just tested it. But now you can also pass a function to stubActions and return a boolean to decide wheather to stub or not. It should fit any advanced cases

posva avatar Nov 03 '25 16:11 posva