training.shoppinpal.com
  • Introduction
  • 1. The Ideal Workspace
    • The Perfect Machine
      • For Biz Team
      • For Developers
      • For Designers
    • Setup a machine in the cloud
      • Solution
      • Setup box on Azure
        • Create a machine on Azure
        • Test drive your remote machine
        • Setup Dropbox On Azure
      • Setup box on DigitalOcean
        • Setup UI
        • Shared FileSystem
          • Dropbox
            • Use locally developed node modules in another project
          • sshfs
        • Long Running Sessions
      • Feedback
  • 2. Learning Git
    • Static Code Analysis
  • 3. The Backend
    • Use Containers
    • Setup a loopback project
    • Lockdown
    • Build a better mousetrap
    • The abyss stares back
    • Built-in models
    • Extending built-in models
    • Understanding UserModel
    • Boot Scripts
    • Promises
    • Find roles for current user
    • Loopback Console
    • Current User
  • 4. Multi-tenancy With Loopback
    • What is Multi-Tenancy
    • Architecting with Loopback
    • Define scope for Roles
    • Role Resolvers
    • Access Control For Tenants
    • Better Programming with multi-tenancy
  • 5. The Frontend
    • The Browser
    • Unit Testing
      • Motivation behind this blog
      • How to write a test
      • Karma and Jasmin
      • Writing Tests
    • End-2-End Testing
    • Angular 1.x
    • Angular 2
      • Testing
  • 6. ElasticSearch
    • Better Search with NGram
    • NGram with Elasticsearch
    • Fun with Path Hierarchy Tokenizer
    • Working with Mappings and Analyzers
  • 7. Promises
    • What are Promises
    • Promise Implementations
    • Nuances
    • What should we use
  • 8. Learning Docker
    • Docker Swarm
  • 9. Queues & Workers
    • PHP workers in AWS EBS
    • NodeJS workers in AWS EBS
      • SQS Daemon by AWS
      • SQS Daemon in NodeJS
      • SQS polling by worker
    • Gearman
  • 10. Docker
    • Capabilities
  • Appendix
    • Bug in WebStorm deployments
    • The Perfect Terminal
    • Scalable App Deployment with AWS
    • Chrome Tips & Tricks
    • Host your own Gitbook
    • Gitbook Tips & Tricks
    • How to handle support incidents
    • Dev Resources
    • Debug e2e Tests on CircleCI
    • Logging
    • Authentication Principles
    • Mac
    • nvm
    • Unify testing with npm
      • Debugging Mocha
    • Sequence Diagrams
    • Project Sync via IDE
      • SFTP with WebStorm
      • SFTP with Visual Studio
    • Soft Linking
    • NodeJS Profiling
      • How to find node.js performance optimization killers
    • Setup Packer on Azure
Powered by GitBook
On this page
  1. 5. The Frontend
  2. Unit Testing

How to write a test

How to write a test

Everyone has the same question when writing tests for first time.

  • You must break down the logic of your application into small chunks or units and verify that each chunk works perfectly and as desired.

  • In javascript the smallest individual chunk you can test is usually functions. The core feature in application must be verified with accompanying unit tests.

  • Unit testing must be done on the basis of input and output for an individual function so when functions are joined together, your code has a better chance to work as a whole.

  • It is better not to interfere with the operation of a function.

Best practice says you should write your unit test before actual development starts. You must be questioning yourself, how can we possibly do that? The answer is simple: just create an empty unit test for the function, keep updating it when function is updated, and freeze it when final touches are made on that function's development. Now if any newcomers try to make changes in code, they would easily find out about any mistakes if & when the tests break.

Unit tests must be broken down into 3 parts:

  1. Story

  2. Feature

  3. Units

So in our case Story is `Users come to our page and login, if successful they are redirected to Profile page`. In this we state that our feature is user login and we further divide this feature into units as follows:

  1. Ensure invalid emails do not pass.

  2. Ensure valid emails pass the validation.

  3. Ensure path changes on user logged in.

describe("user login form", function() {
    it("Ensure invalid emails do not pass", function() {});
    it("Ensure valid emails pass the validation", function() {});
    it("Ensure path changes on user logged in", function() {});
});
PreviousMotivation behind this blogNextKarma and Jasmin

Last updated 7 years ago