完善中…

    如何对electron进行测试呢?可以参考electron-test
    只对单元测试部分进行分析

    1. import {mount} from '@vue/test-utils';
    2. import {expect, test, vi} from 'vitest';
    3. import ElectronVersions from '../src/components/ElectronVersions.vue';
    4. // 模拟模块,api,进行测试
    5. vi.mock('#preload', () => {
    6. return {
    7. versions: {lib1: 1, lib2: 2},
    8. };
    9. });
    10. test('ElectronVersions component', async () => {
    11. expect(ElectronVersions).toBeTruthy();
    12. // 挂载组件
    13. // 挂载组件还有其他用法结合起来 可以测试vuex和pinia
    14. const wrapper = mount(ElectronVersions);
    15. // 找到元素
    16. const lis = wrapper.findAll<HTMLElement>('li');
    17. // 进行断言
    18. expect(lis.length).toBe(2);
    19. expect(lis[0].text()).toBe('lib1: v1');
    20. expect(lis[1].text()).toBe('lib2: v2');
    21. });
    1. import { mount } from '@vue/test-utils';
    2. import { expect, test, vi } from 'vitest';
    3. // 这里一定要导入,会智能识别
    4. import { readFile } from '#preload';
    5. import ReactiveCounter from '../src/components/ReactiveCounter.vue';
    6. // 模拟模块
    7. vi.mock('#preload', () => {
    8. return {
    9. // spy函数
    10. readFile: vi.fn(() => void 0),
    11. };
    12. });
    13. test('ReactiveHash component', async () => {
    14. expect(ReactiveCounter).toBeTruthy();
    15. // 挂载组件
    16. const wrapper = mount(ReactiveCounter);
    17. // 获取元素
    18. const add = wrapper.get('.add');
    19. // 进行断言
    20. expect(add.text()).toBe('count is: 0');
    21. // 触发事件
    22. await add.trigger('click');
    23. // 进行断言
    24. expect(add.text()).toBe('count is: 1');
    25. // 获取元素
    26. const read = wrapper.get('.read');
    27. await read.trigger('click');
    28. expect(readFile).toBeCalledTimes(1);
    29. await read.trigger('click');
    30. expect(readFile).toBeCalledTimes(2);
    31. });
    1. import {mount} from '@vue/test-utils';
    2. import {expect, test, vi} from 'vitest';
    3. import ReactiveHash from '../src/components/ReactiveHash.vue';
    4. vi.mock('#preload', () => {
    5. return {
    6. sha256sum: vi.fn((s: string) => `${s}:HASHED`),
    7. };
    8. });
    9. test('ReactiveHash component', async () => {
    10. expect(ReactiveHash).toBeTruthy();
    11. const wrapper = mount(ReactiveHash);
    12. const dataInput = wrapper.get<HTMLInputElement>('input:not([readonly])');
    13. const hashInput = wrapper.get<HTMLInputElement>('input[readonly]');
    14. const dataToHashed = Math.random().toString(36).slice(2, 7);
    15. await dataInput.setValue(dataToHashed);
    16. expect(hashInput.element.value).toBe(`${dataToHashed}:HASHED`);
    17. });