Open larrykkk opened 4 years ago
遇到...
[Vue warn]: Unknown custom element:
ans: https://github.com/ElemeFE/element/issues/11811 解決方法是把缺少的組件都引入到 test 中,才不會報錯
import { mount, shallowMount, createLocalVue } from "@vue/test-utils";
import ElementUI from "element-ui";
const localVue = createLocalVue();
localVue.use(ElementUI);
//some code
test("is a Vue instance", () => {
const wrapper = shallowMount(create, {
localVue,
store
});
expect(wrapper.isVueInstance()).toBeTruthy();
});
//some code
[Vue warn]: Unknown custom element:
TypeError: Cannot read property 'commit' of undefined ?
找不到 vuex 的東西 https://github.com/vuejs/vue-test-utils/issues/116#issuecomment-374960369 解決方法: mock 出一個最小可行的 vuex store 讓 test 可以運行
使用 Jest 進行 Front-End Unit Test(別人的分享簡報) https://speakerdeck.com/patw0929/shi-yong-jest-jin-xing-front-end-unit-test?slide=56
jest API 使用紀錄
一個 describe 下可已有多個 it(test) 斷言 例如:
describe("test login form", () => {
it("some feature description", (done) => {
// code
});
it("some feature description ", (done) => {
// code
});
it("some feature description ", (done) => {
// code
});
}
優質文章:
測試 watch 內的 method 最後有沒有觸發
import { mount, shallowMount, createLocalVue } from "@vue/test-utils";
import create from "@/pages/issue/create.vue";
import ElementUI from "element-ui";
import Vuex from "vuex";
import { library, config } from "@fortawesome/fontawesome-svg-core";
import { fas } from "@fortawesome/free-solid-svg-icons";
import { far } from "@fortawesome/free-regular-svg-icons";
import { fab } from "@fortawesome/free-brands-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
config.autoAddCss = false;
library.add(fas, far, fab);
const localVue = createLocalVue();
localVue.use(ElementUI);
localVue.use(Vuex);
localVue.component("font-awesome-icon", FontAwesomeIcon);
const factory = (localVue, store) => {
return shallowMount(create, {
localVue,
store
});
};
describe("create.vue", () => {
// add the 2 lines below
let store;
let wrapper;
let spy;
beforeEach(() => {
store = new Vuex.Store({
state: {
isEditing: false
},
mutations: {
changeEditStatus(state, payload) {
state.isEditing = payload;
}
}
// actions: {}
});
wrapper = factory(localVue, store);
});
it("is watch have been trigger", async () => {
const spy = jest.spyOn(wrapper.vm, "addColorAndRemoveColor");
wrapper.vm.formData.standpoint.pop();
await wrapper.vm.$nextTick();
expect(spy).toBeCalled();
});
});
紀錄 jest 學習遇到的障礙與解決方案