2021-12-02 20:25:31 +08:00
|
|
|
import { toHaveNoViolations } from 'jest-axe';
|
2022-04-14 18:09:19 +08:00
|
|
|
import '@testing-library/jest-dom';
|
2022-08-22 22:54:38 +08:00
|
|
|
import format, { plugins } from 'pretty-format';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 => {
|
|
|
|
const htmlContent = format(element, {
|
|
|
|
plugins: [plugins.DOMCollection, plugins.DOMElement],
|
|
|
|
});
|
|
|
|
|
|
|
|
const filtered = htmlContent
|
|
|
|
.split(/[\n\r]+/)
|
|
|
|
.filter(line => line.trim())
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
return filtered;
|
|
|
|
},
|
|
|
|
});
|
2020-04-06 12:05:38 +08:00
|
|
|
|
2021-12-02 20:25:31 +08:00
|
|
|
expect.extend(toHaveNoViolations);
|