Unit Testing with Javascript

From Training Material
Jump to navigation Jump to search


title
Unit Testing with Javascript Training Course
author
Lukasz Sokolowski


Unit Testing with Javascript

Unit Testing with Javascript Training Materials

Introduction ⌘

  • How Unit Testing Works

Unit test ⌘

  • A function or method, which invokes a unit of module in software
  • Checks assumptions about the system that the developer has in mind
  • Helps the developer test the logical functionality of any module
  • A unit is the testable piece of software
    • It can have more than one input and normally a single output
    • Sometimes, we treat a module of a system as a unit

Unit test con't ⌘

  • Is only relevant to developers who are closely working with the code
  • Is only applicable to test a logical piece of a code
  • Illogical code would not be tested with the use of it
    • Example: getting and setting values in text field
  • Usually, the first unit test is hard to write
    • Requires more time, makes the process more painful
    • We should follow a practice of asking questions before writing
      • Should I use an already available unit test framework or write my own custom code?
      • What about an automated build process?
      • How about collecting, displaying, and tracking unit test code coverage?
  • However, it makes life so much easier than before

Overview of Javascript Unit Test Frameworks ⌘

  • Jasmine, Mocha, Jest, Chai, QUnit
  • Unit test uses cases

Testing tools and frameworks ⌘

  • Karma - the test runner for JavaScript
  • Protractor - the end-to-end testing framework
  • Jasmine - the behavior-driven JavaScript testing framework
  • Jest - no config, supportive, snapshotting, isolated
  • Mocha - the JavaScript testing framework (for nodejs)
  • QUnit - the unit testing framework (for jQuery)
  • Selenium - the tool that automates the web browsers
  • PhantomJS - the headless webkit browser

Tools and frameworks ⌘

  • Help developers write, run, and review unit tests
  • Commonly named as xUnit frameworks
    • Share a set of features across implementations
  • Alternatives for writing single tests for developer/development tools in a browser
  • Provide a lot of the ready-made piece of code
    • test suite/case aggregation, assertions, mock/stub helpers, asynchronous testing implementation, and more

Tools and frameworks con't ⌘

Collecting all the tests into suites and test cases

  • Test suites and test cases are part of many files
    • Each test file typically contains tests for a single module
  • Normally, grouping all tests for a module in one test suite is considered as the best practice
  • The suite can contain many test cases
    • Each test case includes testing of small aspects of any module

Pre and after setup ⌘

  • Built-in functions provided at the suite and test-case levels
  • Help easily handle any pretest setup and post-test teardown
    • setting and resetting the state of a database, initial values for variables, etc
  • Oftenly named
    • beforeEach(): runs before each test
    • afterEach(): runs after each test
    • before(): runs before all tests and executes only once
    • after(): runs after all tests

Setting up the Testing Environment ⌘

  • Downloading and installing the libraries
  • Installing and configuring an IDE
  • Setting up a sample application

Writing Your First Unit Test ⌘

  • Setting up a test
  • Writing a test definition
  • Working with nested scoping
  • Creating assertions
  • Running the unit test

Debugging the unit test ⌘

  • Stepping through the code
  • Creating breakpoints
  • Setting up debug tasks

Expanding the Unit Test ⌘

  • Writing a complex Javascript class
  • Writing stubs and mocks
  • Creating a Test Suite

Implementing TDD and BDD ⌘

  • Creating automated specifications
  • Collaborating with users and testers

Writing More Tests ⌘

  • Testing Node.js
  • HTTP endpoint unit tests
  • Testing a REST APIs
  • Writing an integration test
  • Testing React components (optional)
  • Testing Angular components (optional)

Testing React ⌘

Testing Angular ⌘

Troubleshooting ⌘