React ~ Testing - rohit120582sharma/Documentation GitHub Wiki

Introduction

A JavaScript test is simply some code which sets up some state, performs some action, and makes an assertion on the new state.

A test is code that throws an error when the actual result of something does not match the expected output.

The part that says actual !== expected is called an "assertion". The key is that if our assertion fails, then we throw an error.

References

Dependencies



Test Types

Here are three of the most common types of automated tests:

Unit Testing

A unit test focuses on a single “unit of code” – usually a function in an object or module.

A unit test should be isolated from dependencies – for example, no network access and no database access.

There are tools that can replace these dependencies with fakes you can control. This makes it trivial to test all kinds of scenarios that would otherwise require a lot of setup.

A good set of unit tests do not only prevent regressions – bugs that occur repeatedly, but also improve your code design, and make sure you can later refactor your code without everything completely breaking apart.

The basic pieces of a unit test are there: Individual tests, which test one thing, and they are isolated from each other.

Unit Testing comprehensive set of best practices:

  • Actions & Async Action Creators
  • Reducers
  • Components & Connected Components
    • Snapshot testing
    • What does the component render?
    • What props does it receive?
    • What state does it hold?
    • How do these things change during user interaction?
    • What is propTypes defined?
  • Forms

Integration Testing

In integration testing the idea is to test how parts of the system work together – the integration of the parts. Integration tests are similar to unit tests, but there’s one big difference: while unit tests are isolated from other components, integration tests are not.

They might need some set up or configuration, such as the setting up of a test database. This makes writing and maintaining them harder than unit tests.


Functional Testing

Functional testing is also sometimes called E2E testing, or browser testing.

Functional testing is defined as the testing of complete functionality of some application. In practice with web apps, this means using some tool to automate a browser, which is then used to click around on the pages to test the application.



Test Terms

Test runner

A tool that picks up files that contain unit tests, executes them, and writes the test results to the console or log files. Mocha and Jasmine are two popular test runners used within the JavaScript community.

Assertion library

Verifies the results of a test. Chai, Should, and Expect are examples of JavaScript assertion libraries.

Mocks

Used in unit testing a component. A component under test has many dependencies. These dependencies are usually replaced by stubs or mocks. Stubs simulate a dependent object. Mocks offer an additional feature over stubs. With mocks, tests can be written to verify if the component under test has called the mocks as expected.

Mocking library

Facilitates the usage of mocks in unit testing. Sinon and TestDouble are commonly used JavaScript mocking libraries.



⚠️ **GitHub.com Fallback** ⚠️