SlideShare a Scribd company logo
How to write test in Node.js
Jason Lin (starrlingo@gmail.com)
2017/08
1
Why
• Why we need to write test?
– Few questions
• How do you know the status of the system?
• How do you know the code you commit meet the requirement?
• How do you know the code you commit won’t break other module?
• Side effect?
– Problems: Manual test is trivial and time consuming
– Solution: write test and make it run automatically
2
What
• Testing methodologies
3
Test the individual component Test the component groups Test the integrated systems Test the final system
Reference: https://www.inflectra.com/ideas/topic/testing-methodologies.aspx
What
• Testing methodologies
– Unit Test
• Test on isolated component (public)
• Shouldn’t have dependencies (file, db, 3rd Party Lib, …)
– Integrate Test
• Testing on multiple components
• Verify the interaction between components
• It may rely on dependencies
– System Test
• Concerned with the behavior of the full system
4
What
• Development process
– TDD (Test Driven Development)
• Write test first
• Just meet the requirement
• Never over-design
5
Reference: https://www.allaboutcircuits.com/technical-articles/how-test-
driven-development-can-help-you-write-better-unit-tests/
What
• Development process
– BDD (Behavior Driven Development)
• Develop software correctly and develop correct software
• Focused on the business behaviors
• Spec as test case
– PM and target user wrote stories together
– PM and engineers wrote the scenarios together
• Story (gherkin syntax)
• Scenario (gherkin syntax)
6
As a <type of stakeholder>
I want <a feature>
So that <some goal can be met>
Given <a context>
When <an event happens>
Then <an outcome should occur>
What
• Development process
– BDD (Behavior Driven Development)
• Example
7
Feature: QIoT Backup
As an administrator,
I want to backup the whole QIoT
So that I can restore the system
Scenario: Backup QIoT
Given the administrator logged in to QIoT
When administrator press the backup button
Then the backup zip file should be downloaded
automatically
What
• Development process
– BDD + TDD
8
Reference: https://www.getzephyr.com/insights/collaboration-backbone-agile-
testing
How
• How can we write test in Node.js?
– Test Framework
• Mocha / Jasmine / Ava
• Cucumber
– Assertion Library
• Chai
– Test Double Library
• Sinon / Mockery / Proxyquire
9
Test Framework - Mocha
• What is Mocha
– Run on Node.js/Browser
– Supports BDD/TDD
– Choose any assertion library
– Choose any mocking library
– Asynchronous function and promise function support
– Highlights slow tests
10
Test Framework - Mocha
• How to install & run Mocha
11
Testing Result:
Test Framework - Mocha
• How to write test with Mocha
– BDD style (default)
12
Test Framework - Mocha
• How to write test with Mocha
– TDD style
13
Test Framework - Cucumber
• Features
– Support Node.js / Java / Ruby
– Deliver executable spec document
– Support Gherkin DSL syntax
• Example
– 1. Installation: npm install cucumber
– 2. Wrote feature file
14
features/grocery.feature
Test Framework - Cucumber
• Examples
– 3. Generate template
• ./node_modules/cucumber/bin/cucumber.js
– 4. Implement test case by template
15
features/step_definitions/grocery.js
Test Framework - Cucumber
• Examples
– 5. Run test
• ./node_modules/cucumber/bin/cucumber.js
16
Test Framework
• Mocha or Cucumber?
– Mocha are friendly to programmers
• Focus on test case
– Cucumber are friendly to non-programmers
• Deliver executable spec
17
Assertion Library - Chai.js
• Run on Node.js/Browser
• Provide there ways of assertions
18
Test Double
• What is test double?
– Replace the target object for testing purpose
– Type
• Dummy: passed around but never actually used
• Stub: Provide a canned response to method calls
• Mock: Verifies behavior to a method (Stub + expected)
• Spy: Proxy to real objects and record all kinds of things
• Fake: Business behavior simulators
19
Test Double
• Sinon.js
– spy/stub/mock library
– Installation: npm install sinon
– Spy:
20
Test Double
• Sinon.js
– Stub
– Mock
21

More Related Content

What's hot (20)

PDF
ŠšŠžŠ”Š¢ŠÆŠŠ¢Š˜Š ŠŠŠ¢ŠŠ›Š£Š„Š Ā«Setup and run automated test framework for Android applic...
QADay
Ā 
PPTX
Scripting robot
Chonlasith Jucksriporn
Ā 
PDF
Robot Framework :: Demo login application
Somkiat Puisungnoen
Ā 
PDF
Tests immutable when refactoring - SegFault Unconference Cracow 2019
Grzegorz Miejski
Ā 
PDF
Introduction to Robot Framework
Somkiat Puisungnoen
Ā 
PDF
Developers Testing - Girl Code at bloomon
Ineke Scheffers
Ā 
PDF
Introduction to Test Driven Development
Siva Arunachalam
Ā 
PPT
Robot Framework for beginners and what is new at 2019
Laura Ojala
Ā 
PDF
Jest: Frontend Testing leicht gemacht @EnterJS2018
Holger Grosse-Plankermann
Ā 
PDF
How To Use Selenium Successfully
Dave Haeffner
Ā 
PDF
Robot framework and selenium2 library
krishantha_samaraweera
Ā 
PDF
ŠšŠžŠ”Š¢ŠÆŠŠ¢Š˜Š ŠšŠ›Š®Š„Š’ Ā«Postman: API Automation Testing Swiss Army KnifeĀ» Kyiv QADay...
QADay
Ā 
KEY
WTF TDD?
jeremyw
Ā 
PDF
Workshop - E2e tests with protractor
Walmyr Lima e Silva Filho
Ā 
PPTX
Rails automatic test driven development
tyler4long
Ā 
PDF
Robot Framework Introduction
Pekka KlƤrck
Ā 
PPTX
QA Fes 2016. АлексанГр Єотемской. ŠžŠ±Š·Š¾Ń€ ProtractorJS как фреймворка Š“Š»Ń Š±Ń€Š°ŃƒŠ·...
QAFest
Ā 
PDF
Introduction to logging in django
Siva Arunachalam
Ā 
PPTX
Robot framework
Rochak Bhalla
Ā 
PPTX
QA Fest 2016. Роман Горин. ВвеГение в системы Ń€Š°ŃŠæŠ¾Š·Š½Š°Š²Š°Š½ŠøŃ речи глазами тест...
QAFest
Ā 
ŠšŠžŠ”Š¢ŠÆŠŠ¢Š˜Š ŠŠŠ¢ŠŠ›Š£Š„Š Ā«Setup and run automated test framework for Android applic...
QADay
Ā 
Scripting robot
Chonlasith Jucksriporn
Ā 
Robot Framework :: Demo login application
Somkiat Puisungnoen
Ā 
Tests immutable when refactoring - SegFault Unconference Cracow 2019
Grzegorz Miejski
Ā 
Introduction to Robot Framework
Somkiat Puisungnoen
Ā 
Developers Testing - Girl Code at bloomon
Ineke Scheffers
Ā 
Introduction to Test Driven Development
Siva Arunachalam
Ā 
Robot Framework for beginners and what is new at 2019
Laura Ojala
Ā 
Jest: Frontend Testing leicht gemacht @EnterJS2018
Holger Grosse-Plankermann
Ā 
How To Use Selenium Successfully
Dave Haeffner
Ā 
Robot framework and selenium2 library
krishantha_samaraweera
Ā 
ŠšŠžŠ”Š¢ŠÆŠŠ¢Š˜Š ŠšŠ›Š®Š„Š’ Ā«Postman: API Automation Testing Swiss Army KnifeĀ» Kyiv QADay...
QADay
Ā 
WTF TDD?
jeremyw
Ā 
Workshop - E2e tests with protractor
Walmyr Lima e Silva Filho
Ā 
Rails automatic test driven development
tyler4long
Ā 
Robot Framework Introduction
Pekka KlƤrck
Ā 
QA Fes 2016. АлексанГр Єотемской. ŠžŠ±Š·Š¾Ń€ ProtractorJS как фреймворка Š“Š»Ń Š±Ń€Š°ŃƒŠ·...
QAFest
Ā 
Introduction to logging in django
Siva Arunachalam
Ā 
Robot framework
Rochak Bhalla
Ā 
QA Fest 2016. Роман Горин. ВвеГение в системы Ń€Š°ŃŠæŠ¾Š·Š½Š°Š²Š°Š½ŠøŃ речи глазами тест...
QAFest
Ā 

Similar to How to write test in node.js (20)

PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Ortus Solutions, Corp
Ā 
PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Uma Ghotikar
Ā 
PPTX
From Pilot to Product - Morning@Lohika
Ivan Verhun
Ā 
PDF
Testing - How Vital and How Easy to use
Uma Ghotikar
Ā 
PDF
CNUG TDD June 2014
Mayank Srivastava
Ā 
PPTX
Understanding TDD - theory, practice, techniques and tips.
Malinda Kapuruge
Ā 
PDF
2024 DAPUG Conference Arnaud Bouchez Test Driven Design
Arnaud Bouchez
Ā 
PDF
JS Fest 2019. АлексанГр Єотемский. Дпособы Ń€Š°ŃŠæŠ°Ń€Š°Š»Š»ŠµŠ»ŠøŠ²Š°Š½ŠøŃ тестов в JavaScript
JSFestUA
Ā 
PPTX
Practical unit testing in c & c++
Matt Hargett
Ā 
PPT
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
Ā 
PDF
Selenium Tips & Tricks, presented at the Tel Aviv Selenium Meetup
Dave Haeffner
Ā 
PPTX
Automated Acceptance Testing from Scratch
Excella
Ā 
PPTX
Testing with laravel
Derek Binkley
Ā 
PDF
How To Use Selenium Successfully (Java Edition)
Sauce Labs
Ā 
PDF
Mastering Test Automation: How to Use Selenium Successfully
Applitools
Ā 
PPTX
Community day 2013 applied architectures
Panagiotis Kefalidis
Ā 
PPTX
NodeJS - Server Side JS
Ganesh Kondal
Ā 
PPTX
A Brief Introduction to Test-Driven Development
Shawn Jones
Ā 
PDF
How To Use Selenium Successfully (Java Edition)
Dave Haeffner
Ā 
PPTX
Lean-Agile Development with SharePoint - Bill Ayers
SPC Adriatics
Ā 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Ortus Solutions, Corp
Ā 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Uma Ghotikar
Ā 
From Pilot to Product - Morning@Lohika
Ivan Verhun
Ā 
Testing - How Vital and How Easy to use
Uma Ghotikar
Ā 
CNUG TDD June 2014
Mayank Srivastava
Ā 
Understanding TDD - theory, practice, techniques and tips.
Malinda Kapuruge
Ā 
2024 DAPUG Conference Arnaud Bouchez Test Driven Design
Arnaud Bouchez
Ā 
JS Fest 2019. АлексанГр Єотемский. Дпособы Ń€Š°ŃŠæŠ°Ń€Š°Š»Š»ŠµŠ»ŠøŠ²Š°Š½ŠøŃ тестов в JavaScript
JSFestUA
Ā 
Practical unit testing in c & c++
Matt Hargett
Ā 
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
Ā 
Selenium Tips & Tricks, presented at the Tel Aviv Selenium Meetup
Dave Haeffner
Ā 
Automated Acceptance Testing from Scratch
Excella
Ā 
Testing with laravel
Derek Binkley
Ā 
How To Use Selenium Successfully (Java Edition)
Sauce Labs
Ā 
Mastering Test Automation: How to Use Selenium Successfully
Applitools
Ā 
Community day 2013 applied architectures
Panagiotis Kefalidis
Ā 
NodeJS - Server Side JS
Ganesh Kondal
Ā 
A Brief Introduction to Test-Driven Development
Shawn Jones
Ā 
How To Use Selenium Successfully (Java Edition)
Dave Haeffner
Ā 
Lean-Agile Development with SharePoint - Bill Ayers
SPC Adriatics
Ā 
Ad

Recently uploaded (20)

PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
Ā 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
Ā 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
Ā 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
Ā 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
Ā 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
Ā 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
Ā 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
Ā 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
Ā 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
Ā 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
Ā 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
Ā 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
Ā 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
Ā 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
Ā 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
Ā 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
Ā 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
Ā 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
Ā 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
Ā 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
Ā 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
Ā 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
Ā 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
Ā 
Import Data Form Excel to Tally Services
Tally xperts
Ā 
Human Resources Information System (HRIS)
Amity University, Patna
Ā 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
Ā 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
Ā 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
Ā 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
Ā 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
Ā 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
Ā 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
Ā 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
Ā 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
Ā 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
Ā 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
Ā 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
Ā 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
Ā 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
Ā 
Ad

How to write test in node.js

  • 1. How to write test in Node.js Jason Lin ([email protected]) 2017/08 1
  • 2. Why • Why we need to write test? – Few questions • How do you know the status of the system? • How do you know the code you commit meet the requirement? • How do you know the code you commit won’t break other module? • Side effect? – Problems: Manual test is trivial and time consuming – Solution: write test and make it run automatically 2
  • 3. What • Testing methodologies 3 Test the individual component Test the component groups Test the integrated systems Test the final system Reference: https://www.inflectra.com/ideas/topic/testing-methodologies.aspx
  • 4. What • Testing methodologies – Unit Test • Test on isolated component (public) • Shouldn’t have dependencies (file, db, 3rd Party Lib, …) – Integrate Test • Testing on multiple components • Verify the interaction between components • It may rely on dependencies – System Test • Concerned with the behavior of the full system 4
  • 5. What • Development process – TDD (Test Driven Development) • Write test first • Just meet the requirement • Never over-design 5 Reference: https://www.allaboutcircuits.com/technical-articles/how-test- driven-development-can-help-you-write-better-unit-tests/
  • 6. What • Development process – BDD (Behavior Driven Development) • Develop software correctly and develop correct software • Focused on the business behaviors • Spec as test case – PM and target user wrote stories together – PM and engineers wrote the scenarios together • Story (gherkin syntax) • Scenario (gherkin syntax) 6 As a <type of stakeholder> I want <a feature> So that <some goal can be met> Given <a context> When <an event happens> Then <an outcome should occur>
  • 7. What • Development process – BDD (Behavior Driven Development) • Example 7 Feature: QIoT Backup As an administrator, I want to backup the whole QIoT So that I can restore the system Scenario: Backup QIoT Given the administrator logged in to QIoT When administrator press the backup button Then the backup zip file should be downloaded automatically
  • 8. What • Development process – BDD + TDD 8 Reference: https://www.getzephyr.com/insights/collaboration-backbone-agile- testing
  • 9. How • How can we write test in Node.js? – Test Framework • Mocha / Jasmine / Ava • Cucumber – Assertion Library • Chai – Test Double Library • Sinon / Mockery / Proxyquire 9
  • 10. Test Framework - Mocha • What is Mocha – Run on Node.js/Browser – Supports BDD/TDD – Choose any assertion library – Choose any mocking library – Asynchronous function and promise function support – Highlights slow tests 10
  • 11. Test Framework - Mocha • How to install & run Mocha 11 Testing Result:
  • 12. Test Framework - Mocha • How to write test with Mocha – BDD style (default) 12
  • 13. Test Framework - Mocha • How to write test with Mocha – TDD style 13
  • 14. Test Framework - Cucumber • Features – Support Node.js / Java / Ruby – Deliver executable spec document – Support Gherkin DSL syntax • Example – 1. Installation: npm install cucumber – 2. Wrote feature file 14 features/grocery.feature
  • 15. Test Framework - Cucumber • Examples – 3. Generate template • ./node_modules/cucumber/bin/cucumber.js – 4. Implement test case by template 15 features/step_definitions/grocery.js
  • 16. Test Framework - Cucumber • Examples – 5. Run test • ./node_modules/cucumber/bin/cucumber.js 16
  • 17. Test Framework • Mocha or Cucumber? – Mocha are friendly to programmers • Focus on test case – Cucumber are friendly to non-programmers • Deliver executable spec 17
  • 18. Assertion Library - Chai.js • Run on Node.js/Browser • Provide there ways of assertions 18
  • 19. Test Double • What is test double? – Replace the target object for testing purpose – Type • Dummy: passed around but never actually used • Stub: Provide a canned response to method calls • Mock: Verifies behavior to a method (Stub + expected) • Spy: Proxy to real objects and record all kinds of things • Fake: Business behavior simulators 19
  • 20. Test Double • Sinon.js – spy/stub/mock library – Installation: npm install sinon – Spy: 20
  • 21. Test Double • Sinon.js – Stub – Mock 21