The Web Dev Guide by Wishtack
  • The Web Dev Guide by Wishtack
  • HTML
    • HTML Tags
    • HTML Attributes
    • Content Formatting
    • Empty Tags vs Content Tags
    • Some Links
  • ECMAScript
    • Some History
    • Language Properties
    • Single-Threaded thus Asynchronous
    • The Event Loop
    • Classes
    • Hoisting is Dead: var vs. let vs. const
    • this & "binding"
    • Arrow Functions
    • Template Strings
    • Syntactic Sugar
      • Spread
      • Destructuring
      • Rest
      • Object Literal Property Value Shorthand
    • Named Parameters
    • Compatibility
  • Tools
    • Node.js
    • NPM
    • Yarn
    • Webpack
    • WebStorm
    • StackBlitz
  • DOM
    • What Is It?
    • Element Selection
    • Element Modification
    • Events
  • Forms
    • The <form> tag
    • Form elements
    • Form validation
  • Networking
    • Fetch Web API
  • CSS
    • Selectors
    • Transforms
    • Transitions
    • Animations
    • Web Animations API
    • Sass
  • Responsive Web Design
    • Viewport
    • Media Queries
    • Grid Layout
    • Flex Layout
    • Frameworks & Libraries
  • Web APIs
  • Testing
    • Unit Testing
    • End to End Testing
  • Security
    • Injection
    • DOM XSS
    • Insecure Direct Object Reference
    • Cross-Site Request Forgery
    • Client vs API Validation
    • API Unauthorized Access and Data Leak
  • More Links
Powered by GitBook
On this page
  • JavaScript Unit-Test Frameworks
  • Jasmine
  • Mocha
  • Jest
  • Jasmine
  • Example
  • Spies
  • Fetch Mock
  1. Testing

Unit Testing

PreviousTestingNextEnd to End Testing

Last updated 6 years ago

Unit-testing is not optional if you care about quality and your personal health.

Unit testing improves teams efficiency, applications quality and human lifespan.

Without automated tests:

  • every change is a challenge,

  • updating dependencies is too risky,

  • refactoring is inacceptable,

  • deployment is chaos,

  • ...

Modularity is unit testing's best friend.

JavaScript Unit-Test Frameworks

Jasmine

Mocha

Jest

Jasmine

Example

describe('priceScrapper', () => {

    let priceScrapper;
    
    beforeEach(() => priceScrapper = new PriceScrapper());

    it('should scrap prices without currency', () => {

        expect(priceScrapper.scrap('10.01')).toEqual({
            coefficient: 1001,
            currency: null,
            exponent: -2
        });

    });

    it('should scrap prices with currency', () => {

        expect(priceScrapper.scrap('$10.01')).toEqual({
            coefficient: 1001,
            currency: 'USD',
            exponent: -2
        });

    });

});

Spies

Jasmine spies are mocks.

describe('SearchEngine', () => {

    it('should pass locale to third party api', () => {

        /* Spying on `thirdPartySearchApi.search` and faking result. */
        spyOn(thirdPartySearchApi, 'search').and.returnValue([
            {
                title: 'Wishtack - Making Your Wishes Come True',
                url: 'https://www.wishtack.com'
            }
        ]);

        /* Trigger search. */
        searchEngine.search({keywords: 'Wishtack'});

        /* Check spy's call count. */
        expect(thirdPartySearchApi.search.callCount).toBe(1);

        /* Check spy's call args. */
        expect(thirdPartySearchApi.search).toHaveBeenCalledWith({
            country: 'US',
            keywords: 'Wishtack',
            language: 'en'
        });

    });

});

Fetch Mock

If you are using fetch for HTTP requests, you can use fetch-mock to mock these http requests.

https://mochajs.org/
https://jestjs.io/
http://www.wheresrhys.co.uk/fetch-mock/quickstart
https://jasmine.github.io/
Jasmine Documentation
Logo
Mocha - the fun, simple, flexible JavaScript test framework
Logo
Jest
http://www.wheresrhys.co.uk/fetch-mock/quickstartwww.wheresrhys.co.uk
Logo