Forked from tannerlinsley/bd3d4befe38185704bf0fc875e9deed6|configuration.json
Created
February 13, 2020 20:17
-
-
Save AgtLucas/63ed547f1037d1c4a68a350778b9845b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// utils/testing-library.js | |
// Set up a custom renderer for @testing-library/react | |
import { render as originalRender } from '@testing-library/react' | |
import diff from 'jest-diff' | |
import chalk from 'chalk' | |
const render = (...args) => { | |
const rendered = originalRender(...args) | |
rendered.lastFragment = new DocumentFragment() | |
rendered.debugDiff = () => { | |
const nextFragment = rendered.asFragment() | |
console.log( | |
diff(rendered.lastFragment, nextFragment, { | |
aAnnotation: 'Previous', | |
bAnnotation: 'Next', | |
aColor: chalk.red, | |
bColor: chalk.green, | |
}) | |
) | |
rendered.lastFragment = nextFragment | |
} | |
return rendered | |
} | |
export * from '@testing-library/react' | |
export { render } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { render, fireEvent } from './react-testing-library' | |
// Usage | |
// `render` now has a `debugDiff` method that shows how the | |
// rendered fragement changes over time | |
test('shows fragment changes over time', () => { | |
const rendered = render(<App />) | |
// First call shows the initial fragment | |
rendered.debugDiff() | |
const input = rendered.getByPlaceholderText('Search...') | |
fireEvent.change(input, { target: { value: 'Foo!' } }) | |
// Shows changes since the last diff | |
rendered.debugDiff() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment