Skip to content

Can't get simple test to work with Redux #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RyanAtViceSoftware opened this issue May 6, 2020 · 13 comments
Closed

Can't get simple test to work with Redux #660

RyanAtViceSoftware opened this issue May 6, 2020 · 13 comments

Comments

@RyanAtViceSoftware
Copy link
Contributor

  • @testing-library/react version:
    9.5.0
  • react version:
    16.13.1
  • node version:
    v12.13.0
  • npm (or yarn) version:
    6.14.5

Relevant code or config:

import React from 'react'
import { Provider } from 'react-redux'
import { combineReducers, createStore } from 'redux'
import { render } from '@testing-library/react'
import '@testing-library/jest-dom'

const initialState = {}

function reducer(state, action) {
	if (typeof state === 'undefined') {
	  return initialState
	}
  
	// For now, don't handle any actions
	// and just return the state given to us.
	return state
  }

it('renders welcome message', () => {
	const store = createStore(combineReducers({ foos: reducer }))

	const { getByText } = render(
		<Provider store={store}>
			<h1>hi</h1>
		</Provider>
	)

	expect(getByText('hi')).toBeInTheDocument()
})

What you did:

Tried to wire up a basic test with react, redux and rtl.

What happened:

Got this error

 Provider(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render noth
ing, return null.

      20 |      const store = createStore(combineReducers({ foos: reducer }))
      21 | 
    > 22 |      const { getByText } = render(
         |                            ^
      23 |              <Provider store={store}>
      24 |                      <h1>hi</h1>
      25 |              </Provider>

      at reconcileChildFibers (node_modules/react-dom/cjs/react-dom.development.js:14348:23)
      at reconcileChildren (node_modules/react-dom/cjs/react-dom.development.js:16762:28)
      at mountIndeterminateComponent (node_modules/react-dom/cjs/react-dom.development.js:17542:5)
      at beginWork (node_modules/react-dom/cjs/react-dom.development.js:18596:16)
      at HTMLUnknownElement.callCallback (node_modules/react-dom/cjs/react-dom.development.js:188:14)
      at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:193:27)
      at HTMLUnknownElementImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
      at HTMLUnknownElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
      at HTMLUnknownElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/nodes/HTMLElement-impl.js:30:27)
      at HTMLUnknownElement.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
      at Object.invokeGuardedCallbackDev (node_modules/react-dom/cjs/react-dom.development.js:237:16)
      at invokeGuardedCallback (node_modules/react-dom/cjs/react-dom.development.js:292:31)
      at beginWork$1 (node_modules/react-dom/cjs/react-dom.development.js:23203:7)
      at performUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:22157:12)
      at workLoopSync (node_modules/react-dom/cjs/react-dom.development.js:22130:22)
      at performSyncWorkOnRoot (node_modules/react-dom/cjs/react-dom.development.js:21756:9)
      at scheduleUpdateOnFiber (node_modules/react-dom/cjs/react-dom.development.js:21188:7)
      at updateContainer (node_modules/react-dom/cjs/react-dom.development.js:24373:3)
      at node_modules/react-dom/cjs/react-dom.development.js:24758:7
      at unbatchedUpdates (node_modules/react-dom/cjs/react-dom.development.js:21903:12)
      at legacyRenderSubtreeIntoContainer (node_modules/react-dom/cjs/react-dom.development.js:24757:5)
      at Object.render (node_modules/react-dom/cjs/react-dom.development.js:24840:10)
      at node_modules/@testing-library/react/dist/pure.js:86:25
      at batchedUpdates$1 (node_modules/react-dom/cjs/react-dom.development.js:21856:12)
      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:929:14)
      at render (node_modules/@testing-library/react/dist/pure.js:82:26)
      at Object.<anonymous> (src/_tests_/App.test.js:22:24)

Reproduction:

  1. clone: https://github.com/vicesoftware/react-redux-hooks-boilerplate/tree/testing-setup
  2. cd webapp
  3. npm install
  4. npm test

webapp/src/_tests_/App.test.js should fail

Problem description:

Because I can't test apps with redux.

Suggested solution:

Hopefully get this working. I'm guessing it's a config but probably helpful to have this issue solved so others can google the fix.

@DenizUgur
Copy link

Have you found a solution to this?

@RyanAtViceSoftware
Copy link
Contributor Author

@DenizUgur no not yet. Are you having same issue?

@DenizUgur
Copy link

Similar. It happens only when I try to generate a coverage report.

@RyanAtViceSoftware
Copy link
Contributor Author

I think I saw a thread about the coverage report issue somewhere

@betofigueiredo
Copy link

@RyanAtViceSoftware I tested your code here and it worked, but I'm using a different version of @testing-library/react: 9.3.2. I didn't try it with 9.5.0, so I can't tell you if it's a version problem, but it's worth a try.

@satyendrasingh8
Copy link

I'm also facing same issue

@kentcdodds
Copy link
Member

Unfortunately this has nothing to do with React Testing Library. You get the same issue when using ReactDOM directly:

image

My guess is there's some tooling issue going on here. Everyone who's commented here is probably experiencing some variant on the same problem. If you update to the latest of jest/babel then this should probably go away.

In any case, there's nothing we can do within React Testing Library because it's unrelated, so I'm going to close this issue. Good luck!

@shashankKeshava
Copy link

shashankKeshava commented Jun 5, 2020

I'm facing the same issue with ` "@testing-library/react": "^10.2.0",

The render simply returns
<body> <div /> </body>

@shashankKeshava
Copy link

Figured out that I was mocking one of the dependencies of the component. After importing the asset it fixed my issue.

@JimLynchCodes
Copy link

@kentcdodds how can you say this is not related to React Testing Library??

React testing library is buggy, doesn't work, and you don't even care... SMH

@JimLynchCodes
Copy link

A good author would link to a working example of testing a react project with redux.

It should not be that hard!

I guess since it's not possible we should all go back to enzyme or move away from this soon-to-be-deprecated react testing library? 🤔

@alexkrolick
Copy link
Collaborator

alexkrolick commented Sep 2, 2021

Unfortunately we do not have the bandwidth to show examples for every possible build config, such as the the "react-app-rewired" system in the reproduction repo.

React Testing Library requires a working DOM context as a prerequisite, and so does Enzyme. This is usually set up via jest-dom, which is enabled by default in most Jest environments prior to 27, and can be enabled in 27+ via a config option "testEnvironment": "jsdom" : https://jestjs.io/blog/2021/05/25/jest-27#flipping-defaults.

The Redux team maintains an extensive guide to how to test using RTL, including setting up jest-dom: https://redux.js.org/usage/writing-tests#connected-components

We link to it here: https://testing-library.com/docs/example-react-redux

Does that help?

@dv-blk
Copy link

dv-blk commented Jan 19, 2022

I had this error when my code contained jest.mock('react-redux') after I altered my test to include redux provider and store in the render instead of stubbing out the redux hooks.

Silly mistake, hopefully I can save someone the same trouble 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants