SlideShare uma empresa Scribd logo
1 de 18
CommonJS & 
Mocking with Jest 
James Hunter – @cccc00
CommonJS 
var thing = require(“thing”);
“For a unit test to operate on a truly isolated unit 
of the application, we need to mock every 
module except the on we are testing.” 
– facebook.github.io/react/blog/2014/09/24/testing-flux-applications.html
Classicist vs Mockist 
State vs Behaviour
Doubles 
Dummy, Fake, Stub & Mock
“Dummy objects are passed around but never 
actually used. Usually they are just used to fill 
parameter lists.” 
–Martin Fowler [Mocks Aren’t Stubs]
“Fake Objects actually have working 
implementations but usually take some shortcut 
which makes them not suitable for production 
(i.e.: In memory database)” 
–Martin Fowler [Mocks Aren’t Stubs]
“Stubs provide canned answers to calls made 
during the test, usually not responding at all to 
anything outside what’s programmed in for the 
test.” 
–Martin Fowler [Mocks Aren’t Stubs]
“Mocks objects pre-programmed with 
expectations which form a specification of the 
calls they are expected to receive.” 
–Martin Fowler [Mocks Aren’t Stubs]
Stub External State
Mock External 
Behaviour
To Mock Something Properly 
You Need Intimate Knowledge 
of How It Works
Jest
Jest 
• Developed and used internally at Facebook 
• Runs tests in parallel 
• Mock by default 
• Jasmine style syntax (describe, it, beforeEach) 
• Automatically finds tests to execute 
• Can test asynchronous thing synchronously 
• Fake DOM available via jsdom 
• Support for preprocessors (jsx, es6, coffee)
Installing & Using Jest 
• $ npm install --save-dev jest-cli 
• “scripts”:{ “test”: “./node_modules/.bin/jest” } 
• put tests in any `__tests__` directory 
• $ npm test
Tacos App
Learn More 
• facebook.github.io/jest 
• facebook.github.io/jest/docs/api.html 
• martinfowler.com/articles/mocksArentStubs.html 
• facebook.github.io/react/blog/2014/09/24/testing-flux-applications. 
html 
• Taco App Repo: github.com/orodio/melbjs-mock
CommonJS & 
Mocking with Jest 
James Hunter – @cccc00

Mais conteúdo relacionado

Destaque

Javascript unit testing framework
Javascript unit testing frameworkJavascript unit testing framework
Javascript unit testing framework
Võ Duy Tuấn
 
java script unit testing framework
java script unit testing frameworkjava script unit testing framework
java script unit testing framework
hazzaz
 

Destaque (20)

Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
 
Test driven development - the right way
Test driven development - the right wayTest driven development - the right way
Test driven development - the right way
 
Javascript Bundling and modularization
Javascript Bundling and modularizationJavascript Bundling and modularization
Javascript Bundling and modularization
 
Don't Leave Windows Broken
Don't Leave Windows BrokenDon't Leave Windows Broken
Don't Leave Windows Broken
 
JEST: REST on OpenJPA
JEST: REST on OpenJPAJEST: REST on OpenJPA
JEST: REST on OpenJPA
 
Isomorphic React Apps Testing
Isomorphic React Apps TestingIsomorphic React Apps Testing
Isomorphic React Apps Testing
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven TomacJavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
 
Javascript Testing Tools - Overview
Javascript Testing Tools - OverviewJavascript Testing Tools - Overview
Javascript Testing Tools - Overview
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Writing and Testing JavaScript-heavy Web 2.0 apps with JSUnit
Writing and Testing JavaScript-heavy Web 2.0 apps with JSUnitWriting and Testing JavaScript-heavy Web 2.0 apps with JSUnit
Writing and Testing JavaScript-heavy Web 2.0 apps with JSUnit
 
Javascript unit testing framework
Javascript unit testing frameworkJavascript unit testing framework
Javascript unit testing framework
 
Unit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitUnit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnit
 
Performance, Games, and Distributed Testing in JavaScript
Performance, Games, and Distributed Testing in JavaScriptPerformance, Games, and Distributed Testing in JavaScript
Performance, Games, and Distributed Testing in JavaScript
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Jasmine - A BDD test framework for JavaScript
Jasmine - A BDD test framework for JavaScriptJasmine - A BDD test framework for JavaScript
Jasmine - A BDD test framework for JavaScript
 
java script unit testing framework
java script unit testing frameworkjava script unit testing framework
java script unit testing framework
 
JavaScript Unit Testing with Jasmine
JavaScript Unit Testing with JasmineJavaScript Unit Testing with Jasmine
JavaScript Unit Testing with Jasmine
 
Joomla! JavaScript Testing
Joomla! JavaScript TestingJoomla! JavaScript Testing
Joomla! JavaScript Testing
 
Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013
 
Jasmine
JasmineJasmine
Jasmine
 

Semelhante a MelbJS Nov2014 - CommonJS & Mocking with Jest

Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Tony Nguyen
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Harry Potter
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Young Alista
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Luis Goldster
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Fraboni Ec
 

Semelhante a MelbJS Nov2014 - CommonJS & Mocking with Jest (20)

Mocking
MockingMocking
Mocking
 
Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testing
 
Python mocking intro
Python mocking introPython mocking intro
Python mocking intro
 
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubsIntegration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 
Integration testing
Integration testingIntegration testing
Integration testing
 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
 
Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...
 
Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)
 
TDD and mock objects
TDD and mock objectsTDD and mock objects
TDD and mock objects
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

MelbJS Nov2014 - CommonJS & Mocking with Jest

  • 1. CommonJS & Mocking with Jest James Hunter – @cccc00
  • 2. CommonJS var thing = require(“thing”);
  • 3. “For a unit test to operate on a truly isolated unit of the application, we need to mock every module except the on we are testing.” – facebook.github.io/react/blog/2014/09/24/testing-flux-applications.html
  • 4. Classicist vs Mockist State vs Behaviour
  • 5. Doubles Dummy, Fake, Stub & Mock
  • 6. “Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.” –Martin Fowler [Mocks Aren’t Stubs]
  • 7. “Fake Objects actually have working implementations but usually take some shortcut which makes them not suitable for production (i.e.: In memory database)” –Martin Fowler [Mocks Aren’t Stubs]
  • 8. “Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test.” –Martin Fowler [Mocks Aren’t Stubs]
  • 9. “Mocks objects pre-programmed with expectations which form a specification of the calls they are expected to receive.” –Martin Fowler [Mocks Aren’t Stubs]
  • 12. To Mock Something Properly You Need Intimate Knowledge of How It Works
  • 13. Jest
  • 14. Jest • Developed and used internally at Facebook • Runs tests in parallel • Mock by default • Jasmine style syntax (describe, it, beforeEach) • Automatically finds tests to execute • Can test asynchronous thing synchronously • Fake DOM available via jsdom • Support for preprocessors (jsx, es6, coffee)
  • 15. Installing & Using Jest • $ npm install --save-dev jest-cli • “scripts”:{ “test”: “./node_modules/.bin/jest” } • put tests in any `__tests__` directory • $ npm test
  • 17. Learn More • facebook.github.io/jest • facebook.github.io/jest/docs/api.html • martinfowler.com/articles/mocksArentStubs.html • facebook.github.io/react/blog/2014/09/24/testing-flux-applications. html • Taco App Repo: github.com/orodio/melbjs-mock
  • 18. CommonJS & Mocking with Jest James Hunter – @cccc00