IT이야기

Jest에서 VueJ(Nuxt) 스토어를 테스트하는 방법

cyworld 2022. 6. 4. 08:12
반응형

Jest에서 VueJ(Nuxt) 스토어를 테스트하는 방법

VueJs 컴포넌트를 테스트하는 Jest 테스트가 있습니다(특히 Nuxt는 중요하지 않습니다).JSON 오브젝트 스토어를 조롱하려고 합니다.나는 아무리 생각해도 이것을 어떻게 테스트해야 할지 모르겠다.테스트 실행 시 발생하는 오류는 "Cannot read property 'easg_logo' of undefined"입니다.

My Vue 컴포넌트(풋터).비디오)

    <template>
      <div>
        <v-img 
         :height="easg_logo_height"
         :src="$store.state.app.easg_logo.src"
         :width="easg_logo_width"
        contain
         />
     <v-img 
         :height="oma_logo_height"
         :src="$store.state.app.oma_logo.src"
         :width="oma_logo_width"
        contain
         />
       </div>
    </template>
<script>
   export default {
      data(){
         easg_logo_width: this.$store.state.app.easg_logo.top.width, 
         easg_logo_height: this.$store.state.app.easg_logo.top.height,
         oma_logo_width: this.$store.state.app.oma_logo.top.width,
         oma_logo_width: this.$store.state.app.oma_logo.top.width,
      }
   }
</script>

내 테스트(footer.test.js)

import {shallowMount, createLocalVue} from '@vue/test-utils'
import Vuex from 'vuex';
import Footer from '@components/layouts/default/footer'
import Vuetify from 'vuetify';

const vuetify = new Vuetify();
const localVue = createLocalVue();
localVue.use(Vuex);

describe("Footer tests", ()=> {
  let wrapper;
  let store;
  let state;


beforeEach(() => {
   state= {
     app: {
        easg_logo:{
           src: "~/assets/images/easg.jpg",
           text: "EASG", 
           top:{
             height: 72,
             width: 82
           }
         },
    oma_logo:{
           src: "~/assets/images/oma.jpg",
           text: "OMA", 
           top:{
             height: 72,
             width: 82
           }
         }
      }
}

store = new Vuex.Store({
            modules:{
               state
            }
     })

})

test('store test', ()=> {
   wrapper = shallowMount(Footer, {store, localVue, vuetify})
   console.log(wrapper)
   const a = 'a'
    expect(a).toBe('a')
});

});

이 상태란 존재하지 않습니다.state모듈로 잘못 제공되었습니다.

다음 중 하나여야 합니다.

 store = new Vuex.Store({
   state
 })

언급URL : https://stackoverflow.com/questions/70039360/how-to-test-a-vuejs-nuxt-store-in-jest

반응형