ant-design / antd-style

css-in-js library with antd v5 token system
https://ant-design.github.io/antd-style/
MIT License
189 stars 29 forks source link

:wrench: chore: refactor jest to vitest #43

Closed arvinxx closed 1 year ago

arvinxx commented 1 year ago

配置文件

vitest.config.ts

import path from 'path';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  esbuild: {
    jsxInject: "import React from 'react'",
  },
  test: {
    setupFiles: path.join(__dirname, './tests/test-setup.ts'),
    environment: 'jsdom',
    globals: true,
    testTimeout: 20000,
    coverage: {
      reporter: ['text', 'json', 'lcov'],
    },
    alias: {
        // 配置 alias
    },
  },
});

测试调整部分

由于配置了 global ,95% 的测试文件不用改。要改的只涉及到 jest mock 的。

函数 mock

全局替换 jest.fnvi.fn

整个模块 mock

__mocks__ 提到目录最顶层,例如 tests/__mocks__/zustand.ts -> __mocks__/zustand.ts。然后在测试方法里添加:

import { vi } from 'vitest';

vi.mock('zustand');

模块的个别方法 mock

import { mocked } from 'jest-mock';

jest.mock('mockjs');
mocked(Random.word).mockImplementation(() => 'abcd');

需要替换为:

import { vi } from 'vitest';

vi.mock('mockjs', () => ({
  Random: {
    word: vi.fn().mockImplementation(() => 'abcd'),
  },
}));
github-actions[bot] commented 1 year ago

🎊 PR Preview 3d9a212682caf2c6bec4f174843a1afb3935fe31 has been successfully built and deployed to https://ant-design-antd-style-preview-pr-43.surge.sh

:clock1: Build time: 108.383s

🤖 By surge-preview

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 3.0.1-beta.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 3.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: