score:172
You can mock the module so it returns spies and import it into your test:
import {navigationEnabled, guidanceEnabled} from '../../../magic/index'
jest.mock('../../../magic/index', () => ({
navigationEnabled: jest.fn(),
guidanceEnabled: jest.fn()
}));
Then later on you can change the actual implementation using mockImplementation
navigationEnabled.mockImplementation(()=> true)
//or
navigationEnabled.mockReturnValueOnce(true);
and in the next test
navigationEnabled.mockImplementation(()=> false)
//or
navigationEnabled.mockReturnValueOnce(false);
score:9
I had a hard time getting the accepted answers to work - my equivalents of navigationEnabled
and guidanceEnabled
were undefined when I tried to call mockReturnValueOnce
on them.
Here's what I had to do:
In ../../../magic/__mocks__/index.js
:
export const navigationEnabled = jest.fn();
export const guidanceEnabled = jest.fn();
in my index.test.js
file:
jest.mock('../../../magic/index');
import { navigationEnabled, guidanceEnabled } from '../../../magic/index';
import { functionThatReturnsValueOfNavigationEnabled } from 'moduleToTest';
it('is able to mock', () => {
navigationEnabled.mockReturnValueOnce(true);
guidanceEnabled.mockReturnValueOnce(true);
expect(functionThatReturnsValueOfNavigationEnabled()).toBe(true);
});
score:36
what you want to do is
import { navigationEnabled, guidanceEnabled } from '../../../magic/index';
jest.mock('../../../magic/index', () => ({
navigationEnabled: jest.fn(),
guidanceEnabled: jest.fn()
}));
describe('test suite', () => {
it('every test', () => {
navigationEnabled.mockReturnValueOnce(value);
guidanceEnabled.mockReturnValueOnce(value);
});
});
you can look more about these functions here =>https://facebook.github.io/jest/docs/mock-functions.html#mock-return-values
Source: stackoverflow.com
Related Query
- how to change jest mock function return value in each test?
- How to test a return value of a mock function in jest
- How to unit test useEffect cleanUp return function using Jest and Enzyme
- Test return value of function with Jest in React
- How to mock react custom hook return value as a module with Jest
- How to test the "onChange" function who will change the value of state with the hooks
- How to set a value to an input text element of a stateless function inside a Jest test
- How to test return function value
- How to test a function call after state receive value inside expression in React function component using Jest
- How to mock a variable in ES6 Module with jest such that the actual function runs with mocked value
- Jest unit test - mock value of async function within async function
- how to mock variables inside a function in jest
- How to test class instance inside a function with Jest
- How can mock the value of a state and data in my react test
- How to replace global function while doing unit test with jest
- How fix this warrning warning Array.prototype.map() expects a return value from arrow function array-callback-return?
- Using Jest and Enzyme, how do I test a function passed in through props?
- How can i use Jest to test a function inside a stateless component?
- How to make key value of map function increment for each element in JavaScript
- How to mock async function using jest framework?
- How to test with jest and typescript with types a basic react function component
- How can a function value is displayed in a HTML tag with a return value from addEventListerner click?
- Jest unit test with Luxon: how do I mock .setZone('local')
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- Jest unit test - How do I call through async function in a setTimeout repeating function
- How to correctly mock function in Jest tests when using Rollup.js
- Jest mock value per test
- Jest jest.mock function with different return value
- How to test useParam() function with Jest
- How to get mock utility function with jest
More Query from same tag
- Initialising a useReducer with async data in a hook
- How to render a notification from a global function - React
- Dispatching first runs undefined and then resolves in Redux Thunk
- Change Next.js background color with TailwindCSS
- How do I set a global font colour in React which uses Material UI
- Simulating user interaction with a React component in a Jest test
- ReactJS | Cannot update during an existing state transition
- Cannot read property 'X' of undefined - React-bootstrap
- Add value from inputs to array if not exists
- material ui table horizontal scroll
- Spring Boot - Issues redirecting to external URL with AuthenticationSuccessHandler
- Child component list not rendering
- Jest/enzyme: How to test for .then and .catch of an nested asynchronous function
- Recharts - Adjust the position of the yAxis cartesian grid
- fill an object inside another object using useState
- place the footer at the bottom of a react native Drawer
- How to pass an object with onClick React?
- antd - Form input dynamic validation
- How to send POST method with quotation marked key data?
- Antd Table: autosize scroll:y to parent
- Listen for events from PhotoEditorSDK in Angular
- Implementing Apollo Client for NextJS but getting Cannot read property 'WebSocket' of undefined
- How to use Google reCAPTCHA in the Ant Design Form component?
- Pass Current Table Row values to a subcomponent with onClick of a button in React (Material UI)
- ReactJS Unexpected block statement surrounding arrow body
- PWA icon does not appear while adding to home screen using SAFARI
- ASP.NET Core DateTime Rest API with Javascript converts to UTC instead of java
- img.onload refuses to fire after new Image() declaration
- Undefined TypeError when accessing object property
- Best way to find React button with selenium?