Jest Nuxt 测试类型错误:无法读取未定义的属性“$get”

时间:2021-01-17 05:41:46

标签: javascript node.js unit-testing jestjs

我在用 JEST 测试我的 nuxt 应用程序时遇到了一些错误,

发现的错误是这样的:

[Vue warn]: Error in mounted hook: "TypeError: Cannot read property '$get' of undefined"    
found in
---> <Anonymous>
       <Root>

这是我的测试单元测试 spec.js 代码:

describe("DataLoader.vue", () => {
    test("renders properly", () => {
        const wrapper = factory();
        expect(wrapper.html()).toMatchSnapshot();
    });

    test("text axios", () => {
        const commit = jest.fn();
        const messages = {};
        const mockFetchPromise = Promise.resolve({
            data: messages
        });
        axios.get = jest.fn().mockResolvedValue(mockFetchPromise);
    });
});

当我跟踪代码时,它来自这一行:

await this.$axios.$get(this.endpoint, options).then(({ code, meta, content }) => {
      this.data = { content: content, meta: meta };
      this.isLoading = false;
});

对此有何建议或解决方案?

1 个答案:

答案 0 :(得分:0)

改用 $axios.$get=jest.fn().mockResolvedValue(mockFetchPromise);