SlideShare a Scribd company logo
Saving Time By
Testing With Jest
Ben McCormick
Windsor Circle
Twitter: @ben336
Blog: http://benmccormick.org
Who Am I?
• Senior Software Engineer @ Windsor Circle
• Live in Durham NC
• Jest user since early 2016
• Blogger @ benmccormick.org
What To Expect
• What is Jest?
• What problems does it try to solve?
• How can it save me time?
• Demo
• Q & A
So What Is Jest?
• Developed by Facebook for internal testing needs
• Open Sourced in 2014
• Mostly stagnant for 2 years
• Revived in late 2015 / early 2016
JavaScript Testing Challenges
• Many Environments
• Async Code
• User Interface code is hard to test
JavaScript Testing Challenges
• People don’t write tests
Prior Art: Who has been facing these
problems?
• Test Runners: Mocha, Jasmine, Karma, Tape, AVA,
QUnit
• Test libraries: Chai, Sinon, should.js, expect.js,
Istanbul
How Does Jest Fit In?
• Jest saves you time
• Jest is visual
• Jest is reliable
• Jest (now) has a good learning curve
How Does Jest Fit In?
• Jest does not test every environment
• Jest is not simple
• Jest is still growing and changing
How Does Jest Save You Time?
• Setting Up Tests: Simple configuration
• Writing Tests: Simple UI tests, familiar syntax
• Running Tests: Fast startup, fast test runs, smart
watch mode
• Fixing Tests: Clear error and failure messages
Setup Fast
• 1 Dependency for Test Runner, assertion library, code
coverage
• No extra configuration for Babel support (just 1
dependency)
• Sane defaults (run anything in a __tests__ directory)
• Codemods to transition from other libraries
Setup Fast: One Dependency
Karma + Jasmine (Vue.js) Jest
*Not necessarily optimized
Setup Fast: Minimal Project
Setup Fast: Minimal Project
Setup Fast: Sane Defaults
Setup Fast: Easy Babel Support
Quick Poll:
• Mocha
• Jasmine
• Chai
• AVA
• expect.js
• Should.js
• Tape
• Proxyquire
Setup Fast: Codemods
https://www.npmjs.com/package/jest-codemods
Write Tests Fast
• Familiar syntax: Jasmine or `test()` style
• Great mocking + assertion APIS
• Snapshot Tests!
Write Tests Fast: Syntax
Write Tests Fast: APIs
Write Tests Fast: Extensible APIs
Write Tests Fast: Snapshot Tests
• expect(<json>).toMatchSnapshot()
• Captures the current state of the code
• Future runs compare the state to the previous state of
the code
• Has special handling for React components by default
• Can write serializers for other type of data
Saving Time by Testing with Jest
We updated the component to add a #test as the default
Saving Time by Testing with Jest
`jest -u` updates snapshots to the new state
Write Tests Fast: Snapshot Tests
• Pro: Quick To Write and update
• Pro: Catch side effects
• Con: Don’t enforce correctness/communicate
developer intent
• Con: Require a strong process backing them up
• Good for: UI tests, runs against large amounts of real
world data
Run Tests Fast
• Tests Run in Node, no browser overhead
• Tests Run in Parallel
• Smart Testing - only run the tests that changed
• Watch Mode
Run Tests Fast: Smart Tests
• Jest is aware of modules
dependency Graph
• Runs tests when any
dependency of the test (or the
test itself changes)
• Git based
• Don’t run unnecessary tests!
Run Tests Fast: Watch Mode
• Watch mode is a cli that
watches for file changes
• Can control which tests are
watched
Fix Tests Fast
• Failed Tests Run First
• Accurate error reporting by line
• Editor Integration (VS Code)
DEMO
The Future?
• Multi Language Support
• More environment Support?
• More Editor Integrations ?
Repo: https://github.com/facebook/jest
Want more detail?
Questions?
Ben McCormick
Windsor Circle
Twitter: @ben336
Blog: http://benmccormick.org

More Related Content

PPTX
Saving Time By Testing With Jest
PPT
Next generation frontend tooling
PDF
The Many Ways to Test Your React App
PDF
MEAN Stack WeNode Barcelona Workshop
PDF
TDD a REST API With Node.js and MongoDB
PDF
Conquering AngularJS Limitations
PPTX
Олександр Хотемський “ProtractorJS як інструмент браузерної автоматизації для...
PDF
Unit Testing in JavaScript
Saving Time By Testing With Jest
Next generation frontend tooling
The Many Ways to Test Your React App
MEAN Stack WeNode Barcelona Workshop
TDD a REST API With Node.js and MongoDB
Conquering AngularJS Limitations
Олександр Хотемський “ProtractorJS як інструмент браузерної автоматизації для...
Unit Testing in JavaScript

What's hot (20)

PDF
MEAN Stack Workshop at Node Philly, 4/9/14
PPTX
Integration Testing with Selenium
PDF
Lessons in Open Source from the MongooseJS ODM
PDF
Modern javascript
PDF
MongoDB MEAN Stack Webinar October 7, 2015
PDF
A Day of REST
PDF
eMusic: WordPress in the Enterprise
PDF
Michael North "Ember.js 2 - Future-friendly ambitious apps, that scale!"
PPT
Jasmine - A BDD test framework for JavaScript
PDF
Hello world - intro to node js
PDF
WordPress: Getting Under the Hood
PPTX
10 tips to make your ASP.NET Apps Faster
PPTX
Getting Started with ASP.NET 5
PDF
Live Coverage at The New York Times
PPTX
Hybrid Mobile Development with Apache Cordova and
PDF
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
PPTX
C# Async/Await Explained
PDF
Capybara testing
PDF
ITT Flisol 2013
PPTX
How NOT to get lost in the current JavaScript landscape
MEAN Stack Workshop at Node Philly, 4/9/14
Integration Testing with Selenium
Lessons in Open Source from the MongooseJS ODM
Modern javascript
MongoDB MEAN Stack Webinar October 7, 2015
A Day of REST
eMusic: WordPress in the Enterprise
Michael North "Ember.js 2 - Future-friendly ambitious apps, that scale!"
Jasmine - A BDD test framework for JavaScript
Hello world - intro to node js
WordPress: Getting Under the Hood
10 tips to make your ASP.NET Apps Faster
Getting Started with ASP.NET 5
Live Coverage at The New York Times
Hybrid Mobile Development with Apache Cordova and
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
C# Async/Await Explained
Capybara testing
ITT Flisol 2013
How NOT to get lost in the current JavaScript landscape
Ad

Similar to Saving Time by Testing with Jest (20)

PDF
Jest: Frontend Testing richtig gemacht @WebworkerNRW
PDF
Jest: Frontend Testing leicht gemacht @EnterJS2018
PDF
Das Frontend richtig Testen – mit Jest @Developer Week 2018
PDF
Painless JavaScript Testing with Jest
PDF
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
PDF
Front end testing you won't hate
PDF
Frontend automation and stability
PPTX
Testing React Applications
PDF
Introduction to jest
PPTX
JUTE Workshop
PPTX
How to write test in node.js
PDF
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
PDF
An Introduction to the World of Testing for Front-End Developers
PPTX
Testing nodejs apps
PPTX
Automated Testing using JavaScript
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PDF
Node.js Development Workflow Automation with Grunt.js
PPTX
unit test in node js - test cases in node
PPTX
Zero to Testing in JavaScript
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing leicht gemacht @EnterJS2018
Das Frontend richtig Testen – mit Jest @Developer Week 2018
Painless JavaScript Testing with Jest
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
Front end testing you won't hate
Frontend automation and stability
Testing React Applications
Introduction to jest
JUTE Workshop
How to write test in node.js
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
An Introduction to the World of Testing for Front-End Developers
Testing nodejs apps
Automated Testing using JavaScript
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Node.js Development Workflow Automation with Grunt.js
unit test in node js - test cases in node
Zero to Testing in JavaScript
Ad

More from All Things Open (20)

PDF
Tech Hiring Is Not Dead - You Just Actually Have To Try
PDF
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
PPTX
Big Data on a Small Budget: Scalable Data Visualization for the Rest of Us - ...
PDF
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
PDF
Let's Create a GitHub Copilot Extension! - Nick Taylor, Pomerium
PDF
Leveraging Pre-Trained Transformer Models for Protein Function Prediction - T...
PDF
Gen AI: AI Agents - Making LLMs work together in an organized way - Brent Las...
PDF
You Don't Need an AI Strategy, But You Do Need to Be Strategic About AI - Jes...
PPTX
DON’T PANIC: AI IS COMING – The Hitchhiker’s Guide to AI - Mark Hinkle, Perip...
PDF
Fine-Tuning Large Language Models with Declarative ML Orchestration - Shivay ...
PDF
Leveraging Knowledge Graphs for RAG: A Smarter Approach to Contextual AI Appl...
PPTX
Artificial Intelligence Needs Community Intelligence - Sriram Raghavan, IBM R...
PDF
Don't just talk to AI, do more with AI: how to improve productivity with AI a...
PPTX
Open-Source GenAI vs. Enterprise GenAI: Navigating the Future of AI Innovatio...
PDF
The Death of the Browser - Rachel-Lee Nabors, AgentQL
PDF
Making Operating System updates fast, easy, and safe
PDF
Reshaping the landscape of belonging to transform community
PDF
The Unseen, Underappreciated Security Work Your Maintainers May (or may not) ...
PDF
Integrating Diversity, Equity, and Inclusion into Product Design
PDF
The Open Source Ecosystem for eBPF in Kubernetes
Tech Hiring Is Not Dead - You Just Actually Have To Try
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
Big Data on a Small Budget: Scalable Data Visualization for the Rest of Us - ...
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
Let's Create a GitHub Copilot Extension! - Nick Taylor, Pomerium
Leveraging Pre-Trained Transformer Models for Protein Function Prediction - T...
Gen AI: AI Agents - Making LLMs work together in an organized way - Brent Las...
You Don't Need an AI Strategy, But You Do Need to Be Strategic About AI - Jes...
DON’T PANIC: AI IS COMING – The Hitchhiker’s Guide to AI - Mark Hinkle, Perip...
Fine-Tuning Large Language Models with Declarative ML Orchestration - Shivay ...
Leveraging Knowledge Graphs for RAG: A Smarter Approach to Contextual AI Appl...
Artificial Intelligence Needs Community Intelligence - Sriram Raghavan, IBM R...
Don't just talk to AI, do more with AI: how to improve productivity with AI a...
Open-Source GenAI vs. Enterprise GenAI: Navigating the Future of AI Innovatio...
The Death of the Browser - Rachel-Lee Nabors, AgentQL
Making Operating System updates fast, easy, and safe
Reshaping the landscape of belonging to transform community
The Unseen, Underappreciated Security Work Your Maintainers May (or may not) ...
Integrating Diversity, Equity, and Inclusion into Product Design
The Open Source Ecosystem for eBPF in Kubernetes

Recently uploaded (20)

PDF
WOOl fibre morphology and structure.pdf for textiles
PPT
Module 1.ppt Iot fundamentals and Architecture
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Architecture types and enterprise applications.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPTX
Modernising the Digital Integration Hub
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
WOOl fibre morphology and structure.pdf for textiles
Module 1.ppt Iot fundamentals and Architecture
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Hindi spoken digit analysis for native and non-native speakers
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Enhancing emotion recognition model for a student engagement use case through...
Web App vs Mobile App What Should You Build First.pdf
Architecture types and enterprise applications.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Programs and apps: productivity, graphics, security and other tools
DP Operators-handbook-extract for the Mautical Institute
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Modernising the Digital Integration Hub
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Getting started with AI Agents and Multi-Agent Systems
Group 1 Presentation -Planning and Decision Making .pptx
O2C Customer Invoices to Receipt V15A.pptx
Final SEM Unit 1 for mit wpu at pune .pptx

Saving Time by Testing with Jest

  • 1. Saving Time By Testing With Jest Ben McCormick Windsor Circle Twitter: @ben336 Blog: http://benmccormick.org
  • 2. Who Am I? • Senior Software Engineer @ Windsor Circle • Live in Durham NC • Jest user since early 2016 • Blogger @ benmccormick.org
  • 3. What To Expect • What is Jest? • What problems does it try to solve? • How can it save me time? • Demo • Q & A
  • 4. So What Is Jest? • Developed by Facebook for internal testing needs • Open Sourced in 2014 • Mostly stagnant for 2 years • Revived in late 2015 / early 2016
  • 5. JavaScript Testing Challenges • Many Environments • Async Code • User Interface code is hard to test
  • 6. JavaScript Testing Challenges • People don’t write tests
  • 7. Prior Art: Who has been facing these problems? • Test Runners: Mocha, Jasmine, Karma, Tape, AVA, QUnit • Test libraries: Chai, Sinon, should.js, expect.js, Istanbul
  • 8. How Does Jest Fit In? • Jest saves you time • Jest is visual • Jest is reliable • Jest (now) has a good learning curve
  • 9. How Does Jest Fit In? • Jest does not test every environment • Jest is not simple • Jest is still growing and changing
  • 10. How Does Jest Save You Time? • Setting Up Tests: Simple configuration • Writing Tests: Simple UI tests, familiar syntax • Running Tests: Fast startup, fast test runs, smart watch mode • Fixing Tests: Clear error and failure messages
  • 11. Setup Fast • 1 Dependency for Test Runner, assertion library, code coverage • No extra configuration for Babel support (just 1 dependency) • Sane defaults (run anything in a __tests__ directory) • Codemods to transition from other libraries
  • 12. Setup Fast: One Dependency Karma + Jasmine (Vue.js) Jest *Not necessarily optimized
  • 15. Setup Fast: Sane Defaults
  • 16. Setup Fast: Easy Babel Support
  • 17. Quick Poll: • Mocha • Jasmine • Chai • AVA • expect.js • Should.js • Tape • Proxyquire
  • 19. Write Tests Fast • Familiar syntax: Jasmine or `test()` style • Great mocking + assertion APIS • Snapshot Tests!
  • 22. Write Tests Fast: Extensible APIs
  • 23. Write Tests Fast: Snapshot Tests • expect(<json>).toMatchSnapshot() • Captures the current state of the code • Future runs compare the state to the previous state of the code • Has special handling for React components by default • Can write serializers for other type of data
  • 25. We updated the component to add a #test as the default
  • 27. `jest -u` updates snapshots to the new state
  • 28. Write Tests Fast: Snapshot Tests • Pro: Quick To Write and update • Pro: Catch side effects • Con: Don’t enforce correctness/communicate developer intent • Con: Require a strong process backing them up • Good for: UI tests, runs against large amounts of real world data
  • 29. Run Tests Fast • Tests Run in Node, no browser overhead • Tests Run in Parallel • Smart Testing - only run the tests that changed • Watch Mode
  • 30. Run Tests Fast: Smart Tests • Jest is aware of modules dependency Graph • Runs tests when any dependency of the test (or the test itself changes) • Git based • Don’t run unnecessary tests!
  • 31. Run Tests Fast: Watch Mode • Watch mode is a cli that watches for file changes • Can control which tests are watched
  • 32. Fix Tests Fast • Failed Tests Run First • Accurate error reporting by line • Editor Integration (VS Code)
  • 33. DEMO
  • 34. The Future? • Multi Language Support • More environment Support? • More Editor Integrations ? Repo: https://github.com/facebook/jest
  • 37. Ben McCormick Windsor Circle Twitter: @ben336 Blog: http://benmccormick.org

Editor's Notes

  • #2: Hi everyone! I’ll be honest, I wasn’t sure what to expect when I heard I’d been assigned the last session of the conference. I’m told that people remember the first and last sessions best, but I guess that only works if you’re all still awake :) My presentation today is about saving time, so I’ll try to live up to that by keeping things moving and honoring your time. That said, if you have questions as I go, feel free to ask.
  • #3: So quick note about me, I’m a local developer, I work at Windsor Circle, a startup in downtown Durham. I’ve been using Jest since early 2016, and I write about Jest and other JavaScripty topics on my blog at benmccormick.org. Today I’m going to be talking to y’all about testing.
  • #4: This will break down into 5 parts: What is Jest, what is it trying to solve, how can it save you time, a demo, and a chance for you to ask questions. I will say now that this is an introductory talk. If you were hoping for advanced Jest tips, you’ll probably be disappointed. There’s still time to slip out and find another talk though :). This is going to be about what Jest is and why you would want to use it. But first, I want to get you moving, so here is a quick poll: I want you to raise your hand if you’ve: Ever written a test for JavaScript code Written a test in the last month Wrote a test today (During the conference!) Write tests for all of your new code OK looks like this will be relevant to some of y’all and an opportunity to learn new things for the rest. Separate: Enjoy testing? Think testing is a waste of time? Haha alright I’ll try to show you how it can be easier.
  • #5: So! What is Jest? Jest is an Open Source JavaScript testing framework developed by Facebook. When it was first released, Jest didn’t get the same type of love and attention other Facebook open source projects have seen. That changed in 2015 when Christoph Pojer and Dmitrii Abramov joined the project. They really put in a lot of work to improve it, leading to the release of version 11.0 in April 2016, which marked a big change, and the first step towards it being a modern project I will note that Jest works well with Facebook’s other projects like React, but is not “for” them, and can be used in many JavaScript environments. Now before we go further, lets take a step back and talk about the state of JavaScript testing
  • #6: JavaScript has some distinct challenges. I wouldn’t say these are unique challenges, but rather challenges that are more common in the situations where JavaScript is used. JavaScript runs in a lot of places: Node, different browsers, React Native, Electron. That means compatibility is a big deal. And some of those environments are more amenable to testing than others. JS is used for lots of async code. It’s used in the browser and on phones, and in server side web applications. Asynchronous code is notoriously difficult to test and just to comprehend in general Lots of JavaScript is used for building UIs and UIs are hard to test Requirements change a lot for UIs, and its difficult to define a UI to the level of detail needed to run adequate tests much less maintain that over time. Being real, most UIs see a lot of spaghetti code build up over time. That makes testing even harder.
  • #7: All of these challenges and more add up to one real challenge: People don’t write tests. Its hard. Its annoying. So people avoid testing or write inadequate tests. I’m certainly guilty of this, especially when I’m trying to go fast. The problem is, since test suites gain in value the more comprehensive they are, and can be hard to build up, many folks never see any value from tests and may just give up. What do you do when something is important but people don’t do it because its hard or not fun? You either dictate to them, tell them they have to do it, and then fight them over it… or you try to make the experience better.
  • #8: Jest is not the first attempt to solve these problems. There are a lot of JavaScript testing libraries out there. Mocha Jasmine and Karma are big ones that have been around a while now. My understanding is that Tape and Ava are big in node-world QUnit was a jQuery project and is now widely used by the Ember community The test libraries here provide assertion functions or other test utilities and aren’t full test runners. Basically there are a lot of options.
  • #9: So what I always want to know when I evaluate a new tool, especially in an area where there are lots of competitors, is — where does it fit in? How does it stand out (good and bad) against the other options? Jest Saves you time - gonna be talking a lot about that Visual - information is presented clearly Reliable - AFAIC remember I’ve only had a single bug the whole time I’ve been using Jest (A single bug with the framework, it’s caught a lot of my own bugs) Learning curve was a big part of the overhaul of Jest over the course of 2016. Jest has simple concepts and complicated concepts, and previously the defaults required understanding the complicated concepts before using Jest. That is no longer the case, and it now has a nice learning curve.
  • #10: So those were the pros, its also good to understand the limitations. Jest does not test every environment. I mentioned earlier that multiple environments was a JavaScript testing challenge. Jest does not attempt to address that as it exists today. It runs all of its tests against a virtualized DOM (jsDOM) in Node, and doesn’t run against a browser at all. Jest is not simple. It still has plenty of concepts, and is more of a “do everything” library , not a “do one thing well” library. Those components are split out into separate modules though, so you can use parts of it for specific tasks if you want. Jest has gotten a lot more stable in the past year, as it has become “good enough” for Facebook, but it is still making steady progress, and it won’t stay the same.
  • #11: Alright that was the intro to Jest, now lets talk about how Jest can save you time. I’m going to discuss 4 ways that Jest can save you time. As part of Setting up tests Writing Tests Running Tests Fixing Tests We’ll take them one at a time
  • #12: The first way Jest helps you save time is during initial setup. Jest only requires installing one or two dependencies to get a test environment going, and doesn’t require any config by default. It also provides ways to automate switching from other test libraries.
  • #13: This is an example of fairly normal setups for local testing of JavaScript using Jest vs an older test framework. Please note that this slide is probably not completely fair. You definitely can set up a test environment with Jasmine that has fewer dependencies than what I’m showing, Karma is more flexible in what it can do than just running server side tests. There are other test frameworks that can be simple (AVA) But Jest is difficult to make complicated in terms of dependencies and comes with a lot of common things out of the box. Out of the box you get: Babel support Assertions library AND test runner Watch mode (I’ll talk more about that in a bit) And Code coverage When using other testing tools, a lot of these features are split out into separate libraries that require custom setup and configuration to use.
  • #14: So this is an example of a minimal project. All I’ve done is pull in Jest, babel-jest, and a babel preset package (if you don’t use babel, Babel is a tool that transforms JavaScript using newer or experimental syntax into code based on an older more compatible subset of the language. The preset just tells babel how I want the code to be transformed) I have one source file, and one test file, and I’ve written no configuration. I did set the nom script `test` to run jest
  • #15: Here we have the source and test file in our simple project. A simple sum function, and 2 tests, one that should pass, and one that should fail (because 1 + 2 + 7 doesn’t equal 9). Note that we’re using modern import/export syntax, which is not supported on Node natively at this point. So we need to use babel to compile it
  • #16: I’ll show stuff like this live in the demo later, but running npm test with this simple test, Jest is able to identify my test files and run them. That lookup is based on a few default regular expressions. All of that is configurable if you’re opinionated about how to structure your project, but you can also just enjoy the easy defaults and skip having a config file when you’re getting started.
  • #17: So I already pointed out that this super simple little test is using babel syntax (the import/export). But we were able to run the test just fine. That means the test is running against transformed code, and that one liner of adding babel-jest let us run this test with no problems. Note that it still gets the line numbers right and everything when identifying failed tests. <point out line 9 -> line 9>
  • #18: Ok, another quick poll Who is using/has used any of the following libraries? The stuff we’ve talked about before makes it nice to start new projects but what about those of you who are already invested in something?
  • #19: Jest makes it easy to convert from other test frameworks with codemods. A code mod is a script to perform a specific transformation on a set of code, Jest uses them to transform code from other frameworks into something usable by Jest It isn’t perfect, but it will let you know what it can convert and can’t and is a great start for moving tests over to a new system
  • #20: Jest also helps you write tests fast. Jest’s test syntax actually started out as a fork of Jasmine. It has since been extended and re-written but most Jasmine code can be used in Jest without any changes, and everything should feel very familiar Jest also provides a simpler style that is reminiscent of other newer frameworks like Tape and AVA Jest also provides great, detailed APIs for testing different specific scenarios And finally, Jest’s signature feature is snapshot testing. I’ll be talking about that in more detail in a minute
  • #21: So here’s an example of 2 tests using different syntaxes. Jest makes it easy to write tests by NOT innovating on syntax. It lets you write tests in 2 different styles, and they both are pulled straight from widely used libraries. You can see the first test here is written in the Jasmine “describe/it/expect” style, while the second one uses the simplified test syntax The describe style can be nice when you want to group a set of tests with the same “beforeEach”/afterEach setup or cleanup steps. But otherwise, `test` is generally simpler. Either way you can use whatever is familiar or comfortable for you.
  • #22: Jest provides really expressive assertion APIs that let us handle tricky situations like async functions, testing function behavior, and testing the shape of an object easily The first test here demonstrates how Jest tests asynchronous code (a promise). If you’re not familiar with promises, they run a bit of code that can be asynchronous, and then call a resolve or reject callback when they’re completed. Other code can provide handlers for the resolved or rejected values. Asynchronous logic is usually annoying in tests, but Jest provides `resolve` and `reject` methods on expect that makes it easy to test the async values generated by a promise. And since its easy to wrap other async methods like `setTimeout` in a promise, this works for testing any async code. The second test is showing how Jest handles mock functions. There’s nothing exciting here, but Jest provides methods for determining very specifically how a given function was called or not called. The final test is showing a few of the methods we have for checking the shape of an object. The first test is `toEqual`, which does a recursive deep match. That fails because the object isn’t exactly the same. The second is `toMatch`, which looks to see if the object has the same properties as what it is being tested against (but ignores other things). That passes. The third test just looks to see if an object has a specific property. All 3 can be useful depending on how much control you want to have over a test result. These are just examples, Jest has over 25 different specific assertions you can use. Detailed APIs like this help you write tests that aren’t too boilerplate-heavy, don’t need a bunch of setup code, and also don’t result in too much rigidity and break too easily.
  • #23: So this is something I just saw this morning on Twitter. Look at this whole library of extra matchers to play with! Thats the benefit of Jest having an extensible core.
  • #24: Snapshot tests are Jest’s signature feature. They’re a way of testing that the results of a particular operation have not changed, or seeing how they have changed, rather than comparing them to a universally expected outcome. Jest’s snapshot tests have really nice react handling out of the box, but can be used against lots of things, including other UI libraries. Pretty much anything that can be described as a JSON object can be snapshot tested
  • #25: Ok so there is a lot of code on this slide. Lets break it down: This is code for a simple link component. (It’s actually from the Jest repo’s example list, but it’s pretty perfect as an example, so I’m stealing it) We have 3 files here, a link source file, a link test file (note the `toMatchSnapshot` syntax), and a snapshot file. The snapshot file shows the result of running the 2 tests in the test file. But what happens if we change something?
  • #26: So if we just change the test to have a new default value for the link’s href…
  • #27: We can run Jest and see 2 tests are failing, and we get a diff for what is different in those tests. For something like this where its a direct change to the same component, that may not be too interesting. But this can be very helpful for showing side effects and letting us know all the places effected by a change. It also can show which states change when we make a change to logic.
  • #28: If we’re satisfied that the changes we saw are ok, we can run jest -u to update. This updates the snapshots, and once they’re checked into git, they become the new basis of comparison when the test is run in the future.
  • #29: So lets talk pros and cons: Snapshot tests are good for things that are going to change a lot, and also good for generating a lot of tests quickly. However they require good code review, and tend to be less helpful at communicating the original developers intent I find them really useful for UI tests, where the real world alternative would be not having real tests since the code changes so often and in arbitrary ways, and for running code against a large amount of real world data so I can observe the effect of changes against that data. For example I recently used it when I wrote a function that took a JSON format describing data for a graph, and converted it to a configuration for the charting library I was using. I had 20some graph configs actually being used in my application, so I took snapshots of the configs generated for each of them, so that I could see how they changed if I ever changed the transformation logic. It wouldn’t have been worth writing 20 detailed tests laying out the exact shape for the data, but it took me 2 minutes to copy and paste the data into a test file and set up a snapshot test
  • #30: Jest saves you time by running tests quickly. Some of this is just how Jest runs tests. For many traditional JavaScript testing methods, tests run in a browser, which leads to slowness and flakiness when setting up and communicating with an external application. Jest runs tests in Node against a virtual DOM, and therefore has very little overhead on startup. Jest can also run tests in parallel, while sandboxing the tests so that they don’t conflict with each other But the more interesting touches are more about UX. Smart testing and watch mode.
  • #31: Smart tests are really what makes Jest run fast. Jest can give you instant feedback on your changes, because it only runs the tests it needs. Even if you have a gigantic 5000 test project, it gives feedback quickly because it only runs a few tests at a time, and you don’t have to run special scripts to accomplish that. Instead Jest uses git and import tracking to determine what files have changed and which files depend on other files. It builds a dependency graph and then only runs tests when they change or a file they depend on changes.
  • #32: Smart testing is made even better by watch mode. You can leave it on all the time and it will update with feedback as you make changes. This will show a bit better with the demo in a second.
  • #33: The last way Jest saves you time is by helping you fix tests fast. I’m not going to get into this too much today, but this is also really about the UX touches, with good error reporting, making it easy to see if your fixes worked, and editor integration to Visual Studio Code.
  • #34: Ok demo time!
  • #35: I always like to talk about how things are changing, not just how they are now. So whats next for Jest? Jest has settled down a bit after a lot of changes in the past 2 years, but there are still interesting things on the horizon. They’re investigating using the jest test runner for tests in other programming languages like Python. There also is interest in allowing parts of Jest to run in the browser, and adding first class support to more editors. I know they’re looking for contributors to tackle all of those things, so if you’re inspired to contribute to open source after this conference, feel free to checkout their GitHub page