Skip to content

Commit

Permalink
chore: migrate to vitest (#42506)
Browse files Browse the repository at this point in the history
* chore: migrate to vitest

* chore: update ci

* fix: test correctly

* test: support puppeteer

* chore: update coverage

* chore: update include/exclude

* chore: update config

* test: update incorrect tests

* chore: update script

* chore: update

* fix: should close browser at the ended

* chore: improve

* fix: test cause tsc error

* fix: eslint error

* chore: exclude correctly

* test: update snap and fix some tests

* chore: update test config

* fix: countup.js

* fix: incorrect test

* chore: update reference

* test: update

* fix: countup.js

* fix: timeout

* chore: update site test

* fix: fixed countup version

* chore: remove unsed code

* test: update

* test: update demo timeout

* test: update timeout

* chore: update image test

* chore: update threads

* fix: image/svg+xml test failed

* chore: limits threads

* test: update test coverage include

* chore: remove jest files

* chore: rename jest to vi

* chore: update document

* chore: fix missing @types/jsdom

* chore: update coverage

* chore: update snap

* fix:watermark test cases are incorrect

* feat: update ignore comment

* test: fix test case

* test: reset body scrollTop

* test: clean up

* test: use vi

* test: update snapshot

* test: update snapshot

* test: fix dropdown test failed

* fix: toHaveStyle cause test fail

* test: improve test case

* test: fix

* fix: color failed, refer to jsdom/jsdom#3560

* test: fix

* test: fix

* test: fix circular import

* test: revert

* ci: coverage failed

* test: fix c8 ignore comment

* chore: incorrect config

* chore: fix ignore ci

* test: revert svg+xml

* test: fix realTimers

* feat: rc-trigger should be remove

* test: fix some failed test

* chore: remove unused deps and configure eslint-plugin-vitest

* test: update snap

* chore: remove jest

* test: fix lint error

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
Co-authored-by: afc163 <afc163@gmail.com>
  • Loading branch information
3 people committed Jun 7, 2023
1 parent 9b22fd4 commit 6759887
Show file tree
Hide file tree
Showing 454 changed files with 3,577 additions and 3,666 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Expand Up @@ -21,4 +21,3 @@ coverage
**/*.d.ts
# Scripts
scripts/previewEditor/**/*
jest-stare
27 changes: 18 additions & 9 deletions .eslintrc.js
Expand Up @@ -3,7 +3,7 @@ module.exports = {
'airbnb',
'prettier',
'plugin:compat/recommended',
'plugin:jest/recommended',
'plugin:vitest/recommended',
'plugin:react/recommended',
'plugin:import/typescript',
'plugin:markdown/recommended',
Expand All @@ -12,7 +12,6 @@ module.exports = {
browser: true,
node: true,
jasmine: true,
jest: true,
es6: true,
},
settings: {
Expand All @@ -25,7 +24,15 @@ module.exports = {
},
},
parser: '@typescript-eslint/parser',
plugins: ['react', '@babel', 'jest', '@typescript-eslint', 'react-hooks', 'unicorn', 'markdown'],
plugins: [
'react',
'@babel',
'vitest',
'@typescript-eslint',
'react-hooks',
'unicorn',
'markdown',
],
// https://github.com/typescript-eslint/typescript-eslint/issues/46#issuecomment-470486034
overrides: [
{
Expand Down Expand Up @@ -152,8 +159,10 @@ module.exports = {
'scripts/**',
'**/*.test.js',
'**/__tests__/*',
'__mocks__/**',
'*.config.js',
'**/*.md',
'vitest*config.ts',
],
},
],
Expand All @@ -179,12 +188,12 @@ module.exports = {
'no-restricted-globals': 0,
'max-classes-per-file': 0,

'jest/no-test-callback': 0,
'jest/expect-expect': 0,
'jest/no-done-callback': 0,
'jest/valid-title': 0,
'jest/no-conditional-expect': 0,
'jest/no-standalone-expect': 0,
'vitest/prefer-to-be': 0,
'vitest/expect-expect': 0,
'vitest/no-done-callback': 0,
'vitest/valid-title': 0,
'vitest/no-conditional-expect': 0,
'vitest/no-standalone-expect': 0,

'unicorn/better-regex': 2,
'unicorn/prefer-string-trim-start-end': 2,
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -191,7 +191,7 @@ jobs:
# dom test
- name: dom test
if: ${{ matrix.module == 'dom' }}
run: npm test -- --maxWorkers=2 --shard=${{matrix.shard}} --coverage
run: npm test -- --shard=${{matrix.shard}} --coverage

- name: persist coverages
if: ${{ matrix.module == 'dom' && matrix.react == '17' }}
Expand Down Expand Up @@ -347,7 +347,7 @@ jobs:
- name: test
# lib only run in master branch not in pull request
if: ${{ github.event_name != 'pull_request' || matrix.module != 'lib' }}
run: npm test -- --maxWorkers=2 --shard=${{matrix.shard}}
run: npm test -- --shard=${{matrix.shard}}
env:
LIB_DIR: ${{ matrix.module }}
needs: compile
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -58,7 +58,6 @@ components/version/token-meta.json
# Image snapshot diff
__diff_output__/
__image_snapshots__/
/jest-stare
/imageSnapshots
/imageDiffSnapshots

Expand Down
23 changes: 0 additions & 23 deletions .jest.image.js

This file was deleted.

68 changes: 0 additions & 68 deletions .jest.js

This file was deleted.

19 changes: 0 additions & 19 deletions .jest.node.js

This file was deleted.

21 changes: 0 additions & 21 deletions .jest.site.js

This file was deleted.

@@ -1,16 +1,17 @@
import type { TriggerProps } from '@rc-component/trigger';
import MockTrigger from '@rc-component/trigger/lib/mock';
import MockTrigger from '@rc-component/trigger/es/mock';
import * as React from 'react';
import { TriggerMockContext } from '../../shared/demoTestContext';
import { TriggerMockContext } from '../../tests/shared/demoTestContext';

let OriginTrigger = jest.requireActual('@rc-component/trigger');
OriginTrigger = OriginTrigger.default ?? OriginTrigger;
const { default: OriginTrigger } = await vi.importActual<typeof import('@rc-component/trigger')>(
'@rc-component/trigger',
);

const ForwardTrigger = React.forwardRef<any, TriggerProps>((props, ref) => {
const context = React.useContext(TriggerMockContext);

const mergedPopupVisible = context?.popupVisible ?? props.popupVisible;
(global as any).triggerProps = props;
globalThis.triggerProps = props;

const mergedProps = {
...props,
Expand Down
File renamed without changes.
@@ -1,8 +1,8 @@
import React from 'react';
import { TriggerMockContext } from '../../../shared/demoTestContext';
import { TriggerMockContext } from '../../../tests/shared/demoTestContext';

const { default: OriginPortal } = await vi.importActual<typeof import('rc-util/es/Portal')>('rc-util/es/Portal');

let OriginPortal = jest.requireActual('rc-util/lib/Portal');
OriginPortal = OriginPortal.default ?? OriginPortal;
class MockPortal extends React.Component<{ children?: React.ReactNode }> {
container: boolean;

Expand Down
3 changes: 3 additions & 0 deletions __mocks__/rc-virtual-list.ts
@@ -0,0 +1,3 @@
import Mock from 'rc-virtual-list/es/mock';

export default Mock;
12 changes: 6 additions & 6 deletions components/__tests__/__snapshots__/index.test.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`antd exports modules correctly 1`] = `
exports[`antd > exports modules correctly 1`] = `
[
"Affix",
"Alert",
Expand Down Expand Up @@ -37,7 +37,9 @@ exports[`antd exports modules correctly 1`] = `
"List",
"Mentions",
"Menu",
"message",
"Modal",
"notification",
"Pagination",
"Popconfirm",
"Popover",
Expand All @@ -59,6 +61,7 @@ exports[`antd exports modules correctly 1`] = `
"Table",
"Tabs",
"Tag",
"theme",
"TimePicker",
"Timeline",
"Tooltip",
Expand All @@ -68,10 +71,7 @@ exports[`antd exports modules correctly 1`] = `
"TreeSelect",
"Typography",
"Upload",
"Watermark",
"message",
"notification",
"theme",
"version",
"Watermark",
]
`;
4 changes: 2 additions & 2 deletions components/__tests__/__snapshots__/setup.test.tsx.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`SetUp.Test diff of React 18 & React 17 1`] = `
exports[`SetUp.Test > diff of React 18 & React 17 1`] = `
NodeList [
<div>
bamboo
Expand Down
3 changes: 2 additions & 1 deletion components/__tests__/index.test.ts
@@ -1,6 +1,7 @@
import * as antd from '..';

const OLD_NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const antd = require('..');

describe('antd', () => {
afterAll(() => {
Expand Down
37 changes: 22 additions & 15 deletions components/__tests__/node.test.tsx
Expand Up @@ -3,21 +3,24 @@ import * as React from 'react';
import { renderToString } from 'react-dom/server';
import type { Options } from '../../tests/shared/demoTest';

(global as any).testConfig = {};
globalThis.testConfig = {};

jest.mock('../../tests/shared/demoTest', () => {
vi.mock('../../tests/shared/demoTest', () => {
function fakeDemoTest(name: string, option: Options = {}) {
(global as any).testConfig[name] = option;
globalThis.testConfig[name] = option;
}

fakeDemoTest.rootPropsTest = () => {};

return fakeDemoTest;
return {
default: fakeDemoTest,
rootPropsTest: () => {},
};
});

describe('node', () => {
beforeAll(() => {
jest.useFakeTimers().setSystemTime(new Date('2016-11-22'));
vi.useFakeTimers().setSystemTime(new Date('2016-11-22'));
});

// Find the component exist demo test file
Expand All @@ -27,23 +30,27 @@ describe('node', () => {
const componentName = componentTestFile.match(/components\/([^/]*)\//)![1];

// Test for ssr
describe(componentName, () => {
// eslint-disable-next-line vitest/valid-describe-callback
describe(componentName, async () => {
const demoList = globSync(`./components/${componentName}/demo/*.tsx`);

// Use mock to get config
require(`../../${componentTestFile}`); // eslint-disable-line global-require, import/no-dynamic-require
const option = (global as any).testConfig?.[componentName];

await import(`../../${componentTestFile}`);
demoList.forEach((demoFile) => {
const option = globalThis.testConfig?.[componentName];
const skip: string[] = option?.skip || [];
const test = skip.some((skipMarkdown) => demoFile.includes(skipMarkdown)) ? it.skip : it;

test(demoFile, () => {
const Demo = require(`../../${demoFile}`).default; // eslint-disable-line global-require, import/no-dynamic-require
expect(() => {
renderToString(<Demo />);
}).not.toThrow();
});
test(
demoFile,
async () => {
const Demo = (await import(`../../${demoFile}`)).default;
expect(() => {
renderToString(<Demo />);
}).not.toThrow();
},
15000,
);
});
});
});
Expand Down

0 comments on commit 6759887

Please sign in to comment.