KEMBAR78
How to write test in node.js
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

How to write test in node.js

  • 1.
    How to writetest in Node.js Jason Lin (starrlingo@gmail.com) 2017/08 1
  • 2.
    Why • Why weneed 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 Testthe 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 canwe 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 • Mochaor 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 • Whatis 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.