I'm getting an issue when my jest tests run, even though everything seems to work fine in my app.
I'm getting jest errors like these:
Test suite failed to run
TypeError: Cannot read property 'ADD_TO_CART_EVENT' of undefined
at Object.<anonymous> (node_modules/react-native-branch/src/index.js:9:51)
at Object.<anonymous> (src/js/lib/branchLink.js:87:111)
at Object.<anonymous> (src/js/actions/create.js:142:198)
This seems to be caused by branch, but I'm not using ADD_TO_CART_EVENT anywhere in my code. How can i fix this?
I think the best thing here is to mock Branch in your tests and not actually use the module in your testing. I don't have a lot of insight into that process for you, unfortunately. One of my to-dos is to convert the tests in this module to jest from ava. In the process, I may be able to come up with an easy mock solution you can just import into your tests.
For now, you can borrow the RNBranch and RNBranchEventEmitter mocks from the repo tests: https://github.com/BranchMetrics/react-native-branch-deep-linking/blob/master/test/helpers/RNBranch.mock.js#L5.
So to mock Fabric I used code like this:
jest.mock('react-native-fabric', () => {
return {
Crashlytics: {
crash: () => {}
},
Answers: {
logCustom: () => {},
logContentView: () => {}
}
};
});
What would be the equivalent for Branch?
Sorry, i didn't understand the example.
Here is one other dev's solution: https://github.com/BranchMetrics/react-native-branch-deep-linking/issues/161#issuecomment-294294004
The problem is I'm not very familiar with jest. That's why I can't offer very good advice. But based on what you have there, you could try something like:
jest.mock('react-native-branch', () => {
return {
AddToCartEvent: "Add To Cart",
// etc.
subscribe: (cb) => { cb({params: {}, error: null}) }
// and for createBranchUniversalObject, etc.
}
});
But I'm not sure of the details there. It may require some trial and error.
The other thing you could do is add something like the contents of RNBranch.mock.js. You might have to mock other methods on RNBranch, depending on what you're testing. This will definitely fix the immediate problem, but your unit tests will actually call react-native-branch, so bugs in this SDK may show up as bugs in your unit tests. You're better off with an approach like the above if you can get the details right.
import React from 'react-native'
const defaultSession = {params: {}, error: null}
React.NativeModules.RNBranch = {
// Mock constants exported by native layers
ADD_TO_CART_EVENT: 'Add to Cart',
ADD_TO_WISHLIST_EVENT: 'Add to Wishlist',
PURCHASE_INITIATED_EVENT: 'Purchase Started',
PURCHASED_EVENT: 'Purchased',
REGISTER_VIEW_EVENT: 'View',
SHARE_COMPLETED_EVENT: 'Share Completed',
SHARE_INITIATED_EVENT: 'Share Started',
redeemInitSessionResult() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(defaultSession), 500)
})
}
}
// This only has to exist to be passed to the NativeEventEmitter
// constructor.
React.NativeModules.RNBranchEventEmitter = {
}
Hi! Just experienced this issue myself, but I鈥檓 not entirely happy with the solution proposed. The problem is that I don鈥檛 understand why should I mock parts of the SDK that I鈥檓 not using, so I鈥檓 probably missing something important regarding how Branch works. Could anyone help me with this?
Most helpful comment
Hi! Just experienced this issue myself, but I鈥檓 not entirely happy with the solution proposed. The problem is that I don鈥檛 understand why should I mock parts of the SDK that I鈥檓 not using, so I鈥檓 probably missing something important regarding how Branch works. Could anyone help me with this?