Skip to content

Commit

Permalink
chore: migrate to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed May 20, 2023
1 parent 8f1d496 commit 5b1a48f
Show file tree
Hide file tree
Showing 397 changed files with 4,184 additions and 17,971 deletions.
@@ -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.
9 changes: 4 additions & 5 deletions tests/__mocks__/rc-trigger.tsx → __mocks__/rc-trigger.tsx
@@ -1,16 +1,15 @@
import type { TriggerProps } from 'rc-trigger';
import MockTrigger from 'rc-trigger/lib/mock';
import MockTrigger from 'rc-trigger/es/mock';
import * as React from 'react';
import { TriggerMockContext } from '../shared/demoTestContext';
import { TriggerMockContext } from '../tests/shared/demoTestContext';

let OriginTrigger = jest.requireActual('rc-trigger');
OriginTrigger = OriginTrigger.default ?? OriginTrigger;
const { default: OriginTrigger } = await vi.importActual<typeof import('rc-trigger')>('rc-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 as any;

const mergedProps = {
...props,
Expand Down
@@ -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
1 change: 1 addition & 0 deletions __mocks__/rc-virtual-list.ts
@@ -0,0 +1 @@
export { default as default } from 'rc-virtual-list/es/mock';

Check warning on line 1 in __mocks__/rc-virtual-list.ts

View check run for this annotation

codefactor.io / CodeFactor

__mocks__/rc-virtual-list.ts#L1

Export default unnecessarily renamed. (no-useless-rename)

Check warning on line 1 in __mocks__/rc-virtual-list.ts

View check run for this annotation

codefactor.io / CodeFactor

__mocks__/rc-virtual-list.ts#L1

'default' is restricted from being used as an exported name. (no-restricted-exports)
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
4 changes: 2 additions & 2 deletions components/__tests__/index.test.ts
@@ -1,13 +1,13 @@
const OLD_NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const antd = require('..');

describe('antd', () => {
afterAll(() => {
process.env.NODE_ENV = OLD_NODE_ENV;
});

it('exports modules correctly', () => {
it('exports modules correctly', async () => {
const antd = await import('../index');
expect(Object.keys(antd)).toMatchSnapshot();
});
});
18 changes: 11 additions & 7 deletions components/__tests__/node.test.tsx
Expand Up @@ -5,19 +5,22 @@ import type { Options } from '../../tests/shared/demoTest';

(global as any).testConfig = {};

jest.mock('../../tests/shared/demoTest', () => {
vi.mock('../../tests/shared/demoTest', () => {
function fakeDemoTest(name: string, option: Options = {}) {
(global as any).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,19 +30,20 @@ describe('node', () => {
const componentName = componentTestFile.match(/components\/([^/]*)\//)![1];

// Test for ssr
describe(componentName, () => {
// eslint-disable-next-line jest/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
await import(`../../${componentTestFile}`);
const option = (global as any).testConfig?.[componentName];

demoList.forEach((demoFile) => {
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
test(demoFile, async () => {
const Demo = (await import(`../../${demoFile}`)).default;
expect(() => {
renderToString(<Demo />);
}).not.toThrow();
Expand Down
8 changes: 4 additions & 4 deletions components/_util/__tests__/getScroll.test.ts
Expand Up @@ -7,7 +7,7 @@ describe('getScroll', () => {
});

it('getScroll window', async () => {
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
const scrollToSpy = vi.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
window.pageXOffset = x;
window.pageYOffset = y;
});
Expand All @@ -18,7 +18,7 @@ describe('getScroll', () => {
});

it('getScroll document', async () => {
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
const scrollToSpy = vi.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
document.documentElement.scrollLeft = x;
document.documentElement.scrollTop = y;
});
Expand All @@ -30,7 +30,7 @@ describe('getScroll', () => {

it('getScroll div', async () => {
const div = document.createElement('div');
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
const scrollToSpy = vi.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
div.scrollLeft = x;
div.scrollTop = y;
});
Expand All @@ -42,7 +42,7 @@ describe('getScroll', () => {

it('getScroll documentElement', async () => {
const div: any = {};
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
const scrollToSpy = vi.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
div.scrollLeft = null;
div.scrollTop = null;
div.documentElement = {};
Expand Down
2 changes: 1 addition & 1 deletion components/_util/__tests__/responsiveObserve.test.tsx
Expand Up @@ -11,7 +11,7 @@ describe('Test ResponsiveObserve', () => {
return null;
};
render(<Demo />);
const subscribeFunc = jest.fn();
const subscribeFunc = vi.fn();
const token = responsiveObserveRef.subscribe(subscribeFunc);
expect(
responsiveObserveRef.matchHandlers[responsiveObserveRef.responsiveMap.xs].mql.matches,
Expand Down
12 changes: 6 additions & 6 deletions components/_util/__tests__/scrollTo.test.ts
Expand Up @@ -2,27 +2,27 @@ import { waitFakeTimer } from '../../../tests/utils';
import scrollTo from '../scrollTo';

describe('Test ScrollTo function', () => {
const dateNowMock = jest.spyOn(Date, 'now');
const dateNowMock = vi.spyOn(Date, 'now');

beforeAll(() => {
jest.useFakeTimers();
vi.useFakeTimers();
});

beforeEach(() => {
dateNowMock.mockReturnValueOnce(0).mockReturnValueOnce(1000);
});

afterAll(() => {
jest.useRealTimers();
vi.useRealTimers();
});

afterEach(() => {
jest.clearAllTimers();
vi.clearAllTimers();
dateNowMock.mockClear();
});

it('test scrollTo', async () => {
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
const scrollToSpy = vi.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
window.scrollY = y;
window.pageYOffset = y;
});
Expand All @@ -36,7 +36,7 @@ describe('Test ScrollTo function', () => {
});

it('test callback - option', async () => {
const cbMock = jest.fn();
const cbMock = vi.fn();
scrollTo(1000, {
callback: cbMock,
});
Expand Down
14 changes: 7 additions & 7 deletions components/_util/__tests__/util.test.tsx
@@ -1,7 +1,7 @@
/* eslint-disable class-methods-use-this */
import KeyCode from 'rc-util/lib/KeyCode';
import React from 'react';
import { waitFakeTimer, render, fireEvent } from '../../../tests/utils';
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
import getDataOrAriaProps from '../getDataOrAriaProps';
import { isStyleSupport } from '../styleChecker';
import throttleByAnimationFrame from '../throttleByAnimationFrame';
Expand All @@ -10,19 +10,19 @@ import TransButton from '../transButton';
describe('Test utils function', () => {
describe('throttle', () => {
beforeAll(() => {
jest.useFakeTimers();
vi.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
vi.useRealTimers();
});

afterEach(() => {
jest.clearAllTimers();
vi.clearAllTimers();
});

it('throttle function should work', async () => {
const callback = jest.fn();
const callback = vi.fn();
const throttled = throttleByAnimationFrame(callback);
expect(callback).not.toHaveBeenCalled();

Expand All @@ -35,7 +35,7 @@ describe('Test utils function', () => {
});

it('throttle function should be canceled', async () => {
const callback = jest.fn();
const callback = vi.fn();
const throttled = throttleByAnimationFrame(callback);

throttled();
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('Test utils function', () => {
});

it('should trigger onClick when press enter', () => {
const onClick = jest.fn();
const onClick = vi.fn();

const { container } = render(<TransButton onClick={onClick}>TransButton</TransButton>);

Expand Down
8 changes: 5 additions & 3 deletions components/_util/__tests__/warning.test.ts
@@ -1,16 +1,18 @@
import { SpyInstance } from 'vitest';

Check warning on line 1 in components/_util/__tests__/warning.test.ts

View check run for this annotation

codefactor.io / CodeFactor

components/_util/__tests__/warning.test.ts#L1

All imports in the declaration are only used as types. Use `import type`. (@typescript-eslint/consistent-type-imports)

describe('Test warning', () => {
let spy: jest.SpyInstance;
let spy: SpyInstance;

beforeAll(() => {
spy = jest.spyOn(console, 'error').mockImplementation(() => {});
spy = vi.spyOn(console, 'error').mockImplementation(() => {});
});

afterAll(() => {
spy.mockRestore();
});

beforeEach(() => {
jest.resetModules();
vi.resetModules();
});

afterEach(() => {
Expand Down
12 changes: 6 additions & 6 deletions components/_util/__tests__/wave.test.tsx
@@ -1,11 +1,11 @@
import React from 'react';
import mountTest from '../../../tests/shared/mountTest';
import { render, fireEvent, getByText, waitFakeTimer, act } from '../../../tests/utils';
import { act, fireEvent, getByText, render, waitFakeTimer } from '../../../tests/utils';
import Wave from '../wave';

(global as any).isVisible = true;

jest.mock('rc-util/lib/Dom/isVisible', () => {
vi.mock('rc-util/lib/Dom/isVisible', () => {
const mockFn = () => (global as any).isVisible;
return mockFn;
});
Expand All @@ -29,11 +29,11 @@ describe('Wave component', () => {
}

(window as any).ResizeObserver = FakeResizeObserver;
jest.useFakeTimers();
vi.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
vi.useRealTimers();
expect(obCnt).not.toBe(0);
expect(disCnt).not.toBe(0);
});
Expand All @@ -44,7 +44,7 @@ describe('Wave component', () => {
});

afterEach(() => {
jest.clearAllTimers();
vi.clearAllTimers();
const styles = document.getElementsByTagName('style');
for (let i = 0; i < styles.length; i += 1) {
styles[i].remove();
Expand All @@ -66,7 +66,7 @@ describe('Wave component', () => {

function waitRaf() {
act(() => {
jest.advanceTimersByTime(100);
vi.advanceTimersByTime(100);
});
}

Expand Down

0 comments on commit 5b1a48f

Please sign in to comment.