SlideShare a Scribd company logo
JavaScript Testing
   With Mocha and Chai
JavaScript Test Frameworks
•   QUnit


•   Mocha


•   YUI Test


•   Jasmine


•   JSUnit       •   CrosscheckJ3Unit JSNUnit JSSpec
                     UnitTestingJSpec
                     screw-unit
•   Suitest
                     Test.SimpleRhinoUnit     Buster.JS
•   Sinon.js


•   DOH


•   Enhance JS


•   RhUnit
Choosing a framework

•   Client side?

•   Server side?

•   Well maintained?

•   Well documented?

•   Integration with CI
Mocha
Mocha

•   Feature Rich

•   Runs on node + the browser

•   Simplifies async testing

•   Growl notifications

•   Choose your own assertion library
Chai
•   BBD / TDD

•   For node + the browser

•   Three assertion styles

    •   should - foo.should.be.a(‘string’)

    •   expect - expect(foo).to.be.a(‘string’)

    •   assert - assert.typeOf(foo, ‘string’)
Getting started


•   Requires node

•   Requires npm

•   npm install -g mocha

•   npm install -g chai
Setup

•   Expects tests to be in <project_root>/test

•   Allows of per project options file:

    •   mocha.opts

•   To run test:

    •   mocha
First test
describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
       [1,2,3].indexOf(5).should.equal(-1);
       [1,2,3].indexOf(0).should.equal(-1);
    })
  })
})
Hooks


•   before()

•   after()

•   beforeEach()

•   afterEach()
Modifying Test Cases


•   Pending tests - no callback

•   Exclusive tests - append .only

•   Inclusive tests - append .skip
Other features

•   mocha --reports

•   mocha --watch

•   mocha --growl

•   mocha --compilers

•   Interface support for: TDD, BDD + QUnit
CoffeeScript

•   mocha --compilers coffee:coffee-script

             describe 'Task instance', ->
               task1 = task2 = null
               it 'should have a name', ->
                 task1 = new Task 'feed the cat'
                 task1.name.should.equal 'feed the cat'
Fin

More Related Content

PDF
Testing with Express, Mocha & Chai
PDF
FlutterでGraphQLを扱う
PDF
グラフデータベース Neptune 使ってみた
PDF
君はyarn.lockをコミットしているか?
PDF
ReactとSeleniumの幸せな関係
PDF
Redux Toolkit - Quick Intro - 2022
PDF
アジャイル開発の現在・過去・未来~今を知り、源流を訪ね、先を見据える~
PPTX
Testing with Express, Mocha & Chai
FlutterでGraphQLを扱う
グラフデータベース Neptune 使ってみた
君はyarn.lockをコミットしているか?
ReactとSeleniumの幸せな関係
Redux Toolkit - Quick Intro - 2022
アジャイル開発の現在・過去・未来~今を知り、源流を訪ね、先を見据える~

What's hot (20)

PDF
Jakarta CDI 4.0
PPTX
How to Get Started with Cypress
PPT
PDF
NET MAUI for .NET 7 for iOS, Android app development
PDF
ツール比較しながら語る O/RマッパーとDBマイグレーションの実際のところ
PPTX
Why you should switch to Cypress for modern web testing?
PDF
Selenium WebDriver with C#
PPTX
5分で出来る!イケてるconfluenceページ
PPTX
React Hooks
PPTX
Wpfと非同期
PDF
Espresso testing
PDF
Prism + ReactiveProperty入門
PDF
Cucumber ppt
PPTX
Cypress Automation
PDF
ASP.NET シングル ページ アプリケーション (SPA) 詳説
PDF
Javaのログ出力: 道具と考え方
PDF
【BS13】チーム開発がこんなにも快適に!コーディングもデバッグも GitHub 上で。 GitHub Codespaces で叶えられるシームレスな開発
PPTX
実践!Django + GraphQL 実装
PDF
Cypress vs Selenium WebDriver: Better, Or Just Different? -- by Gil Tayar
Jakarta CDI 4.0
How to Get Started with Cypress
NET MAUI for .NET 7 for iOS, Android app development
ツール比較しながら語る O/RマッパーとDBマイグレーションの実際のところ
Why you should switch to Cypress for modern web testing?
Selenium WebDriver with C#
5分で出来る!イケてるconfluenceページ
React Hooks
Wpfと非同期
Espresso testing
Prism + ReactiveProperty入門
Cucumber ppt
Cypress Automation
ASP.NET シングル ページ アプリケーション (SPA) 詳説
Javaのログ出力: 道具と考え方
【BS13】チーム開発がこんなにも快適に!コーディングもデバッグも GitHub 上で。 GitHub Codespaces で叶えられるシームレスな開発
実践!Django + GraphQL 実装
Cypress vs Selenium WebDriver: Better, Or Just Different? -- by Gil Tayar
Ad

Similar to JavaScript Testing: Mocha + Chai (20)

PPT
Mocha Testing
PDF
TDD for Javascript developers
PPTX
How do I write Testable Javascript so I can Test my CF API on Server and Client
PDF
Developers Testing - Girl Code at bloomon
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
PPTX
Full Stack Unit Testing
PDF
How do I Write Testable Javascript so I can Test my CF API on Server and Client
PPTX
Testing nodejs apps
PDF
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
PDF
Test driven node.js
PDF
Node.js Development Workflow Automation with Grunt.js
PDF
Maximize Your JavaScript Test Automation: Top Frameworks & Best Practices
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PDF
An Introduction to the World of Testing for Front-End Developers
PDF
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
PDF
Unit testing with mocha
PPTX
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
PDF
mocha sinon chai Dc jquery 4-24
Mocha Testing
TDD for Javascript developers
How do I write Testable Javascript so I can Test my CF API on Server and Client
Developers Testing - Girl Code at bloomon
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API -
Full Stack Unit Testing
How do I Write Testable Javascript so I can Test my CF API on Server and Client
Testing nodejs apps
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
Test driven node.js
Node.js Development Workflow Automation with Grunt.js
Maximize Your JavaScript Test Automation: Top Frameworks & Best Practices
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
An Introduction to the World of Testing for Front-End Developers
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
Unit testing with mocha
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
Intro To JavaScript Unit Testing - Ran Mizrahi
mocha sinon chai Dc jquery 4-24
Ad

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
sap open course for s4hana steps from ECC to s4
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf

JavaScript Testing: Mocha + Chai

  • 1. JavaScript Testing With Mocha and Chai
  • 2. JavaScript Test Frameworks • QUnit • Mocha • YUI Test • Jasmine • JSUnit • CrosscheckJ3Unit JSNUnit JSSpec UnitTestingJSpec screw-unit • Suitest Test.SimpleRhinoUnit Buster.JS • Sinon.js • DOH • Enhance JS • RhUnit
  • 3. Choosing a framework • Client side? • Server side? • Well maintained? • Well documented? • Integration with CI
  • 5. Mocha • Feature Rich • Runs on node + the browser • Simplifies async testing • Growl notifications • Choose your own assertion library
  • 6. Chai • BBD / TDD • For node + the browser • Three assertion styles • should - foo.should.be.a(‘string’) • expect - expect(foo).to.be.a(‘string’) • assert - assert.typeOf(foo, ‘string’)
  • 7. Getting started • Requires node • Requires npm • npm install -g mocha • npm install -g chai
  • 8. Setup • Expects tests to be in <project_root>/test • Allows of per project options file: • mocha.opts • To run test: • mocha
  • 9. First test describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present', function(){ [1,2,3].indexOf(5).should.equal(-1); [1,2,3].indexOf(0).should.equal(-1); }) }) })
  • 10. Hooks • before() • after() • beforeEach() • afterEach()
  • 11. Modifying Test Cases • Pending tests - no callback • Exclusive tests - append .only • Inclusive tests - append .skip
  • 12. Other features • mocha --reports • mocha --watch • mocha --growl • mocha --compilers • Interface support for: TDD, BDD + QUnit
  • 13. CoffeeScript • mocha --compilers coffee:coffee-script describe 'Task instance', -> task1 = task2 = null it 'should have a name', -> task1 = new Task 'feed the cat' task1.name.should.equal 'feed the cat'
  • 14. Fin