ant-design/tests/setupAfterEnv.ts
二货爱吃白萝卜 45eeee60bb
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
feat: Add unstable api for React 19 compitable (#51979)
* chore: add unstable entrance

* chore: rest of it

* chore: use React 19

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: test ignore 19 preload

* chore: bump rc-util

* fix: warning of pure render

* fix: warning of 19

* chore: adjust ts

* test: fix test logic

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* chore: restore file

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: update test

* test: fix test case

* test: update snapshot

* test: fix coverage

* test: fix coverage

* test: add ignore image
2024-12-18 14:09:49 +08:00

116 lines
3.2 KiB
TypeScript

import '@testing-library/jest-dom';
import { toHaveNoViolations } from 'jest-axe';
import jsdom from 'jsdom';
import format, { plugins } from 'pretty-format';
import { defaultConfig } from '../components/theme/internal';
// Not use dynamic hashed for test env since version will change hash dynamically.
defaultConfig.hashed = false;
if (process.env.LIB_DIR === 'dist') {
jest.mock('antd', () => jest.requireActual('../dist/antd'));
} else if (process.env.LIB_DIR === 'dist-min') {
jest.mock('antd', () => jest.requireActual('../dist/antd.min'));
} else if (process.env.LIB_DIR === 'es') {
jest.mock('antd', () => jest.requireActual('../es'));
jest.mock('../es/theme/internal', () => {
const esTheme = jest.requireActual('../es/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')
.filter((line) => line.trim())
.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,
// @ts-ignore
print: ({ html }) => {
const { JSDOM } = jsdom;
const { document } = new JSDOM().window;
document.body.innerHTML = html;
const children = Array.from(document.body.childNodes).filter(
(node) =>
// Ignore `link` node since React 18 or blew not support this
node.nodeName !== 'LINK',
);
// Clean up `data-reactroot` since React 18 do not have this
// @ts-ignore
children.forEach((ele: HTMLElement) => {
if (typeof ele.removeAttribute === 'function') {
ele.removeAttribute('data-reactroot');
}
});
return formatHTML(children.length > 1 ? children : children[0]);
},
});
expect.extend(toHaveNoViolations);