SlideShare a Scribd company logo
React Testing
ES6 = ES2015
Agenda
1. Static code analysis
2. Jest
Simon Bächler
Web developer at Feinheit
Testing
What tool do you use to test
your JavaScript?
http://ashleynolan.co.uk/blog/frontend-tooling-survey-2015-results
Static code analysis
Style checking and linting
Pluggable linting rules
Supports ES2015
Supports JSX
Autofix option
ESLint
var foo = bar;
Testing React Applications
eslint —init
Test Framework
Built on top of Jasmine
Developed by Facebook
Supports React out of the box
Mock by default
fake DOM
0.12
No support for Node.js 0.12
Jest >= 0.5.2 supports Node 4
Behavior driven
jest.dontMock(‚../sum');
describe('sum', function() {
it('adds 1 + 2 to equal 3', function() {
var sum = require(‚../sum');
expect(sum(1, 2)).toBe(3);
});
});
Component under test
Mock object JSON fixture
State
Dependency
Mocks
Dependency
(Default Mock)
dependency.foo(bar)
undefined
Component under
test
Jest
expect(dependency.foo)
.toBeCalled()
expect(dependency.foo)
.lastCalledWith(bar)
expect(dependency.foo)
.not.toBeCalledWith(baz)
doSomething(bar)
Let’s look at some
code
Jest runs in Node.js
• No window object, no LocalStorage
• No debugging (there is a workaround)
• 'npm test'
• node-debug --nodejs --harmony
node_modules/.bin/jest --runInBand
More Jest functionality
• Test asynchronous calls
• Spy on non-mocked objects and methods
• Timer mocks
• Regex matcher
• Partial matchers
• Setup and teardown methods
System Tests
Selenium Web Driver
Remote control a Browser
Supports Chrome, Firefox, IE,
Edge and PhantomJS
Also iOs and Android (with
Appium)
But this is another talk.
–Jacob Kaplan-Moss
„Code without tests is broken by design.“
END OF PART I

More Related Content

PDF
Isomorphic React Apps Testing
PDF
Unit-testing and E2E testing in JS
PPTX
Angular Unit Testing
PPTX
Angular Unit Testing
PDF
Javascript tdd byandreapaciolla
PDF
Client side unit tests - using jasmine & karma
PPT
Testing in AngularJS
PDF
AngularJS Unit Test
Isomorphic React Apps Testing
Unit-testing and E2E testing in JS
Angular Unit Testing
Angular Unit Testing
Javascript tdd byandreapaciolla
Client side unit tests - using jasmine & karma
Testing in AngularJS
AngularJS Unit Test

What's hot (20)

PPTX
Test-Driven JavaScript Development (JavaZone 2010)
PDF
Painless JavaScript Testing with Jest
PPTX
Protractor Tutorial Quality in Agile 2015
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
PDF
Angular Application Testing
PDF
Efficient JavaScript Unit Testing, May 2012
PPTX
Unit testing JavaScript: Jasmine & karma intro
PDF
Angular testing
PDF
Intro to testing Javascript with jasmine
PPT
Unit Testing in iOS
PDF
Automated Web Testing using JavaScript
PPT
Unit Testing in iOS - Ninjava Talk
PDF
Unit Testing your React / Redux app (@BucharestJS)
PDF
Angularjs - Unit testing introduction
PDF
Angular Unit Testing from the Trenches
PDF
AngularJS Unit Testing w/Karma and Jasmine
PDF
Intro to Unit Testing in AngularJS
ODP
Unit Testing and Coverage for AngularJS
PDF
Angular Unit Testing NDC Minn 2018
KEY
iOS Unit Testing
Test-Driven JavaScript Development (JavaZone 2010)
Painless JavaScript Testing with Jest
Protractor Tutorial Quality in Agile 2015
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Angular Application Testing
Efficient JavaScript Unit Testing, May 2012
Unit testing JavaScript: Jasmine & karma intro
Angular testing
Intro to testing Javascript with jasmine
Unit Testing in iOS
Automated Web Testing using JavaScript
Unit Testing in iOS - Ninjava Talk
Unit Testing your React / Redux app (@BucharestJS)
Angularjs - Unit testing introduction
Angular Unit Testing from the Trenches
AngularJS Unit Testing w/Karma and Jasmine
Intro to Unit Testing in AngularJS
Unit Testing and Coverage for AngularJS
Angular Unit Testing NDC Minn 2018
iOS Unit Testing
Ad

Viewers also liked (14)

PDF
War of Attrition: AWS vs. Google, IBM and Microsoft Azure
PPTX
Java script performance tips
PPTX
Testing of React JS app
PPTX
Modular development with redux
PDF
Workshop 23: ReactJS, React & Redux testing
PDF
Infographic Trends in Treasury 2017
PDF
Understanding JavaScript Testing
PDF
Unit testing JavaScript using Mocha and Node
PDF
The Many Ways to Test Your React App
PDF
React & Redux
PDF
RxJS + Redux + React = Amazing
PPTX
Refactoring (TDD Mini Serie) Haarlem Software Developer Meetup
PPTX
Better React state management with Redux
PDF
Introduction to Redux
War of Attrition: AWS vs. Google, IBM and Microsoft Azure
Java script performance tips
Testing of React JS app
Modular development with redux
Workshop 23: ReactJS, React & Redux testing
Infographic Trends in Treasury 2017
Understanding JavaScript Testing
Unit testing JavaScript using Mocha and Node
The Many Ways to Test Your React App
React & Redux
RxJS + Redux + React = Amazing
Refactoring (TDD Mini Serie) Haarlem Software Developer Meetup
Better React state management with Redux
Introduction to Redux
Ad

Similar to Testing React Applications (20)

PPT
Test strategy for web development
PDF
UI Testing Best Practices - An Expected Journey
KEY
Javascript unit testing, yes we can e big
PDF
ForwardJS 2017 - Fullstack end-to-end Test Automation with node.js
PDF
Json generation
PDF
軟體測試是在測試什麼?
PPT
Js unit testing
PPTX
Java script unit testing
PPTX
Full Stack Unit Testing
PPTX
Automation - web testing with selenium
PPTX
Security testing of YUI powered applications
PPT
JSF and Seam
PPTX
Adding a modern twist to legacy web applications
ODP
Grails unit testing
PDF
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Devoxx UK 2016
PPTX
QA Fest 2014. Ярослав Пернеровский. Appium - два в одном. рецепт приготовлени...
PPT
Unit testing with Spock Framework
PPTX
Wire Mock API, implementation using JAVA wiremock_workshop.pptx
PDF
MeetJS Summit 2016: React.js enlightenment
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
Test strategy for web development
UI Testing Best Practices - An Expected Journey
Javascript unit testing, yes we can e big
ForwardJS 2017 - Fullstack end-to-end Test Automation with node.js
Json generation
軟體測試是在測試什麼?
Js unit testing
Java script unit testing
Full Stack Unit Testing
Automation - web testing with selenium
Security testing of YUI powered applications
JSF and Seam
Adding a modern twist to legacy web applications
Grails unit testing
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Devoxx UK 2016
QA Fest 2014. Ярослав Пернеровский. Appium - два в одном. рецепт приготовлени...
Unit testing with Spock Framework
Wire Mock API, implementation using JAVA wiremock_workshop.pptx
MeetJS Summit 2016: React.js enlightenment
We Are All Testers Now: The Testing Pyramid and Front-End Development

More from stbaechler (8)

PPTX
Distributed apps
PPTX
Immutable Libraries for React
PPTX
Javascript Bundling and modularization
PPTX
User stories schreiben
PPTX
Nested sets
PPTX
Microformats
PPTX
Zeitplanung mit PERT
PPTX
Bower Paketmanager
Distributed apps
Immutable Libraries for React
Javascript Bundling and modularization
User stories schreiben
Nested sets
Microformats
Zeitplanung mit PERT
Bower Paketmanager

Recently uploaded (20)

PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPT
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PDF
Sims 4 Historia para lo sims 4 para jugar
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
DOCX
Unit-3 cyber security network security of internet system
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PPTX
Internet___Basics___Styled_ presentation
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPT
tcp ip networks nd ip layering assotred slides
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
introduction about ICD -10 & ICD-11 ppt.pptx
WebRTC in SignalWire - troubleshooting media negotiation
SASE Traffic Flow - ZTNA Connector-1.pdf
Tenda Login Guide: Access Your Router in 5 Easy Steps
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
Decoding a Decade: 10 Years of Applied CTI Discipline
Sims 4 Historia para lo sims 4 para jugar
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Unit-1 introduction to cyber security discuss about how to secure a system
Unit-3 cyber security network security of internet system
Power Point - Lesson 3_2.pptx grad school presentation
artificialintelligenceai1-copy-210604123353.pptx
Introuction about WHO-FIC in ICD-10.pptx
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Internet___Basics___Styled_ presentation
PptxGenJS_Demo_Chart_20250317130215833.pptx
Job_Card_System_Styled_lorem_ipsum_.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
tcp ip networks nd ip layering assotred slides
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...

Testing React Applications

Editor's Notes

  • #3: ECMA Script 6 is now called ECMA Script 2015
  • #4: This talk has two parts Part 2 is much shorter.
  • #5: This is an actual portrait of me, drawn by the very talented Tika.
  • #6: Recent survey (August/September 2015)
  • #7: 60% don't use a test framework. 1% uses Jest (8% use React, so only ⅛ of all React users uses Jest).
  • #8: Like jslint / jshint but uses a plugin architecture. Checks for implicitly declared global variables or missing semicolons but also code style. If you don't have it installed, do it tonight. Can be added to the build process or as webpack-loader. Most editors or IDEs have a plugin for live code checking. Feinheit has a git pre-commit hook that runs it. Has an autofixing options. Used by Node and Webkit to validate their Javascript.
  • #9: Lint example: A file like this produces a linting result like that. The numbers are the line and colon numbers of the error. Error code is 1, so a build process stops if there is an error.
  • #10: Jetbrains IDE support ESLint (and the other linters). There is also a plugin for SublimeLinter
  • #11: Runs an assistant that creates your configuration.
  • #12: Jest is the framework of choice for React unit- and functional tests. Mocha is possible, but more difficult to set up.
  • #13: Node 0.10 is ok. io.js or Node.js 4.0 + are ok, too. Several generators have Jest 0.4 in their package.json which fails to install on Node V4.
  • #14: More like natural language than JUnit syntax. Similar to what Angular.js uses. describe part: like a JUnit test class it part: like a JUnit test method expect function: like the JUnit assertEqual() method. Everything is a function. Path resolve: does not resolve jsx files. Those need to be explicit.
  • #15: Test components as independent entities. This is a big advantage of using components. Inject all required properties from outside. Do not create props from within the module, such as reading a value from a DOM element. This creates a dependency on the DOM and possibly jQuery. Explicitly don't mock the component under test, React and Underscore.js.
  • #16: Jest calls the method doSomething with the argument bar. We want to test if it calls the method foo on the dependency. The component is supposed to call the method 'foo' with argument 'bar' on the dependency. In Jest you can test if the method has been called with the correct arguments. The return value of the mocked function can be specified of course.
  • #17: http://chler.com/cafe/ https://github.com/sbaechler/cafe-international/blob/master/Content/__tests__/beverage-test.js
  • #18: You can access rendered DOM nodes for the React components. Pass a reference to LocalStorage to your component. Don’t access it directly. https://github.com/facebook/jest/issues/152 node-debug opens a developer-tools window and connects to the node process. Tests with debugging are slower because they cannot run in parallel.
  • #19: With a spy it is possible to check if a method on a concrete object has been called and what the parameters were.
  • #20: Jest is well suited for unit and functional tests.
  • #21: Quote: Co-Creator of Django, Director of Security at Heroku.