From c47693e3bc7a6362c6f15f48d7e7f4cd03327532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 18 Nov 2018 17:11:49 +0100 Subject: [PATCH 1/5] feat: add debug to render helpers for easier console debugging --- README.md | 27 +++- .../__snapshots__/render.test.js.snap | 138 ++++++++++++++++++ src/__tests__/debug.test.js | 10 +- src/__tests__/render.test.js | 43 +++++- src/debug.js | 18 ++- src/render.js | 10 ++ 6 files changed, 236 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 586658ca5..21cf69433 100644 --- a/README.md +++ b/README.md @@ -139,9 +139,32 @@ Unmount the in-memory tree, triggering the appropriate lifecycle events When using React context providers, like Redux Provider, you'll likely want to wrap rendered component with them. In such cases it's convenient to create your custom `render` method. [Follow this great guide on how to set this up](https://github.com/kentcdodds/react-testing-library#custom-render). -### `toJSON: () => ?ReactTestRendererJSON` +### `debug: (message?: string) => void` -Get the rendered component JSON representation, e.g. for snapshot testing. +Prints deeply rendered component passed to `render` with optional message on top. Uses [debug.deep](#debug) under the hood, but it's easier to use. + +```jsx +const { debug } = render(); + +debug('optional message'); +``` + +logs optional message and colored JSX: + +```jsx +optional message + + + Press me + +``` + +### `debug.shallow: (message?: string) => void` + +Prints shallowly rendered component passed to `render` with optional message on top. Uses [debug.shallow](#debug) under the hood, but it's easier to use. ## `shallow` diff --git a/src/__tests__/__snapshots__/render.test.js.snap b/src/__tests__/__snapshots__/render.test.js.snap index 09bae7c49..63b4a8c3c 100644 --- a/src/__tests__/__snapshots__/render.test.js.snap +++ b/src/__tests__/__snapshots__/render.test.js.snap @@ -1,5 +1,143 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`debug 1`] = ` +" + + Is the banana fresh? + + + not fresh + + + + Change freshness! + + +" +`; + +exports[`debug changing component: bananaFresh button message should now be "fresh" 1`] = ` +" + + Is the banana fresh? + + + fresh + + + + Change freshness! + + +" +`; + +exports[`debug: shallow 1`] = ` +" + + Is the banana fresh? + + + not fresh + + +" +`; + +exports[`debug: shallow with message 1`] = ` +"my other custom message + + + + Is the banana fresh? + + + not fresh + + +" +`; + +exports[`debug: with message 1`] = ` +"my custom message + + + + Is the banana fresh? + + + not fresh + + + + Change freshness! + + +" +`; + exports[`toJSON 1`] = ` { debug(component, 'test message'); - expect(console.log).toBeCalledWith(output, 'test message'); + expect(console.log).toBeCalledWith('test message\n\n', output); }); test('debug.shallow', () => { @@ -56,7 +56,8 @@ test('debug.deep', () => { const component =