SlideShare a Scribd company logo
1 of 13
1
CONFIDENTIAL
Andrew Winder
Senior Software Engineer, Message Systems
Testing Javascript with Mocha and
Friends
July 31, 2014
2
Why Testing?
3
What is a test?
expect(something).to.equal(something)
4
What is a test?
expect(something).to.equal(something)
The Subject
5
What is a test?
expect(something).to.equal(something)
The Assertion
6
What Kind of Testing?
7
How to test Javascript?
8
Simple Unit Testing
9
What about Async?
10
What about Async?
11
Mocking with Sinon & Proxyquire
• Use Mocks in unit testing for:
• Stability
• Speed
• Accuracy
• Proxyquire for Mock Injection
• Sinon for creating Mocks
12
Example of ProxyQuire & Sinon.JS
13
Demo & Questions
Twitter: @awinder
andrew.winder@messagesystems.com

More Related Content

What's hot

Appropriate questions for evidence gathering
Appropriate questions for evidence gatheringAppropriate questions for evidence gathering
Appropriate questions for evidence gathering
NMarshall01
 
Test Driven Relevancy, Presented by Doug Turnbull at SolrExchage DC
Test Driven Relevancy, Presented by Doug Turnbull at SolrExchage DCTest Driven Relevancy, Presented by Doug Turnbull at SolrExchage DC
Test Driven Relevancy, Presented by Doug Turnbull at SolrExchage DC
Lucidworks (Archived)
 

What's hot (8)

Re-thinking Test Automation and Test Process Modelling (in pictures)
Re-thinking Test Automation and Test Process Modelling (in pictures)Re-thinking Test Automation and Test Process Modelling (in pictures)
Re-thinking Test Automation and Test Process Modelling (in pictures)
 
Agile Testing in the Enterprise
Agile Testing in the Enterprise Agile Testing in the Enterprise
Agile Testing in the Enterprise
 
Escaping the Pitfalls of Software Product Development
Escaping the Pitfalls of Software Product DevelopmentEscaping the Pitfalls of Software Product Development
Escaping the Pitfalls of Software Product Development
 
Appropriate questions for evidence gathering
Appropriate questions for evidence gatheringAppropriate questions for evidence gathering
Appropriate questions for evidence gathering
 
Continuous Delivery antipatterns from the wild - Matthew Skelton - IPEXPO Man...
Continuous Delivery antipatterns from the wild - Matthew Skelton - IPEXPO Man...Continuous Delivery antipatterns from the wild - Matthew Skelton - IPEXPO Man...
Continuous Delivery antipatterns from the wild - Matthew Skelton - IPEXPO Man...
 
Test Driven Relevancy, Presented by Doug Turnbull at SolrExchage DC
Test Driven Relevancy, Presented by Doug Turnbull at SolrExchage DCTest Driven Relevancy, Presented by Doug Turnbull at SolrExchage DC
Test Driven Relevancy, Presented by Doug Turnbull at SolrExchage DC
 
Working effectively with legacy codes
Working effectively with legacy codesWorking effectively with legacy codes
Working effectively with legacy codes
 
Test Driven Product: Applying Test Driven Thinking to the Product World
Test Driven Product: Applying Test Driven Thinking to the Product WorldTest Driven Product: Applying Test Driven Thinking to the Product World
Test Driven Product: Applying Test Driven Thinking to the Product World
 

Viewers also liked

Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverageTesting NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
mlilley
 

Viewers also liked (17)

Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverageTesting NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
 
JavaScript Testing: Mocha + Chai
JavaScript Testing: Mocha + ChaiJavaScript Testing: Mocha + Chai
JavaScript Testing: Mocha + Chai
 
Unit testing with mocha
Unit testing with mochaUnit testing with mocha
Unit testing with mocha
 
Mocha Testing
Mocha TestingMocha Testing
Mocha Testing
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
 
Mocha.js
Mocha.jsMocha.js
Mocha.js
 
Testing with Express, Mocha & Chai
Testing with Express, Mocha & ChaiTesting with Express, Mocha & Chai
Testing with Express, Mocha & Chai
 
Test automation introduction training at Polteq
Test automation   introduction training at PolteqTest automation   introduction training at Polteq
Test automation introduction training at Polteq
 
Unit Testing TypeScript
Unit Testing TypeScriptUnit Testing TypeScript
Unit Testing TypeScript
 
Agile scrum roles
Agile scrum rolesAgile scrum roles
Agile scrum roles
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
The What, Why and How of (Web) Analytics Testing (Web, IoT, Big Data)
The What, Why and How of (Web) Analytics Testing (Web, IoT, Big Data)The What, Why and How of (Web) Analytics Testing (Web, IoT, Big Data)
The What, Why and How of (Web) Analytics Testing (Web, IoT, Big Data)
 
Appium: Prime Cuts
Appium: Prime CutsAppium: Prime Cuts
Appium: Prime Cuts
 
DevOps Pipelines and Metrics Driven Feedback Loops
DevOps Pipelines and Metrics Driven Feedback LoopsDevOps Pipelines and Metrics Driven Feedback Loops
DevOps Pipelines and Metrics Driven Feedback Loops
 
Alphorm.com Formation TypeScript
Alphorm.com Formation TypeScriptAlphorm.com Formation TypeScript
Alphorm.com Formation TypeScript
 
Mudularity and Unit Testing in TypeScript (for ng-bkk #3)
Mudularity and Unit Testing in TypeScript (for ng-bkk #3)Mudularity and Unit Testing in TypeScript (for ng-bkk #3)
Mudularity and Unit Testing in TypeScript (for ng-bkk #3)
 
Les Aventures d'Alice - la Révolte des Tests
Les Aventures d'Alice - la Révolte des TestsLes Aventures d'Alice - la Révolte des Tests
Les Aventures d'Alice - la Révolte des Tests
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Testing Javascript Apps with Mocha and Chai

Editor's Notes

  1. Hi, I’m Andy Winder, and I work here at Message Systems as one of the engineers working on NodeJS APIs, and I wanted to talk today about some of the ways you can test your software, and go through some quick examples of testing JavaScript.
  2. There are 3 main reasons for why we even want to do this whole testing business. We want to be able to ensure the quality of the code as we write it. Testing makes us think of how we’re writing code, and how people will interface with that code (either internally as developers or externally) as users. We want to be able to ensure our code is stable, and that any changes we make to the system don’t have any knock-on effects. We want to be able to refactor code and make sure it still maintains the same external interface. And we also want the security that we are able to change our systems and code without the whole thing blowing up in the future.
  3. A test is a really simple construct, with lots of complexity that ends up coming around it. But at it’s core, testing is really just two parts…
  4. The Subject is something that you want to introspect in your test. It’s usually some kind of output from the code you’re testing. If you’re testing a function, it might be the return value. If you’re testing a system, it might a variable that you look up in a database, or a string of text that you grab off the screen.
  5. The Assertion is what you want to confirm about the subject of the test. You might want to confirm the value is equal to something, or you might want to confirm that it isn’t an error. If the assertion passes, that means your system is behaving as you expect it to. When an assertion fails, that means something is not working correctly within your code.
  6. Three main kinds of testing: Unit testing, Integration Testing, Functional Testing. We write unit tests to make sure that individual objects can be interacted with, in isolation, the way we expect. Integration testing makes sure our objects can be consumed and used by other parts of the system in a stable way. Functional testing makes sure that our system as a whole works as expected in the same way that an end user is going to us the system.
  7. JavaScript has a great ecosystem of tools for testing. Mocha, and alternatively Jasmine, are great test runners. They provide command line tools for running tests, they can help bootstrap environments, and provide different test result outputs to let you know what is passing and failing. Chai is an assertion library that provides the language for writing your tests. Sinon helps you write unit tests, while Protractor helps you test out your Angular apps. All these tools operate both independently and cooperatively with each other.
  8. Imagine we have a method called getVehicleCost that we want to test. It takes in a parameter for the type of vehicle and returns the cost of that vehicle, or an error if we don’t know anything about the vehicle argument. So our tests need to be able to test the conditions that we know about, and the conditions that we don’t know about. We pass in expected arguments, car and boat, and then try to trick the function up by passing something that it doesn’t know about.
  9. But what about asynchronous code? It’s going to be harder to write tests, right?
  10. By handling the “done” function argument in your tests, you can control when the test case is done, and then use that to wait for asynchronous tests to finish up.
  11. Mocking is a technique that is commonly used in unit testing. Remember before we talked about the purpose of unit testing as testing individual units of code, like objects, in isolation. If you have code that depends on third party services like MySQL, or a REST API somewhere, then you would want to replace interactions with those services with a fake, or “mock”, service. This way, you’re testing the unit by itself, rather than its interactions with the full system. This will help you write really fast test suites, that are very dependable, and have less dependencies than when you go to test your full system.
  12. Sinon is a tool that can help with creating mocks. Think back to our example of async testing, where we queried MySQL . We had a call to get a MySQL connection object, then a call to query the database. We can use proxyquire to replace the calls to the MySQL module, and then use “yields” in our query mock to simulate the asynchronous callback from the database. Now our tests don’t rely on even having MySQL set up, and they don’t require any of the connection time & querying time that would normally need to take place. Over time this will save tons of time and is very useful for unit testing, and meeting the goals of unit tests being very stable & predictable.
  13. So to recap, there’s 3 main types of testing: Unit testing, for fine-grained testing of individual modules in our codebases. Integration testing, for seeing how those individual modules connect with each other and start to form the basis for a functioning system. And functional testing, for testing full codebases in the ways users will interact with the system. Mocha, Sinon, and Proxyquire form the basis for really solid unit testing in Node.JS and in frontend / angular codebases. And we got to dig in a little on some examples of how these tests can look when testing out some simple code that connects to MySQL. Thanks for coming out today! Any questions?