enzyme

Jest test fails for async function that calls another async function

守給你的承諾、 提交于 2021-01-28 12:10:25
问题 I am trying to test an async function that uses data returned by another async function. Here is the code to explain my problem: StudentInfo.js export async function getData() { //studentData imported from another file return new Promise(resolve => { setTimeout(() => { resolve(studentData); }, 5000); }); } export async function filterStudents() { const studentData = await StudentInfo.getData(); //computations return filteredData; //object } StudentInfo.test.js import * as studentInfo from

Enzyme instance() returns null

寵の児 提交于 2021-01-28 03:03:58
问题 I have the following test: describe('Form', () => { let store; let wrapper; beforeEach(() => { store = mockStore(mockData); wrapper = mount( <Provider store={store}> <Form /> </Provider> ); }); it('handleForm calls uses validate() for validation', () => { const instance = wrapper.instance(); const submitFormButton = wrapper.find('.submitFormButton'); submitFormButton.simulate('click'); console.log(instance); // null }); }); Any idea of what I'm doing wrong exactly? I know that Enzyme has this

Testing asynchronous componentDidMount that changes state with Jest and Enzyme

元气小坏坏 提交于 2021-01-28 02:05:35
问题 All I am doing in my code is upon componentDidMount being run, I am making an axios get request to GitHub, and setting some data back onto state. However, when I run the test it still says the state is an empty array. Here is my component below: export default class HelloWorld extends Component { constructor(props) { super(props) this.state = { goodbye: false, data: [] } } async componentDidMount() { await this.func() } func = async () => { let data = await axios.get('https://api.github.com

How to mock/spy useState hook in jest?

亡梦爱人 提交于 2021-01-27 18:41:12
问题 I am trying to spy on useState React hook but i always get the test failed This is my React component: const Counter= () => { const[counter, setCounter] = useState(0); const handleClick=() => { setCounter(counter + 1); } return ( <div> <h2>{counter}</h2> <button onClick={handleClick} id="button">increment</button> </div> ) } counter.test.js : it('increment counter correctlry', () => { let wrapper = shallow(<Counter/>); const setState = jest.fn(); const useStateSpy = jest.spyOn(React,

Babel throwing Support for the experimental syntax 'jsx' isn't currently enabled

泪湿孤枕 提交于 2021-01-26 03:15:02
问题 I started newly writing unit test cases using Jest and Enzyme for the react application and when try to run test cases using jest like "test": "jest --watch" rather "test": "react-scripts test" tests going through babel for the runner to understand react syntax. And have been doing setup step by step using babel but this error Support for the experimental syntax 'jsx' isn't currently enabled stopping me to go further. And as suggested in some threads I have been trying with npm install --save

Babel throwing Support for the experimental syntax 'jsx' isn't currently enabled

[亡魂溺海] 提交于 2021-01-26 03:13:08
问题 I started newly writing unit test cases using Jest and Enzyme for the react application and when try to run test cases using jest like "test": "jest --watch" rather "test": "react-scripts test" tests going through babel for the runner to understand react syntax. And have been doing setup step by step using babel but this error Support for the experimental syntax 'jsx' isn't currently enabled stopping me to go further. And as suggested in some threads I have been trying with npm install --save

React’s new context api with enzyme

余生颓废 提交于 2021-01-20 19:28:27
问题 I have been using enzyme and love it a lot. It works with react 16 until I wanted to test my new project that uses react’s new context api. If I render only my basic component using shallow and console log the debug of the component I can see its content but when I use the new context api with provider and consumer, I get <undefined /> as a render out. Enzyme does not render the component but react does. Can someone please provide some guidance. Thank you. 回答1: Support for this and other

React’s new context api with enzyme

拈花ヽ惹草 提交于 2021-01-20 19:26:27
问题 I have been using enzyme and love it a lot. It works with react 16 until I wanted to test my new project that uses react’s new context api. If I render only my basic component using shallow and console log the debug of the component I can see its content but when I use the new context api with provider and consumer, I get <undefined /> as a render out. Enzyme does not render the component but react does. Can someone please provide some guidance. Thank you. 回答1: Support for this and other

React’s new context api with enzyme

狂风中的少年 提交于 2021-01-20 19:23:27
问题 I have been using enzyme and love it a lot. It works with react 16 until I wanted to test my new project that uses react’s new context api. If I render only my basic component using shallow and console log the debug of the component I can see its content but when I use the new context api with provider and consumer, I get <undefined /> as a render out. Enzyme does not render the component but react does. Can someone please provide some guidance. Thank you. 回答1: Support for this and other

React’s new context api with enzyme

删除回忆录丶 提交于 2021-01-20 19:21:56
问题 I have been using enzyme and love it a lot. It works with react 16 until I wanted to test my new project that uses react’s new context api. If I render only my basic component using shallow and console log the debug of the component I can see its content but when I use the new context api with provider and consumer, I get <undefined /> as a render out. Enzyme does not render the component but react does. Can someone please provide some guidance. Thank you. 回答1: Support for this and other