【发布时间】:2020-12-27 13:52:30
【问题描述】:
我在 nuxt js 中使用 jest 进行单元测试 我已经安装了这样的钩子
async mounted(){
try{
var response = await this.$axios.get("api_url here");
this.result = response.data;
} catch(e){
console.log("Exception: ",e)
}
}
当我对其进行单元测试时,我的代码是 . utnit.spec.js
jest.mock("axios", () => ({
get: () => Promise.resolve({ data: [{ val: 1 }] })
}));
import { mount } from '@vue/test-utils';
import file from '../filefile';
import axios from "axios";
describe('file', () => {
test('check comp. working correctly', () => {
var wrapper = mount(file);
afterEach(() => {
wrapper.destroy()
})
})
})
我在那里收到此警告,结果中没有数据
Exception: TypeError: Cannot read property 'get' of undefined
at VueComponent.mounted
我怎么知道这里有什么问题,是不是我无法访问单元文件中的axios 有没有什么具体方法可以在mounted hook中测试axios
【问题讨论】:
-
你必须模拟 axios
-
我有这样的模拟 axios jest.mock("axios", () => ({ get: () => Promise.resolve({ data: [{ val: 1 }] }) }));但同样的问题
标签: unit-testing vue.js jestjs nuxt.js vue-test-utils