From 5a52fb33a1799e145a8d3e3423105247bb6980e7 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 28 Jun 2021 15:24:07 +0200 Subject: [PATCH] feat(testing): bypass useStore(pinia) --- __tests__/testing.spec.ts | 20 +++++++++++++++++++- rollup.config.js | 5 ++++- src/rootStore.ts | 7 +++++++ src/store.ts | 7 ++++++- src/testing.ts | 4 +++- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/__tests__/testing.spec.ts b/__tests__/testing.spec.ts index 56410451..0f97b3df 100644 --- a/__tests__/testing.spec.ts +++ b/__tests__/testing.spec.ts @@ -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) + }) }) diff --git a/rollup.config.js b/rollup.config.js index 96c025ed..c4ccd2d9 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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? diff --git a/src/rootStore.ts b/src/rootStore.ts index 37dc3dd4..64eb0396 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -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' { diff --git a/src/store.ts b/src/store.ts index 55017eb5..82218b16 100644 --- a/src/store.ts +++ b/src/store.ts @@ -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() diff --git a/src/testing.ts b/src/testing.ts index a09a44f1..e309757c 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -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 -- 2.47.2