Testing Actions Using Jest Is Throwing Error Module Not Found, Where Module Is Used In Component
I am writing unit test for redux actions, there are no other tests that are being run. when i run npm run test , it finds the file for actions test and runs it. the test fails throwing the error of a module not defined.
I tried mocking the module and it works fine after it, But why does testing actions only require me to mock modules not imported in the actions file
Actions testing code :
import * as actions from '../app/screens/NewOrders/NewOrdersAction'
import AppConstants from '../app/Constants/AppConstants'
import { Thunk } from 'redux-testkit';
describe('actions validation for New Orders Action Component', async() => {
it('should get the Service Orders', async () => {
const expectedAction = {
type: AppConstants.SET_ORDERS,
payload: {"data": 12}
}
const dispatches = await Thunk(actions.getOrders).execute('shubham.anand','5',5);
expect(dispatches[0].getAction()).toEqual(expectedAction);
})
});
Action code:
import AppConstants from '../../Constants/AppConstants'
import {GetData} from '../../Utils/Utils'
/**
* @module NewOrdersAction
*/
/**
*This action is used to get the service orders in the Tasks based on the UserID
*@function getServiceOrders
*/
export const getOrders = (user_id,id,orderlimit) => dispatch => {
GetData.getOrders(user_id,id,orderlimit).then(res => {
if (!res.error) {
dispatch({
type: AppConstants.SET_ORDERS,
payload: res
})
}
})
}
TypeError: Cannot read property 'trackEvent' of undefined
> 1 | import React from "react";
2 | import {
3 | Animated,
4 | Dimensions,
at Object.trackEvent (node_modules/appcenter-analytics/Analytics.js:13:46)
at Object.<anonymous> (app/screens/SplashScreen/SplashScreenLoader.js:1:1819)
at Object.<anonymous> (app/index.js:1:858)
at Object.<anonymous> (index.js:1:1031)
Trackevent is a method in module i use for analytics
Answer
Your test can't find the node module, so you have to mock it. Mocking it destroys its native behavior, but it bypasses the need for your tests to try and load the module it can't properly find. Inside your test file, you need something like this. This automatically mocks every function that is in appcenter-analytics
.
import Analytics from 'appcenter-analytics'
jest.mock('appcenter-analytics')
If you actually need trackEvent
to run for your tests, you still mock it. But you give it an actual return
value with no algorithmic logic inside it. Explicitly mock trackEvent
underneath and give the function some default return
value in the form of a callback of jest.fn
// in your test, regardless of the arguments put in, the function that runs it will evaluate to 1000
Analytics.trackEvent= jest.fn(() => 1000)
EDIT:
At some point in time, trackEvent
is being used or imported in the file you're testing. It might be an import in the file itself or be part of a function from a different file that, at some point, needs to be run in order for the function to operate. It might be an import from another file in your codebase that also imports trackEvent
. It doesn't matter if your explicit function doesn't use trackEvent
. The files still have to be run in their entirety, which means it also attempts to get data from every import statement in the file.
Your code needs to be able to run before it can be tested. So if it tries to run a function that it can't find (in this case trackEvent
), it will fail.
This behavior is no different from any part of your codebase. Can you run a function using an npm package you haven't installed yet? Your test environment is different from your production and develop environments
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?