-import { createTestingPinia, defineStore, TestingOptions } from '../src'
+import {
+ createPinia,
+ createTestingPinia,
+ defineStore,
+ TestingOptions,
+} from '../src'
import { mount } from '@vue/test-utils'
import { defineComponent } from 'vue'
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)
+ })
})
: // 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?
* @internal
*/
_a: App
+
+ /**
+ * Added by `createTestingPinia()` to bypass `useStore(pinia)`.
+ *
+ * @internal
+ */
+ _testing?: boolean
}
declare module '@vue/runtime-core' {
storesMap,
piniaSymbol,
Pinia,
+ activePinia,
} from './rootStore'
import { IS_CLIENT } from './env'
// 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()
app.use(pinia)
}
+ pinia._testing = true
+
setActivePinia(pinia)
return Object.assign(
spiedActions.clear()
},
get app() {
- return this._a as App
+ return (this as TestingPinia)._a
},
},
pinia