Posts

Showing posts with the label jest

Mock imports to test function logic

Image
Clash Royale CLAN TAG #URR8PPP Mock imports to test function logic Lets say I have the following file. This file imports promiseFunction from a library and I'd like to test the doSomething() function. promiseFunction doSomething() In particular, assert the states when the promise resolves or fails // file: MyComponent.js import promiseFunction from 'somewhere'; class MyClass extends Component { doSomething = () => { promiseFunction() .then((data) => { this.setState({...this.state, name: data.name}); }.catch(() => { this.setState({...this.state, error: 'error'}); }); } } How do I mock the promiseFunction thats being imported. Or really just any function that's being imported. promiseFunction // file: MyClass.spec.js it('sets error in state when doSomething() is rejected', () => { const wrapper = shallow(<MyClass />); // How do I mock promiseFunction here? wrapper.instance().doSomething()...

Is there a good report for jest test case?

Is there a good report for jest test case? I am using jest to test my reactjs application. It works fine but jest doesn't give me a good report about test case results. The output of jest shows each test suite followed by its output. I have more than 50 test suites and it is hard for me to scroll up to check each failed test cases. Is there a brief jest report which printing a brief summary about the whole test cases? below is my jest.conf file: jest jest jest jest jest.conf { "testRegex": "/tests/.*\.test\.jsx?$", "testEnvironment": "node", "roots": ["./src"], "coverageReporters": ["text-summary", "html"] } 2 Answers 2 You can run jest with the --coverage flag. --coverage If you want something different than the default reporters, you have to set them in your jest config file. jest.json { ...