--- /dev/null
+import { describe, it, expect } from 'vitest'
+import { mount } from '@vue/test-utils'
+import { createPinia, defineStore } from '../src'
+import { devtoolsPlugin } from '../src/devtools'
+
+describe('devtoolsPlugin', () => {
+ const useStore = defineStore('test', {
+ actions: {
+ myAction() {
+ return 42
+ },
+ },
+ })
+
+ it('preserves mocked actions during testing', () => {
+ const pinia = createPinia()
+ // Simulate using createTestingPinia
+ pinia._testing = true
+
+ mount({ template: 'none' }, { global: { plugins: [pinia] } })
+
+ // Simulate mocking with @pinia/testing createSpy
+ pinia.use(({ store, options }) => {
+ Object.keys(options.actions).forEach((action) => {
+ store[action]._mockImplementation = () => {}
+ })
+ })
+ // Previously the mocked actions would be wrapped again
+ pinia.use(devtoolsPlugin)
+
+ const store = useStore(pinia)
+
+ // @ts-expect-error we have not actually loaded @pinia/testing and mocked actions
+ expect(store.myAction._mockImplementation).toBeDefined()
+ })
+})
// detect option api vs setup api
store._isOptionsAPI = !!options.state
- patchActionForGrouping(
- store as StoreGeneric,
- Object.keys(options.actions),
- store._isOptionsAPI
- )
-
- // Upgrade the HMR to also update the new actions
- const originalHotUpdate = store._hotUpdate
- toRaw(store)._hotUpdate = function (newStore) {
- originalHotUpdate.apply(this, arguments as any)
+ // Do not overwrite actions mocked by @pinia/testing (#2298)
+ if (!store._p._testing) {
patchActionForGrouping(
store as StoreGeneric,
- Object.keys(newStore._hmrPayload.actions),
- !!store._isOptionsAPI
+ Object.keys(options.actions),
+ store._isOptionsAPI
)
+
+ // Upgrade the HMR to also update the new actions
+ const originalHotUpdate = store._hotUpdate
+ toRaw(store)._hotUpdate = function (newStore) {
+ originalHotUpdate.apply(this, arguments as any)
+ patchActionForGrouping(
+ store as StoreGeneric,
+ Object.keys(newStore._hmrPayload.actions),
+ !!store._isOptionsAPI
+ )
+ }
}
addStoreToDevtools(