ant-design/tests/setup.ts
Dunqing 6759887c44
chore: migrate to vitest (#42506)
* 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 https://github.com/jsdom/jsdom/pull/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>
2023-06-07 11:54:50 +08:00

189 lines
5.7 KiB
TypeScript

/* eslint-disable no-console */
import type { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';
import matchers from '@testing-library/jest-dom/matchers';
import React from 'react';
import util from 'util';
import { expect, vi } from 'vitest';
import { toHaveNoViolations } from 'jest-axe';
import jsdom from 'jsdom';
import format, { plugins } from 'pretty-format';
import { defaultConfig } from '../components/theme/internal';
console.log('Current React Version:', React.version);
declare module 'vitest' {
interface Assertion<T = any> extends jest.Matchers<void, T>, TestingLibraryMatchers<T, void> {
toHaveNoViolations(): void;
}
}
expect.extend(matchers);
const originConsoleErr = console.error;
// Hack off React warning to avoid too large log in CI.
console.error = (...args) => {
const str = args.join('').replace(/\n/g, '');
if (
['validateDOMNesting', 'on an unmounted component', 'not wrapped in act'].every(
(warn) => !str.includes(warn),
)
) {
originConsoleErr(...args);
}
};
if (typeof window !== 'undefined') {
globalThis.window.resizeTo = (width, height) => {
globalThis.window.innerWidth = width || globalThis.window.innerWidth;
globalThis.window.innerHeight = height || globalThis.window.innerHeight;
globalThis.window.dispatchEvent(new Event('resize'));
};
globalThis.window.scrollTo = () => {};
// ref: https://github.com/ant-design/ant-design/issues/18774
if (!window.matchMedia) {
Object.defineProperty(globalThis.window, 'matchMedia', {
writable: true,
configurable: true,
value: vi.fn((query) => ({
matches: query.includes('max-width'),
addListener: vi.fn(),
removeListener: vi.fn(),
})),
});
}
// Fix css-animation or rc-motion deps on these
// https://github.com/react-component/motion/blob/9c04ef1a210a4f3246c9becba6e33ea945e00669/src/util/motion.ts#L27-L35
// https://github.com/yiminghe/css-animation/blob/a5986d73fd7dfce75665337f39b91483d63a4c8c/src/Event.js#L44
window.AnimationEvent = window.AnimationEvent || window.Event;
window.TransitionEvent = window.TransitionEvent || window.Event;
// https://github.com/nickcolley/jest-axe/issues/147#issuecomment-758804533
const { getComputedStyle } = window;
window.getComputedStyle = (elt) => getComputedStyle(elt);
// ref: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
// ref: https://github.com/jsdom/jsdom/issues/2524
if (!window.TextDecoder) {
Object.defineProperty(window, 'TextEncoder', { writable: true, value: util.TextEncoder });
Object.defineProperty(window, 'TextDecoder', { writable: true, value: util.TextDecoder });
}
}
vi.mock('@rc-component/trigger');
vi.mock('rc-util/es/Portal');
vi.mock('rc-virtual-list');
vi.mock('copy-to-clipboard');
// Not use dynamic hashed for test env since version will change hash dynamically.
defaultConfig.hashed = false;
if (process.env.LIB_DIR === 'dist') {
vi.mock('../dist/antd', async (importOriginal) => {
const antd = await importOriginal<typeof import('antd')>();
antd.theme.defaultConfig.hashed = false;
return antd;
});
} else if (process.env.LIB_DIR === 'es') {
vi.mock('../es/theme/internal', async (importOriginal) => {
const esTheme = await importOriginal<typeof import('../components/theme/internal')>();
if (esTheme.defaultConfig) {
esTheme.defaultConfig.hashed = false;
}
return esTheme;
});
}
function cleanup(node: HTMLElement) {
const childList = Array.from(node.childNodes);
node.innerHTML = '';
childList.forEach((child) => {
if (!(child instanceof Text)) {
node.appendChild(cleanup(child as any));
} else if (child.textContent) {
node.appendChild(child);
}
});
return node;
}
function formatHTML(nodes: any) {
let cloneNodes: any;
if (Array.isArray(nodes) || nodes instanceof HTMLCollection || nodes instanceof NodeList) {
cloneNodes = Array.from(nodes).map((node) => cleanup(node.cloneNode(true) as any));
} else {
cloneNodes = cleanup(nodes.cloneNode(true));
}
const htmlContent = format(cloneNodes, {
plugins: [plugins.DOMCollection, plugins.DOMElement],
});
const filtered = htmlContent
.split(/[\n\r]+/)
.filter((line) => line.trim())
.map((line) => line.replace(/\s+$/, ''))
.join('\n');
return filtered;
}
/**
* React 17 & 18 will have different behavior in some special cases:
*
* React 17:
*
* ```html
* <span> Hello World </span>
* ```
*
* React 18:
*
* ```html
* <span> Hello World </span>
* ```
*
* These diff is nothing important in front end but will break in snapshot diff.
*/
expect.addSnapshotSerializer({
test: (element) =>
typeof HTMLElement !== 'undefined' &&
(element instanceof HTMLElement ||
element instanceof DocumentFragment ||
element instanceof HTMLCollection ||
(Array.isArray(element) && element[0] instanceof HTMLElement)),
print: (element) => formatHTML(element),
});
/** Demo Test only accept render as SSR to make sure align with both `server` & `client` side */
expect.addSnapshotSerializer({
test: (node) => node && typeof node === 'object' && node.type === 'demo' && node.html,
print: ({ html }) => {
const { JSDOM } = jsdom;
const { document } = new JSDOM().window;
document.body.innerHTML = html;
const children = Array.from(document.body.childNodes);
// Clean up `data-reactroot` since React 18 do not have this
children.forEach((ele: HTMLElement) => {
if (typeof ele.removeAttribute === 'function') {
ele.removeAttribute('data-reactroot');
}
});
return formatHTML(children.length > 1 ? children : children[0]);
},
});
expect.extend(toHaveNoViolations);
// with jest-canvas-mock
(globalThis as any).jest = vi;
await import('jest-canvas-mock');