Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I feel like most component testing with jest and enzyme are valueless, am I wrong?

I’m new to tests with React-Jest-Enzyme, but from all the info I collected about it it seems to me that most of the tests actually tests if the React library breaks, and not my actual business logic.

I’ll give you some examples, and please correct me if I’m wrong:

Snapshot testing:

What’s the deal with this strategy?

From what I see it’s main purpose is to catch any unwanted changes to my code. it “stringify” my component tree, and just noticed if any new line breaks / characters were added, right?

so its mostly being used for those cases I could accidently pressed my keyboard? or someone else accidently mess with my code?

Enzyme’s mount/shallow and Jest’s

Most of the examples I saw explaining the way you use those are something like this:

const wrapper = mount(<MyComponeny />)
expect(wrapper.find(‘button’).simulate(‘click)).toHaveBeenCalledTime(1)

What value do I get out of it? If I simulate a button click with enzyme’s simulate(‘click’), then i should expect it would trigger a click event.

what am I testing here exactly? Enzyme’s functionality?

also the setState method enzyme gives us. if wrapper.setState({value: ‘some value’)} suppose to change my state, why do I see use cases like this:

wrapper.setState({value: ‘new value’)}
expect(wrapper.state(‘value’)).toBe(‘new value’)

?

why do i need to test the testing framework / extra libraries?

it’s all seems a little bit ambiguous

like image 223
ueeieiie Avatar asked Feb 21 '18 10:02

ueeieiie


People also ask

Should I use Enzyme with Jest?

Many people choose to use Jest and Enzyme together to test their React web applications. They use Jest as a test runner and assertion library, then use Enzyme to build the tests for their UI. This results in slimmer, cleaner testing code that's also easier to debug when a test breaks.

What type of testing does Enzyme allow us to perform?

Enzyme is a JavaScript testing utility for easily testing React components. It helps render React components in testing mode.

What is Enzyme Jest testing?

Jest and Enzyme are tools which are used in tandem to test React components, and are used in the C#Bot testing framework for unit tests of client-side components. While they are used in this context to test react components, Jest is not specific to React, and can be used in other JavaScript applications.

How do you test if else condition in Jest Enzyme?

so to test this function you need to provide some input and expect an output. So you could write a test like this: describe('extractInfo', () => { test('test 1', () => { //inputs const Info = ''; const description = ''; const jestinformation = ''; //test const result = extractInfo(Info); //expect expect(result).


1 Answers

Snapshot testing:

so its mostly being used for those cases I could accidently pressed my keyboard? or someone else accidently mess with my code?

If you tweak a common component/service/utility and don't notice it affects some unexpected component for example.

Now it can affect it in a good way, for example fix an unexpected text in a component- but snapshots gives you the power to quickly see the changes across all affected components.

const wrapper = mount(<MyComponeny />)
expect(wrapper.find(‘button’).simulate(‘click)).toHaveBeenCalledTime(1)

What value do I get out of it?

This is just a simple example. It would of been a really bad test indeed- it doesn't tests anything. Usually you test more important stuff like:

toHaveBeenCalledTime(1) on some sort of process- for example, making sure a network request was only done once during an entire flow of clicks and other triggers.

why do I see use cases like this:

wrapper.setState({value: ‘new value’)}
expect(wrapper.state(‘value’)).toBe(‘new value’)

?

This is also a simple example to show you that you can set state on a React component. It doesn't actually tests anything.

What you can do is to set state on a component and make sure it renders the right amount of children or that it renders some other things you expect.

And this also has to do with snapshots-

Set a certain state on a component and make a snapshot, then, when you work on services and utilities this component uses, you can make sure it doesn't breaks for that certain state.

like image 85
Vitali Zaidman Avatar answered Oct 06 '22 05:10

Vitali Zaidman