简体   繁体   中英

Jest Nuxt Test TypeError: Cannot read property '$get' of undefined

I got some error when testing my nuxt app with JEST,

the error found like this:

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

this is my test Unit Test spec.js code:

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);
    });
});

when I trace the code, it come from this line:

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

any suggestion or solution for this?

use $axios.$get=jest.fn().mockResolvedValue(mockFetchPromise); instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM