]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(testing): bypass useStore(pinia)
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Jun 2021 13:24:07 +0000 (15:24 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Jun 2021 13:24:07 +0000 (15:24 +0200)
__tests__/testing.spec.ts
rollup.config.js
src/rootStore.ts
src/store.ts
src/testing.ts

index 56410451669d35c872a0fc00db92b201b2d4af4a..0f97b3dfc796b86bab0b37b408b17abf7a8e118e 100644 (file)
@@ -1,4 +1,9 @@
-import { createTestingPinia, defineStore, TestingOptions } from '../src'
+import {
+  createPinia,
+  createTestingPinia,
+  defineStore,
+  TestingOptions,
+} from '../src'
 import { mount } from '@vue/test-utils'
 import { defineComponent } from 'vue'
 
@@ -138,4 +143,17 @@ describe('Testing', () => {
     expect(counter.pluginN).toBe(0)
     expect(pinia.app).toHaveProperty('mount', expect.any(Function))
   })
+
+  it('bypass useStore(pinia)', () => {
+    const realPinia = createPinia()
+    const { counter } = factory()
+
+    const counterWithRealPinia = useCounter(realPinia)
+
+    expect(counter.n).toBe(0)
+    expect(counterWithRealPinia.n).toBe(0)
+    counter.n++
+    expect(counter.n).toBe(1)
+    expect(counterWithRealPinia.n).toBe(1)
+  })
 })
index 96c025edee63c968a30656f62d73148bb9ba0e7b..c4ccd2d964c496740ba6990669ffa7dc1fc74453 100644 (file)
@@ -158,7 +158,10 @@ function createReplacePlugin(
         : // hard coded dev/prod builds
           JSON.stringify(!isProduction),
     // this is only used during tests
-    __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : 'false',
+    __TEST__:
+      isBundlerESMBuild || isNodeBuild
+        ? `(process.env.NODE_ENV === 'test')`
+        : 'false',
     // If the build is expected to run directly in the browser (global / esm builds)
     __BROWSER__: JSON.stringify(isBrowserBuild),
     // is targeting bundlers?
index 37dc3dd471cdb6b68ac7a3322a3ae8cc1829de2c..64eb0396cb60222eda30428b62f0afe9c4ee26e5 100644 (file)
@@ -91,6 +91,13 @@ export interface Pinia {
    * @internal
    */
   _a: App
+
+  /**
+   * Added by `createTestingPinia()` to bypass `useStore(pinia)`.
+   *
+   * @internal
+   */
+  _testing?: boolean
 }
 
 declare module '@vue/runtime-core' {
index 55017eb508999df03f393a520383e5d5c96daeb0..82218b1664020fabc543f43a3c8d6ade3b7700d1 100644 (file)
@@ -41,6 +41,7 @@ import {
   storesMap,
   piniaSymbol,
   Pinia,
+  activePinia,
 } from './rootStore'
 import { IS_CLIENT } from './env'
 
@@ -409,7 +410,11 @@ export function defineStore<
     // only run provide when pinia hasn't been manually passed
     const shouldProvide = currentInstance && !pinia
     // avoid injecting if `useStore` when not possible
-    pinia = pinia || (currentInstance && inject(piniaSymbol))
+    pinia =
+      // in test mode, ignore the argument provided as we can always retrieve a
+      // pinia instance with getActivePinia()
+      (__TEST__ && activePinia && activePinia._testing ? null : pinia) ||
+      (currentInstance && inject(piniaSymbol))
     if (pinia) setActivePinia(pinia)
     // TODO: worth warning on server if no piniaKey as it can leak data
     pinia = getActivePinia()
index a09a44f140148f8451d6935d54f9038c47ee70cb..e309757c3fab7e7eca66b57d7a9feedb7843fb4c 100644 (file)
@@ -103,6 +103,8 @@ export function createTestingPinia({
     app.use(pinia)
   }
 
+  pinia._testing = true
+
   setActivePinia(pinia)
 
   return Object.assign(
@@ -111,7 +113,7 @@ export function createTestingPinia({
         spiedActions.clear()
       },
       get app() {
-        return this._a as App
+        return (this as TestingPinia)._a
       },
     },
     pinia