direct-vuex icon indicating copy to clipboard operation
direct-vuex copied to clipboard

Error TS2615 when accessing store.original in a unit test

Open a-kriya opened this issue 3 years ago • 6 comments

I'll share my header.spec.ts file that tries to mock a getter and encounters ts(2615) on store.original:

import {createLocalVue, mount} from '@vue/test-utils'
import Vuex from 'vuex'
import {createDirectStore} from 'direct-vuex'
import Header from '@/components/Header.vue'

const localVue = createLocalVue()
localVue.use(Vuex)

const {store} = createDirectStore({
  getters: {
    isAuthenticated: () => true,
  },
})

describe('Header.vue', () => {
  it('renders logo', () => {
    const wrapper = mount(Header, {store: store.original, localVue})
  })
})
 FAIL  tests/unit/header.spec.ts
  ● Test suite failed to run

    TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    tests/unit/header.spec.ts:17:43 - error TS2615: Type of property 'original' circularly references itself in mapped type '{ [K in keyof { readonly state: {}; getters: { readonly isAuthenticated: boolean; }; commit: {}; dispatch: {}; original: VuexStore<{ getters: { isAuthenticated: () => boolean; }; }>; }]: ShowContentDepth1<{ readonly state: {}; getters: { ...; }; commit: {}; dispatch: {}; original: VuexStore<...>; }[K]>; }'.

    17     const wrapper = mount(Header, {store: store.original, localVue})
                                                 ~~~~~~~~~~~~~~

The Header component itself is fairly straightforward: main store is imported and an isAuthenticated computed property is defined that returns the value from the imported store.

Is the above issue related to this package, or caused due to other packages/configuration in my project? Any help is appreciated.

a-kriya avatar Apr 04 '21 20:04 a-kriya

Related: https://github.com/microsoft/TypeScript/issues/38279#issuecomment-631328971

a-kriya avatar Apr 12 '21 17:04 a-kriya

I have same error. "direct-vuex": "0.10.4"

viT-1 avatar Apr 29 '21 15:04 viT-1

@viT-1 I had to downgrade to TS 3.8.3. Upgrading to Vue3 may also solve the issue.

@paleo Unlikely that the issue can be resolved in this package, but I thought filing this ticket may help others.

a-kriya avatar Apr 30 '21 05:04 a-kriya

Hi. I suggest you edit node_modules/direct-vuex/types/direct-types.d.ts. Line 24, change the type of original to any. If it works, I can consider to publish the package with this workaround. I think the type of original is not really important in our case.

export type ToDirectStore<O extends StoreOptions> = ShowContent<{
  readonly state: ShowContent<DirectState<O>>
  getters: ShowContent<DirectGetters<O>>
  commit: ShowContent<DirectMutations<O>>
  dispatch: ShowContent<DirectActions<O>>
  original: any // <-- here, replace 'VuexStore<O>' by 'any'
}>

paleo avatar Apr 30 '21 08:04 paleo

@paleo Thanks for the suggestion. That fixes the error.

a-kriya avatar May 10 '21 23:05 a-kriya

@paleo Do you think it's something you'll want to include and publish?

a-kriya avatar May 18 '21 17:05 a-kriya