chore: Introduce customer jest match to support matchRenderedSnapshot (#22947)

* Introduce customer jest match to support matchRenderedSnapshot

* lint:tsc stop emiting
This commit is contained in:
Eric Wang 2020-04-06 14:05:38 +10:00 committed by GitHub
parent c4fb9edfe0
commit 095e21220f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 31 deletions

View File

@ -10,6 +10,7 @@ const transformIgnorePatterns = [
module.exports = {
verbose: true,
setupFiles: ['./tests/setup.js'],
setupFilesAfterEnv: ['./tests/setupAfterEnv.ts'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'md'],
modulePathIgnorePatterns: ['/_site/'],
moduleNameMapper: {

View File

@ -3,6 +3,7 @@ const { moduleNameMapper, transformIgnorePatterns } = require('./.jest');
// jest config for server render environment
module.exports = {
setupFiles: ['./tests/setup.js'],
setupFilesAfterEnv: ['./tests/setupAfterEnv.ts'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'md'],
moduleNameMapper,
transform: {

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { render, mount } from 'enzyme';
import { mount } from 'enzyme';
import renderer from 'react-test-renderer';
import { SearchOutlined } from '@ant-design/icons';
import Button from '..';
@ -26,8 +26,7 @@ describe('Button', () => {
rtlTest(() => <Button.Group size="middle" />);
it('renders correctly', () => {
const wrapper = render(<Button>Follow</Button>);
expect(wrapper).toMatchSnapshot();
expect(<Button>Follow</Button>).toMatchRenderedSnapshot();
});
it('mount correctly', () => {
@ -45,40 +44,33 @@ describe('Button', () => {
});
it('renders Chinese characters correctly', () => {
const wrapper = render(<Button>按钮</Button>);
expect(wrapper).toMatchSnapshot();
expect(<Button>按钮</Button>).toMatchRenderedSnapshot();
// should not insert space when there is icon
const wrapper1 = render(<Button icon={<SearchOutlined />}>按钮</Button>);
expect(wrapper1).toMatchSnapshot();
expect(<Button icon={<SearchOutlined />}>按钮</Button>).toMatchRenderedSnapshot();
// should not insert space when there is icon
const wrapper2 = render(
expect(
<Button>
<SearchOutlined />
按钮
</Button>,
);
expect(wrapper2).toMatchSnapshot();
).toMatchRenderedSnapshot();
// should not insert space when there is icon
const wrapper3 = render(<Button icon={<SearchOutlined />}>按钮</Button>);
expect(wrapper3).toMatchSnapshot();
expect(<Button icon={<SearchOutlined />}>按钮</Button>).toMatchRenderedSnapshot();
// should not insert space when there is icon while loading
const wrapper4 = render(
expect(
<Button icon={<SearchOutlined />} loading>
按钮
</Button>,
);
expect(wrapper4).toMatchSnapshot();
).toMatchRenderedSnapshot();
// should insert space while loading
const wrapper5 = render(<Button loading>按钮</Button>);
expect(wrapper5).toMatchSnapshot();
expect(<Button loading>按钮</Button>).toMatchRenderedSnapshot();
// should insert space while only one nested element
const wrapper6 = render(
expect(
<Button>
<span>按钮</span>
</Button>,
);
expect(wrapper6).toMatchSnapshot();
).toMatchRenderedSnapshot();
});
it('renders Chinese characters correctly in HOC', () => {
@ -103,8 +95,7 @@ describe('Button', () => {
// https://github.com/ant-design/ant-design/issues/18118
it('should not insert space to link button', () => {
const wrapper = render(<Button type="link">按钮</Button>);
expect(wrapper).toMatchSnapshot();
expect(<Button type="link">按钮</Button>).toMatchRenderedSnapshot();
});
it('should render empty button without errors', () => {
@ -191,12 +182,9 @@ describe('Button', () => {
});
it('fixbug renders {0} , 0 and {false}', () => {
const wrapper = render(<Button>{0}</Button>);
expect(wrapper).toMatchSnapshot();
const wrapper1 = render(<Button>0</Button>);
expect(wrapper1).toMatchSnapshot();
const wrapper2 = render(<Button>{false}</Button>);
expect(wrapper2).toMatchSnapshot();
expect(<Button>{0}</Button>).toMatchRenderedSnapshot();
expect(<Button>0</Button>).toMatchRenderedSnapshot();
expect(<Button>{false}</Button>).toMatchRenderedSnapshot();
});
it('should has click wave effect', async () => {

View File

@ -65,7 +65,7 @@
"lint:md": "remark components/",
"lint:script": "eslint . --ext '.js,.jsx,.ts,.tsx'",
"lint:style": "stylelint '{site,components}/**/*.less' --syntax less",
"lint:tsc": "npm run tsc",
"lint:tsc": "npm run tsc -- --noEmit",
"pre-publish": "npm run check-commit && npm run test-all",
"prettier": "prettier -c --write '**/*'",
"pretty-quick": "pretty-quick",
@ -148,6 +148,7 @@
"@qixian.cs/github-contributors-list": "^1.0.3",
"@stackblitz/sdk": "^1.3.0",
"@types/classnames": "^2.2.8",
"@types/enzyme": "^3.10.5",
"@types/gtag.js": "^0.0.3",
"@types/jest": "^25.1.0",
"@types/lodash": "^4.14.139",

View File

@ -0,0 +1,21 @@
import { render } from 'enzyme';
import { ReactElement } from 'react';
export default function toMatchRenderedSnapshot(
this: jest.MatcherUtils,
jsx: ReactElement<unknown>,
): { message(): string; pass: boolean } {
try {
expect(render(jsx)).toMatchSnapshot();
return {
message: () => 'expected JSX not to match snapshot',
pass: true,
};
} catch (e) {
return {
message: () => `expected JSX to match snapshot: ${e.message}`,
pass: false,
};
}
}

5
tests/setupAfterEnv.ts Normal file
View File

@ -0,0 +1,5 @@
import toMatchRenderedSnapshot from './matchers/rendered-snapshot';
expect.extend({
toMatchRenderedSnapshot,
});

View File

@ -1,7 +1,7 @@
import React from 'react';
import Moment from 'moment';
import MockDate from 'mockdate';
import { render, mount } from 'enzyme';
import { mount } from 'enzyme';
import ConfigProvider from '../../components/config-provider';
// eslint-disable-next-line jest/no-export
@ -16,7 +16,7 @@ export default function rtlTest(Component, mockDate) {
<Component />
</ConfigProvider>,
);
expect(render(wrapper)).toMatchSnapshot();
expect(wrapper).toMatchRenderedSnapshot();
if (mockDate) {
MockDate.reset();
}

5
typings/jest.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare namespace jest {
interface Matchers<R> {
toMatchRenderedSnapshot(): R;
}
}