score:1
Accepted answer
i ended up solving this by creating a manual mock in the mocks folder. the key is that moment exports it's prototype with moment.fn
.
import fixtures from '../__fixtures__/moment';
const moment = require.requireactual('moment');
// by default, the issame function will always return true
let issame = true;
moment.fn.issame = () => issame;
moment.fn.calendar = () => fixtures.calendar;
moment.fn.fromnow = () => fixtures.from_now;
// since format is often called with a variety of format strings, we need to
// differentiate how we are calling the function
moment.fn.format = (format) => {
switch (format) {
case 'mmm do yy':
return fixtures.date;
case 'h:mm a':
return fixtures.time;
case 'mmm do yy h:mm a':
return fixtures.datetime;
default:
return error('unsupported format in moment mock. add case to __mocks__/moment.js');
}
};
moment.duration.fn.humanize = () => fixtures.duration;
// this function is added to moment's prototype in order for our tests to
// change moment's issame behaviour
moment.fn.__issame = (value) => { issame = value; };
// this resets the issame behaviour back to default
moment.fn.__reset = () => {
moment.fn.issame = () => true;
};
export default moment;
jest automatically loads this file when running the tests, and i can then test my components like:
it('renders a moment duration', () => {
expect(wrapper.text()).toequal(fixtures.duration);
});
if i need to change the behavior of the .issame function, i can modify it with:
import moment from 'moment';
beforeall(() => {
moment.fn.__issame(false);
});
score:0
if you are using jest to test, i can recommend you https://github.com/hustcc/jest-date-mock then you can mock current date using
advanceto(new date(2018, 5, 27, 0, 0, 0));
best practice to test is to mock fixed date and compare with result
Source: stackoverflow.com
Related Query
- Mocking moment.js react props with jest and enzyme
- How to mock React component methods with jest and enzyme
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- Testing debounced function in React component with Jest and Enzyme
- Unit test method that calls clearInterval with Jest and Enzyme on React
- Testing React Router with Jest and Enzyme
- React testing with Jest and Enzyme (in Symfony) got "Syntax Error: Unexpected token import"
- React testing state of component with Jest and Enzyme
- Getting started testing React components with Enzyme and Jest
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How to test functions in React with Jest and Enzyme
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- testing custom react methods with jest and enzyme
- Test an whole table React component cells with jest and Enzyme
- Props aren't passing inside component in test cases written with Jest and Enzyme
- stub fetch unit test react with enzyme and jest
- How to mock React component events with jest and enzyme
- Test react components with jest and enzyme and css modules
- Testing react app with jest and enzyme token problem
- React testing with Jest and Enzyme @react-google-maps/api returns TypeError: Cannot read property 'maps' of undefined
- How to test async handleSubmit(e) in react with jest and enzyme
- SyntaxError: Unexpected token '<' on shallow render with Enzyme and Jest in React
- Mock a custom service with jest and enzyme for a react component
- Jest encountered an unexpected token - React with jest and enzyme
- Testing component function in React with Enzyme and Jest
- Unable to get props while testing with jest Enzyme React
- How to test and/or mock ref callback nodes and test .querySelector with Jest mocking + Enzyme shallow?
- Mock a button click in React with Jest and Enzyme
- Testing Modal props with enzyme and jest
- How to test React with hooks and PubSub-js with Jest Enzyme
More Query from same tag
- How to conditionally fetch within useEffect
- method onChange not working in radio button
- Framer motion animate when element is in-view (When you scroll to element)
- Using spread operator to setState in multiple succession
- React.js: counter function for slider component doesn't work properly
- Resize Soccer Pitch to Fill Screen- React/CSS
- How to read redux state inside a component the right way?
- Problem with changing class in React using useState
- How to prevent tooltip going out of screen reactjs
- How do I import an input string into another page
- Can I use DRY Concept for this code in React JS?
- How to import components correctly in react app?
- React router dynamic url no redirecting me to the desired page
- When using history.push to the same component but paginated the component doesn't remount (component will mount isn't called)
- How to get code coverage inside a callback using jest?
- Using private key in a .env file
- setStatus of another Component in react
- Why Google Maps refuse to render when a hashtag sign is present in the address?
- React JS add counting JSON elements
- React-router is not displaying anything in the browser
- How to declare plugin options, using typescript and react-chartjs-2
- Ant design table click on tabel cell
- How to send data to database with user account from Firebase?
- Redux manipulate state object
- Weather API request cors error
- In React.js should I make my initial network request in componentWillMount or componentDidMount?
- Challenges with useEffect, useState
- Reactjs: Direct Firebase connection vs Firebase Redux store
- Fetch weirdness in JavaScript with for...in
- Is there any way to detect window layover on a browser?