简体   繁体   English

Jest Vuejs 测试 - 类型错误:无法读取未定义的属性(读取“参数”)

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

I started first time to test my vuejs application.我第一次开始测试我的 vuejs 应用程序。 In my application I have a list of clients, and when you click on client, you go to that clients page, which I am try to test right now.在我的应用程序中,我有一个客户端列表,当您单击客户端时,您将 go 转到该客户端页面,我现在正在尝试对其进行测试。 But my test fails before I started with this error:但是在我开始出现这个错误之前我的测试失败了:

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

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

I tried to set this into my test:我试图将其设置为我的测试:

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

Can you help me understand how to give this params a value through the test?你能帮我理解如何通过测试给这个参数赋值吗?

There has been many different answers on the.net, but the for the vue1. .net 上有很多不同的答案,但是对于 vue1. This worked for me:这对我有用:

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.

相关问题 TypeError:无法读取未定义(读取“长度”)JEST 测试的属性 - TypeError: Cannot read properties of undefined (reading 'length') JEST test 笑话:- TypeError:无法读取未定义的属性(读取“params”)。 开玩笑时出错 - Jest:- TypeError: Cannot read properties of undefined (reading 'params'). Error coming in jest 类型错误:无法在 vuejs 中读取未定义的属性(读取“添加”) - TypeError: Cannot read properties of undefined (reading 'add') in vuejs TypeError:无法读取未定义的属性(读取“集合”)-Vuejs 和 Firebase - TypeError: Cannot read properties of undefined (reading 'collection') - Vuejs and Firebase TypeError:无法使用 VueJS 读取未定义的属性(读取 '$refs') - TypeError: Cannot read properties of undefined (reading '$refs') with VueJS vuejs 2 组合 api: TypeError: Cannot read properties of undefined (reading 'length') - vuejs 2 composition api: TypeError: Cannot read properties of undefined (reading 'length') VueJS TypeError:无法读取未定义的属性(读取'toLowerCase') - VueJS TypeError: Cannot read properties of undefined (reading 'toLowerCase') 类型错误:无法读取未定义的属性(读取“$router”)vuejs - TypeError: Cannot read properties of undefined (reading '$router') vuejs TypeError:无法在 React js 中读取未定义的属性(读取“参数”) - TypeError: Cannot read properties of undefined (reading 'params') in React js 未捕获的 TypeError:无法读取未定义的属性(读取“params”)-React - Uncaught TypeError: Cannot read properties of undefined (reading 'params') - React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM