SlideShare a Scribd company logo
1 of 23
Download to read offline
How to go about testing in React?
Lisa Gagarina
@lisa_gagarina
lisagagarina17@gmail.com
JPMorgan Chase & Co.
JavaScript Matters
London, 11th
March 2019
My talk
Is about sharing our experience and
raising awareness around testing
types and tools
My talk
Is NOT about giving you a magical
solution or convincing you to use a
particular tool
Is about sharing our experience and
raising awareness around testing
types and tools
Project: Notifications Distribution Service
Notifications
Distribution Service
Financial Markets Events
Users
trigger
SMS, Voice, Email, Mobile,
real-time messages
View notifications feed,
manage subscriptions
Technology stacks comparison
Old frontend app New frontend app
JS Framework, state
management
AngularJS (1.x) React, Redux
Task runner / module bundler Grunt Webpack
Static code analysis JSHint ESLint, Prettier
Styling CSS CSS-in-JS
IDE / Code Editor Intellij VSCode
Testing Karma, Jasmine
Protractor, Selenium
Jest, Enzyme
Why do we write tests?
● To make sure everything works as expected
● To write better code
● To automate manual testing
● To protect code from breakage
● To provide documentation
Why [frontend] testing is so hard?
● The closest to end users
● You can test a lot
● You can use a lot of tools
● It is hard to define a testing strategy
Jest
Is great:
- Widely used, well documented and
supported
- Jasmine-like
- Built-in assertions, spies, coverage etc.
- Minimum configuration
- Fast
Jest
Is great:
- Widely used, well documented and
supported
- Jasmine-like
- Built-in assertions, spies, coverage etc.
- Minimum configuration
- Fast
And challenging:
- Mocking
- Snapshots
Demo:
snapshot-tests/Notification.test.js
Jest snapshot tests: red flags
● Too many, too big
● False negative failures
● Hard to PR
● Unclear intention
● False impression of good coverage
● Located separately from tested code
Jest snapshot tests: advices
● Limit usage to small static components
● Delete all useless snapshots
● Limit snapshot size (use ESLint jest/no-large-snapshots)
● Take advantage of inline snapshots
Demo: LikeButton.test.js
● Consider using diff snapshots
Demo: NotificationHeader.test.js
● Consider using beyond React components
Enzyme unit tests: concerns
● Different types of rendering: shallow, mount, static
● Shallow rendering:
○ tests are coupled to implementation
○ tests fail on refactoring
○ tests pass but component is broken
● Lack of confidence
Demo: Notification.enzyme.test.js
React Testing Library (by Kent Dodds)
What?
● Avoid testing implementation
details
● Focus on how users use your
application
● Allow for refactoring without
breaking tests
● Increase confidence in your tests
React Testing Library (by Kent Dodds)
What?
● Avoid testing implementation
details
● Focus on how users use your
application
● Allow for refactoring without
breaking tests
● Increase confidence in your tests
How?
● No shallow rendering
● Always renders your components in
the DOM (jsdom)
● No way to interact with component
instances, props, state etc.
● Queries the DOM how real user
would
React Testing Library (by Kent Dodds)
Demo:
1. Unit tests:
Notification.test.js compared with Notification.enzyme.test.js, NotificationHeader.test.js
2. Integration test: App.test.js
Testing trophy by Kent Dodds
1. Static tests
ESLint, TypeScript, Flow
2. Unit tests
Jest, React Testing Library
3. Integration tests
Jest, React Testing Library, Cypress
4. End to End tests
Cypress
Cypress
● Bundles multiple tools
● Runs inside a browser
● Gives you full control over your application
● Eliminates flaky tests
● Provides debuggability, rich UI, time travel
Demo: cypress/integration/notification.spec.js
Takeaways
1. Focus on your users
Takeaways
1. Focus on your users
2. Write tests that give you confidence
Takeaways
1. Focus on your users
2. Write tests that give you confidence
3. Take full advantage of static tests
Takeaways
1. Focus on your users
2. Write tests that give you confidence
3. Take full advantage of static tests
4. Define test strategy that works for your project and your team
Links
● Demo from this presentation
● Write tests. Not too many. Mostly integration.
● Why I Never Use Shallow Rendering
● Testing JavaScript
● Cypress vs. Selenium, is this the end of an era?
Thank you!

More Related Content

What's hot

Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in AngularKnoldus Inc.
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react formYao Nien Chung
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with JestMichał Pierzchała
 
Intro to react native
Intro to react nativeIntro to react native
Intro to react nativeModusJesus
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Fabio Biondi
 
BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumberDaniel Kummer
 
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...GreeceJS
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
Meetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React TestingMeetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React TestingAugusto Lazaro
 
Selenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And AnswersSelenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And AnswersAjit Jadhav
 

What's hot (20)

BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
Cucumber & gherkin language
Cucumber & gherkin languageCucumber & gherkin language
Cucumber & gherkin language
 
Automated Test Framework with Cucumber
Automated Test Framework with CucumberAutomated Test Framework with Cucumber
Automated Test Framework with Cucumber
 
Intro to react native
Intro to react nativeIntro to react native
Intro to react native
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
Cucumber ppt
Cucumber pptCucumber ppt
Cucumber ppt
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022
 
BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumber
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Meetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React TestingMeetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React Testing
 
Selenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And AnswersSelenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And Answers
 
React and redux
React and reduxReact and redux
React and redux
 
Jenkins tutorial
Jenkins tutorialJenkins tutorial
Jenkins tutorial
 

Similar to How to go about testing in React?

Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...DicodingEvent
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by ExampleNalin Goonawardana
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUGGatling - Bordeaux JUG
Gatling - Bordeaux JUGslandelle
 
xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012Justin Gordon
 
Optimizing Your Agile Testing Processes
Optimizing Your Agile Testing ProcessesOptimizing Your Agile Testing Processes
Optimizing Your Agile Testing ProcessesStanton Champion
 
manas expre-01
manas expre-01manas expre-01
manas expre-01Manas Rout
 
Agile Testing 20021015
Agile Testing 20021015Agile Testing 20021015
Agile Testing 20021015Raghu Karnati
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testingmahmoud ramadan
 
Selenium_Automation
Selenium_AutomationSelenium_Automation
Selenium_Automationmadhu g
 
Web performance
Web  performance Web  performance
Web performance Major Ye
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
Katalon Studio - Successful Test Automation for both Testers and Developers
Katalon Studio - Successful Test Automation for both Testers and DevelopersKatalon Studio - Successful Test Automation for both Testers and Developers
Katalon Studio - Successful Test Automation for both Testers and DevelopersKatalon Studio
 

Similar to How to go about testing in React? (20)

Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
 
Shweta_Saini_Resume
Shweta_Saini_ResumeShweta_Saini_Resume
Shweta_Saini_Resume
 
NidhiVasudev_Resume
NidhiVasudev_ResumeNidhiVasudev_Resume
NidhiVasudev_Resume
 
sanjayresume
sanjayresumesanjayresume
sanjayresume
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by Example
 
Unit tests benefits
Unit tests benefitsUnit tests benefits
Unit tests benefits
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUGGatling - Bordeaux JUG
Gatling - Bordeaux JUG
 
xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012
 
Resume
Resume Resume
Resume
 
Optimizing Your Agile Testing Processes
Optimizing Your Agile Testing ProcessesOptimizing Your Agile Testing Processes
Optimizing Your Agile Testing Processes
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
manas expre-01
manas expre-01manas expre-01
manas expre-01
 
Sunitha_CV
Sunitha_CVSunitha_CV
Sunitha_CV
 
Munmi_QA engineer _cv
Munmi_QA engineer _cvMunmi_QA engineer _cv
Munmi_QA engineer _cv
 
Agile Testing 20021015
Agile Testing 20021015Agile Testing 20021015
Agile Testing 20021015
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testing
 
Selenium_Automation
Selenium_AutomationSelenium_Automation
Selenium_Automation
 
Web performance
Web  performance Web  performance
Web performance
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Katalon Studio - Successful Test Automation for both Testers and Developers
Katalon Studio - Successful Test Automation for both Testers and DevelopersKatalon Studio - Successful Test Automation for both Testers and Developers
Katalon Studio - Successful Test Automation for both Testers and Developers
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 

How to go about testing in React?

  • 1. How to go about testing in React? Lisa Gagarina @lisa_gagarina lisagagarina17@gmail.com JPMorgan Chase & Co. JavaScript Matters London, 11th March 2019
  • 2. My talk Is about sharing our experience and raising awareness around testing types and tools
  • 3. My talk Is NOT about giving you a magical solution or convincing you to use a particular tool Is about sharing our experience and raising awareness around testing types and tools
  • 4. Project: Notifications Distribution Service Notifications Distribution Service Financial Markets Events Users trigger SMS, Voice, Email, Mobile, real-time messages View notifications feed, manage subscriptions
  • 5. Technology stacks comparison Old frontend app New frontend app JS Framework, state management AngularJS (1.x) React, Redux Task runner / module bundler Grunt Webpack Static code analysis JSHint ESLint, Prettier Styling CSS CSS-in-JS IDE / Code Editor Intellij VSCode Testing Karma, Jasmine Protractor, Selenium Jest, Enzyme
  • 6. Why do we write tests? ● To make sure everything works as expected ● To write better code ● To automate manual testing ● To protect code from breakage ● To provide documentation
  • 7. Why [frontend] testing is so hard? ● The closest to end users ● You can test a lot ● You can use a lot of tools ● It is hard to define a testing strategy
  • 8. Jest Is great: - Widely used, well documented and supported - Jasmine-like - Built-in assertions, spies, coverage etc. - Minimum configuration - Fast
  • 9. Jest Is great: - Widely used, well documented and supported - Jasmine-like - Built-in assertions, spies, coverage etc. - Minimum configuration - Fast And challenging: - Mocking - Snapshots Demo: snapshot-tests/Notification.test.js
  • 10. Jest snapshot tests: red flags ● Too many, too big ● False negative failures ● Hard to PR ● Unclear intention ● False impression of good coverage ● Located separately from tested code
  • 11. Jest snapshot tests: advices ● Limit usage to small static components ● Delete all useless snapshots ● Limit snapshot size (use ESLint jest/no-large-snapshots) ● Take advantage of inline snapshots Demo: LikeButton.test.js ● Consider using diff snapshots Demo: NotificationHeader.test.js ● Consider using beyond React components
  • 12. Enzyme unit tests: concerns ● Different types of rendering: shallow, mount, static ● Shallow rendering: ○ tests are coupled to implementation ○ tests fail on refactoring ○ tests pass but component is broken ● Lack of confidence Demo: Notification.enzyme.test.js
  • 13. React Testing Library (by Kent Dodds) What? ● Avoid testing implementation details ● Focus on how users use your application ● Allow for refactoring without breaking tests ● Increase confidence in your tests
  • 14. React Testing Library (by Kent Dodds) What? ● Avoid testing implementation details ● Focus on how users use your application ● Allow for refactoring without breaking tests ● Increase confidence in your tests How? ● No shallow rendering ● Always renders your components in the DOM (jsdom) ● No way to interact with component instances, props, state etc. ● Queries the DOM how real user would
  • 15. React Testing Library (by Kent Dodds) Demo: 1. Unit tests: Notification.test.js compared with Notification.enzyme.test.js, NotificationHeader.test.js 2. Integration test: App.test.js
  • 16. Testing trophy by Kent Dodds 1. Static tests ESLint, TypeScript, Flow 2. Unit tests Jest, React Testing Library 3. Integration tests Jest, React Testing Library, Cypress 4. End to End tests Cypress
  • 17. Cypress ● Bundles multiple tools ● Runs inside a browser ● Gives you full control over your application ● Eliminates flaky tests ● Provides debuggability, rich UI, time travel Demo: cypress/integration/notification.spec.js
  • 18. Takeaways 1. Focus on your users
  • 19. Takeaways 1. Focus on your users 2. Write tests that give you confidence
  • 20. Takeaways 1. Focus on your users 2. Write tests that give you confidence 3. Take full advantage of static tests
  • 21. Takeaways 1. Focus on your users 2. Write tests that give you confidence 3. Take full advantage of static tests 4. Define test strategy that works for your project and your team
  • 22. Links ● Demo from this presentation ● Write tests. Not too many. Mostly integration. ● Why I Never Use Shallow Rendering ● Testing JavaScript ● Cypress vs. Selenium, is this the end of an era?