簡體   English   中英

Jest Vuejs 測試 - 類型錯誤:無法讀取未定義的屬性(讀取“參數”)

[英]Jest Vuejs Test - TypeError: Cannot read properties of undefined (reading 'params')

我第一次開始測試我的 vuejs 應用程序。 在我的應用程序中,我有一個客戶端列表,當您單擊客戶端時,您將 go 轉到該客戶端頁面,我現在正在嘗試對其進行測試。 但是在我開始出現這個錯誤之前我的測試失敗了:

TypeError: Cannot read properties of undefined (reading 'params')

created() {
   this.clientid = this.$route.params.id;
                                   ^
   this.getClient();
}

我試圖將其設置為我的測試:

describe('Client', () => {
    it('should mount Client', () => {
      const wrapper = mount(Client, {
        data: () => ({
          route: {
            params: {
              id: ''
            }
          },
        }),
      });
      expect(wrapper.exists()).toBe(true);
    });
  });

你能幫我理解如何通過測試給這個參數賦值嗎?

.net 上有很多不同的答案,但是對於 vue1. 這對我有用:

describe('Client', () => {
  it('should mount Client', async () => {
    const wrapper = shallowMount(Client, {
      global: {
        mocks: {
          $route: {params: { id: ""}},
        }
      }
    })
    expect(wrapper.exists()).toBe(true);
  });
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM