完善中…
如何对electron进行测试呢?可以参考electron-test
只对单元测试部分进行分析
import {mount} from '@vue/test-utils';
import {expect, test, vi} from 'vitest';
import ElectronVersions from '../src/components/ElectronVersions.vue';
// 模拟模块,api,进行测试
vi.mock('#preload', () => {
return {
versions: {lib1: 1, lib2: 2},
};
});
test('ElectronVersions component', async () => {
expect(ElectronVersions).toBeTruthy();
// 挂载组件
// 挂载组件还有其他用法结合起来 可以测试vuex和pinia
const wrapper = mount(ElectronVersions);
// 找到元素
const lis = wrapper.findAll<HTMLElement>('li');
// 进行断言
expect(lis.length).toBe(2);
expect(lis[0].text()).toBe('lib1: v1');
expect(lis[1].text()).toBe('lib2: v2');
});
import { mount } from '@vue/test-utils';
import { expect, test, vi } from 'vitest';
// 这里一定要导入,会智能识别
import { readFile } from '#preload';
import ReactiveCounter from '../src/components/ReactiveCounter.vue';
// 模拟模块
vi.mock('#preload', () => {
return {
// spy函数
readFile: vi.fn(() => void 0),
};
});
test('ReactiveHash component', async () => {
expect(ReactiveCounter).toBeTruthy();
// 挂载组件
const wrapper = mount(ReactiveCounter);
// 获取元素
const add = wrapper.get('.add');
// 进行断言
expect(add.text()).toBe('count is: 0');
// 触发事件
await add.trigger('click');
// 进行断言
expect(add.text()).toBe('count is: 1');
// 获取元素
const read = wrapper.get('.read');
await read.trigger('click');
expect(readFile).toBeCalledTimes(1);
await read.trigger('click');
expect(readFile).toBeCalledTimes(2);
});
import {mount} from '@vue/test-utils';
import {expect, test, vi} from 'vitest';
import ReactiveHash from '../src/components/ReactiveHash.vue';
vi.mock('#preload', () => {
return {
sha256sum: vi.fn((s: string) => `${s}:HASHED`),
};
});
test('ReactiveHash component', async () => {
expect(ReactiveHash).toBeTruthy();
const wrapper = mount(ReactiveHash);
const dataInput = wrapper.get<HTMLInputElement>('input:not([readonly])');
const hashInput = wrapper.get<HTMLInputElement>('input[readonly]');
const dataToHashed = Math.random().toString(36).slice(2, 7);
await dataInput.setValue(dataToHashed);
expect(hashInput.element.value).toBe(`${dataToHashed}:HASHED`);
});