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.

@posva/pinia runs unit tests properly, but throws jest warnings and ts errors

See original GitHub issue

Reproduction

yarn add @posva/pinia

test.spec.ts

it("calls a function from store", () => {
    // eslint-disable-next-line
    // @ts-ignore <--- by the way, the typescript error happens here: global does not exist on type...
    const wrapper = shallowMount(App, {  global: { plugins: [createTestingPinia()] } });

    // defined outside
    const store = useStore();

    wrapper.vm.$router.onReady(async () => {
      expect(store.fetchList).toHaveBeenCalled();
      // test succeeded
    });
  });

Warnings from @vue/test-utils

[Vue warn]: Injection "Symbol(pinia)" not found

TS error for global property:

Object literal may only specify known properties, and 'global' does not exist in type 'FunctionalComponentShallowMountOptions<Vue>'

Steps to reproduce the behavior

  1. Define a store using defineStore
  2. Define an action in actions
  3. Create a testing file test.spec.ts
  4. Write a test similar to above using Vue 2 environment that is described above
  5. See the warning/ts-error

Expected behavior

No TS error, no jest warnings

Actual behavior

TS throws an error on using global prop within shallowMount method from @vue/test-utils Jest throws warnings [Vue warn]: Injection "Symbol(pinia)" not found

Additional information

Env:

@pinia/testing”: “0.0.2”, “pinia”: “0.5.4”, “vue”: “2.6.14”,

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
posvacommented, Sep 1, 2021

Oh well, that’s pretty neat it just works! The type issue needs to be fixed #630, it shouldn’t be a necessary step for users. Ideally without any extra type file, mixing Vue 3 and Vue 2 types together, if that’s not an issue for TypeScript. If that’s not possible, having a separate file for Vue 2 types would be enough pinia/vue-2-types. I will still have to think about it but I’m open to proposals

1reaction
posvacommented, Aug 24, 2021

Currently, @pinia/testing only officialy supports Vue 3. For Vue 2, You need pinia@2 (it works with Vue 2 but devtools need an upstream fix) and to adapt your tests to use the vue-test-utils for Vue 2 too.

I added this to my list of things to document for the testing pinia but I haven’t even tested it yet so it might require some changes to @pinia/testing. Based on https://vue-test-utils.vuejs.org/api/options.html#other-options, you probably need

localVue.use(PiniaPlugin)
const pinia = createTestingPinia()
const wrapper = mount(Component, {localVue, pinia})

Feel free to PR if you find anything missing/broken.

Read more comments on GitHub >

github_iconTop Results From Across the Web

@posva/pinia runs unit tests properly, but throws jest warnings ...
@posva I have just added pinia v2 in Vue 2 project, and I receive TS errors in my tests like Object literal may...
Read more >
How to test a function that's expected to throw error in jest…
This caused the error I was getting. Here are the correct ways to write the unit tests: Based on the warning on the...
Read more >
How to test the type of a thrown exception in Jest
My current testing framework is AVA and I can test it as a second argument t.throws method, like here: it('should throw Error with...
Read more >
Catch warnings in Jest tests | Ben Ilegbodu
These warnings typically show up when running unit tests as well. ... Jest tests fail whenever an error is thrown (and not caught), ......
Read more >
Troubleshooting - Jest
Troubleshooting. Uh oh, something went wrong? Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​.
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