]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(devtools): Do not patch mocked actions (#2300)
authorBodo Graumann <mail@bodograumann.de>
Wed, 17 Apr 2024 08:05:59 +0000 (10:05 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2024 08:05:59 +0000 (10:05 +0200)
packages/pinia/__tests__/devtools.spec.ts [new file with mode: 0644]
packages/pinia/src/devtools/plugin.ts

diff --git a/packages/pinia/__tests__/devtools.spec.ts b/packages/pinia/__tests__/devtools.spec.ts
new file mode 100644 (file)
index 0000000..5d45692
--- /dev/null
@@ -0,0 +1,36 @@
+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()
+  })
+})
index 82d36bd8c23415caa60ba07b172dc99a9271ceba..e122a04d1b7f49e4fbd905ea44959cf1b2e9ab07 100644 (file)
@@ -566,21 +566,24 @@ export function devtoolsPlugin<
   // 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(