React-redux: TypeError: batch is not a function in Jest

Created on 9 Sep 2019  路  10Comments  路  Source: reduxjs/react-redux

    TypeError: batch is not a function
      at Object.batch [as notify] (node_modules/react-redux/lib/utils/Subscription.js:29:7)
      at Subscription.notify [as notifyNestedSubs] (node_modules/react-redux/lib/utils/Subscription.js:71:20)
      at notifyNestedSubs (node_modules/react-redux/lib/components/connectAdvanced.js:272:13)
      at checkForUpdates (node_modules/react-redux/lib/components/connectAdvanced.js:296:9)
      at create (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10867:26)
      at commitHookEffectList (node_modules/react-test-renderer/cjs/react-test-renderer.develo

Versions

    "react": "^16.9.0",
    "react-native": "0.60.5",
    "react-redux": "7.0.0",
    "redux": "^4.0.0",
    "redux-persist": "^5.10.0",

Once I updated to 7.0, this problem appears in all test cases using react-redux.
When I go into node_modules/react-redux/lib/utils/Subscription.js and change line

var batch = (0, _batch.getBatch)();
to
var batch = (0, _batch.getBatch);

it works fine

Most helpful comment

Was able to temporarily get around the issue with this.

jest.mock('../../node_modules/react-redux/lib/utils/batch.js', () => ({
  setBatch: jest.fn(),
  getBatch: () => fn => fn()
}))

All 10 comments

Was able to temporarily get around the issue with this.

jest.mock('../../node_modules/react-redux/lib/utils/batch.js', () => ({
  setBatch: jest.fn(),
  getBatch: () => fn => fn()
}))

I would update beyond 7.0.0 exactly. At least 7.0.2, if not the latest 7.1.1.

@timdorr

Have updated to updated and refreshed node_modules, still the same issue.

    "react-redux": "7.1.1",
    "redux": "^4.0.4",

Can you show an example of the test code that you're using? Is it possible that it's bypassing our index file somehow?

My thought was Alister is doing direct imports. But even then, the getBatch function should still be an actual function.

We should bring over flat bundles to this library at some point. But for now, I would make sure you're doing import { connect } from 'react-redux' and not doing a direct import anywhere.

Sorry actually I think it's to do with the react-native mock we have

We're using react-native-mock-render which doesn't have the unstable_batchedUpdates method

https://github.com/Root-App/react-native-mock-render/blob/fdc2d0d791bc99bf65d1697023bdb6b6be666811/src/react-native.js#L123-L124

Yep, that would do it. Nothing we can do on our end. We depend on that API existing in your react-dom/native implementation.

IMHO, they should add the unstable_* APIs, as those are part of the public API currently (despite their unstable nature). But until they do, we can't do anything on our end.

Was able to temporarily get around the issue with this.

jest.mock('../../node_modules/react-redux/lib/utils/batch.js', () => ({
  setBatch: jest.fn(),
  getBatch: () => fn => fn()
}))

Tnx. This works for me.

In case it helps anyone, here is how I've attempted to implement unstable_batchedUpdates for my React custom renderer without creating a dependency upon React DOM or React Native. It's undocumented, so I have no idea.

My app is now freezing at run-time post state update, rather than crashing at startup, so I think it's at least a step in the right direction. And the latter part about freezing may not even be related.

EDIT: My methodology seems to be exactly the same as that used by react-three, so I can at least have some confidence in advising it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kissyRui picture kissyRui  路  3Comments

juangl picture juangl  路  3Comments

nainardev picture nainardev  路  3Comments

winterbe picture winterbe  路  3Comments

a-koka picture a-koka  路  3Comments