question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow bypassing getters in `@pinia/testing`

See original GitHub issue

There should be a way to bypass any getter when testing components that use a store.

Some Ideas:

  • use the createTestingPinia() to add a plugin that allows modifying any getter:

    // in a test
    const store = useStore()
    store.myGetter // getter computed value
    store.myGetter = newvalue
    store.myGetter // newvalue
    
  • Maybe a different way to pass options specific to stores:

    createTestingPinia({ overrides: [
      [useSomeStore, { getters: { /* autocompletion */ stuff: ... }}]
    ] })
    

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
JJBocanegracommented, Jul 16, 2022

This isn’t working properly with Typescript.

// store.ts
import { defineStore } from 'pinia';

export const useStore = defineStore('store', {
  getters: {
    foo: () => true,
  },
});
// test.ts
import { createTestingPinia } from '@pinia/testing';
import { useStore } from 'store.ts';

describe('Test', () => {
  const pinia = createTestingPinia();
  const store = useStore(pinia);

  // Error TS2540: Cannot assign to ' foo' because it is a read-only property
  store.foo = false;
});

To fix this I needed to add the next line just above each getter modification

 // @ts-expect-error: Getter is read only

Versions

1reaction
bodograumanncommented, Jul 27, 2022

I am doing the following now (latest version 2.0.17):

type Mocked<T> = { [P in keyof T]: ReturnType<typeof jest.fn> };
function mocked<Id extends string, S, G, A>(store: Store<Id, S, G, A>): S & UnwrapRef<G> & Mocked<A> {
  /* eslint-disable-next-line @typescript-eslint/ban-ts-comment -- the new type is due to `app.use(createTestingPinia())` */
  //@ts-ignore
  return store;
}

describe('Test', () => {
  const pinia = createTestingPinia();
  const store = mocked(useStore(pinia));

  store.foo = false;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing stores - Pinia
Stores will, by design, be used at many places and can make testing much harder than ... Specifying the createSpy function; Mocking getters;...
Read more >
Testing Pinia Data Stores in Vue - TestDriven.io
This tutorial describes how to test Pinia data stores in Vue applications.
Read more >
Quick Full Pinia Course. Vue VueX Pinia Store State Action…
Next, let's try mutating several states at the same time. We go to the store file and add a new action method. We...
Read more >
State Management on Vue with Pinia - YouTube
Pinia is an intuitive Vue Store, lightweight, type-safe, with an incredible devtools integration and of course, a very tropical name.
Read more >
Pinia Crash Course #5 - Getters - YouTube
In this Pinia tutorial, you'll learn all about Getters and why we'd use them. Watch the whole course now (without ads) on Net...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found