-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
前端单元测试二三事 #27
Comments
Jest在React项目Jest是FB家的开源的单元测试的工具,对React项目支持非常友好。 jest支持localStorage如果单元测试用例的组件中有localStorage的使用时,会有
然后在
jest中的XX_ENV配置在 globals: {
REACT_APP_ENV: 'dev'
}, jest支持dva/redux等这一类的配置有点类似,在业务组件的测试文件如Tag.test.js,注意亮点一个是标签为Tag.WrappedComponent,属性传递的即为Tag组件需要的store中的对象
jest测试组件内部方法
jest快照snapshot是使用render方法,并生成一个文件夹,每次运行进行前后的“快照”对比,这里的“快照”要单独说明一下,不是照片,在不toJSON情况下指的是生成的类似于AST(抽象语法树)的json结构,
jest,enzyme支持jsx中import的css变量与click事件enzyme默认是支持jsx中css的普通写法的,但是如果组件内部是使用的import的形式,比如: <div className={styles.add_intersect} onClick={this.addNewGroup}>
新增组
</div> 上面这种情况下,我们使用enzyme的find等选择器是获取不到dom元素的,这里我们需要借助
moduleNameMapper: {
'\\.(css|less|sass|scss)$': 'identity-obj-proxy',
},
参考: jest解决
|
jest在Vue项目防止element-ui报错找不到:"Unknown custom element: - did you register the component correctly"
import ElementUI from 'element-ui';
import { shallowMount, createLocalVue } from "@vue/test-utils";
import Rules from "@/views/rules.vue";
import Cases2WashSaveData from "./cases/case-washSaveData.js";
const localVue = createLocalVue()
localVue.use(ElementUI)
test("washSaveData", () => {
Cases2WashSaveData.forEach((caseData) => {
const wrapper = shallowMount(Rules,{localVue}, {});
expect(wrapper.vm.washSaveData(caseData.test)).toStrictEqual(caseData.expect);
});
});
module.exports = {
preset: "@vue/cli-plugin-unit-jest",
moduleNameMapper: {
'\\.(css|less|sass|scss)$': 'identity-obj-proxy',
},
}; router-link “Unknown custom element: - did you register the component correctly”
const wrapper = shallowMount(Rules,{localVue, stubs: ['router-link']}, {}); 参考: https://stackoverflow.com/questions/49681546/vue-test-utils-unknown-custom-element-router-link 支持element-ui组件的事件测试
支持vueximport { shallowMount, createLocalVue, mount } from "@vue/test-utils";
import Vuex from 'vuex';
const localVue = createLocalVue();
localVue.use(Vuex)
let actions
let store
beforeEach(() => {
actions = {
setMenuExpand: jest.fn()
}
store = new Vuex.Store({
state: {
menuExpand: false,
},
actions
})
})
/**addNewGroup的click事件 */
test("addNewGroup", () => {
const wrapper = mount(Rules, { localVue,store, stubs: ["router-link"] });
}); 相对较全的demoimport ElementUI from "element-ui";
import { shallowMount, createLocalVue, mount } from "@vue/test-utils";
import Rules from "@/views/rules.vue";
import Vuex from "vuex";
const localVue = createLocalVue();
localVue.use(ElementUI);
localVue.use(Vuex);
let actions;
let store;
beforeEach(() => {
actions = {
setMenuExpand: jest.fn(),
};
store = new Vuex.Store({
state: {
menuExpand: false,
},
actions,
});
});
/**addNewGroup的click事件 */
test("addNewGroup", () => {
const wrapper = mount(Rules, { localVue, store, stubs: ["router-link"] });
//初始化state数据
wrapper.setData({
ruleObj: { include: [[]], exclude: [[]] },
});
//寻找Dom
const button = wrapper.find(".add-group");
//Dom文本
expect(button.text()).toBe("Btn");
//事件触发
button.trigger("click");
//state数据
expect(wrapper.vm.ruleObj).toStrictEqual({
include: [[], []],
exclude: [[]],
});
}); |
THREE等webgl工程单元测试 |
好处
单元测试的好处就不用多说了,对于敏捷开发的迭代需求或者业务逻辑的重构,有了单元测试之后非常方便的担保业务逻辑平滑过渡,而且单元测试的case的存在,可以有效的说明逻辑,比代码注释更为清晰。在MVVM框架流行的今天,数据驱动DOM使得单元测试更加重要而且可行性更高。
痛点
前端单元测试推动是一直有痛点的,包括一些大厂对前端单元测试这个态度不是很统一。原因主要概括为两点:
从而导致业务线前期稳定性靠开发者把控,后面项目逐渐庞大,测试用例又没有时间补贴,或者开发者转了战场。
折中
比较好的处理方式是折中方案,Utils工具类中的方法必须进行单元测试,业务基础组件和项目的基础业务逻辑必须进行单元测试,这样可以很好的避免后期基础的逻辑,手动痛苦地回归case。而目前看来,前端很多项目也确实是这么做的。其他的case则视重要性与时间代价视情况而定了。
The text was updated successfully, but these errors were encountered: