Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: doc testing added #84

Merged
merged 8 commits into from
Apr 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 85 additions & 7 deletions doc/TESTING.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,100 @@
# Testing
This chapter should provide the user with all needed information around testing within your project.
This document explains project's testing strategy, including why we write tests and what is our goals are in testing and the tooling library used to write the test cases
mdsign marked this conversation as resolved.
Show resolved Hide resolved

## Content
- [Different kind of tests](#different-kind-of-tests)
- [Tooling](#tooling)
- [Why Do We Write Tests](#why-do-we-write-tests)
- [Our Goal in Testing](#our-goal-in-testing)
- [TDD (Test-driven development)](#tdd-test-driven-development)
- [Tooling](#tooling---testing-libraries-installed)
- [How to install Jest](#how-to-install-jest)
- [How to write tests](#how-to-write-tests)
- [How to run tests](#how-to-run-tests)

## Different kind of tests
mdsign marked this conversation as resolved.
Show resolved Hide resolved
This section should display all the different kinds of tests that you write in your project. Explain what each kind of test is for and why you use them in your project. You can also give an outlook on new tests that should be added in the future of your project. If you have a QA in place, also mention what kind of tests they perform and add contact persons.
In this project Unit Testing is the only type of testing done in the development process to ensure that a section of an application (known as the "unit") meets its design
r-l-d marked this conversation as resolved.
Show resolved Hide resolved
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

Unit Testing - The main objective of unit testing is to isolate written code to test and determine if it works as intended. This testing method is also the first level of software testing, which is performed before other testing methods such as integration testing
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

## Tooling
Please explain what kind of tools you use for testing, why you have choosen them and how to work with them.
In future other testing types like Integration Testing, Performance Testing can be added in the Continuous Integration process.
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

## Why Do We Write Tests
r-l-d marked this conversation as resolved.
Show resolved Hide resolved
We write tests to verify that our software works as intended and to catch bugs and errors before they pushed and make it into production. Testing helps us catch issues early in the development process, which can save us time and resources in the long run. It also helps ensure that our software meets the requirements.

## Our Goal in Testing
Our goal is to write automated unit tests for each module or function in our codebase to ensure that it works as intended and the changes do not introduce regressions. The minimum code coverage of unit tests should be **70%**. To achieve this goal, we follow a comprehensive testing strategy of TDD (Test-driven development) process.
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

## TDD (Test-driven development)
**telemetry-functions** follows TDD (Test-driven development) process, a method of implementing software programming that interlaces unit testing, programming and refactoring on source code, to eusure the quality, reliability, and functionality of the software.

Test-driven development (TDD) is a software development approach where developers write automated tests before writing the code. The idea is to write a test case that fails initially and then write the code to make the test pass. The process is repeated for each new feature or change to the codebase.

## Tooling - Testing Libraries Installed
**Jest** - a JavaScript testing framework is testing library used to write the test cases, It allows you to write tests with an approachable, familiar and feature-rich API that gives you results quickly

## How to install Jest
Install the jest package (and optional typings) to the existing project's package.json by the bellow command
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

npm install --save-dev jest @types/jest
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

When cloning the project the package.json have the library in dev dependency which installs Jest with "npm install" in the project's root directoty.
mdsign marked this conversation as resolved.
Show resolved Hide resolved

## How to write tests
This section should explain how to write different tests on a basic level. Please also include information about your testing strategies. Don't hesitate to use a lot of code exaples and links to further information and external tech documentation if you use .

Jest is configured by default to look for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (this includes files called test or spec).

Our project have __tests__ folders which have *.test.ts files that will contain our test cases and fixtures folder having all the payload mocks used in test cases.
r-l-d marked this conversation as resolved.
Show resolved Hide resolved
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

To write a test case we first define the outcomes that we must validate to ensure that the functionality is working correctly.

- To group test cases in Jest into a test suite we can use the describe() function. It takes a suite name string and handler function as the first two arguments.
r-l-d marked this conversation as resolved.
Show resolved Hide resolved
- In Jest we use the it() function. It takes a test name string and handler function as the first two arguments.
r-l-d marked this conversation as resolved.
Show resolved Hide resolved
- An expectation is created with the expect() function. It returns an object of matcher methods (toMatchObject()) with which we assert something expected about the tested value.
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

Example:
```
// fooBar.test.ts
const fooBar = require("./fooBar");

describe("fooBar", () => {
it("returns 'foo' for multiples of 3", () => {
expect(fooBar(3)).toMatch("foo");
});
});
```

## How to run tests
Here you should provide a guide on how to run the different tests you use. Write down the exact commands to use and where to execute them. You could also use the structure of your set up step-by-step guide from your Contribute page.
To run tests with Jest call the jest command inside the root of the project folder.

The project's package.json is updated with the following test script that calls the jest command:
```
{
// ... package.json contents
"scripts": {
// ... existing scripts
"test": "jest",
"test:coverage": "jest --coverage",
"test:dev": "jest --watch",
}
}
```

To run the Jest testing library

**_npm run test_**
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

In this case, npm run test is defined as jest in the package.json scripts, which will execute the Jest testing library
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

To run the test in development environment

**_npm run test:dev_**
r-l-d marked this conversation as resolved.
Show resolved Hide resolved

This is defined as jest --watch, which will execute Jest in "watch" mode.
mdsign marked this conversation as resolved.
Show resolved Hide resolved

When you run npm run test without any arguments, Jest will run all the test suites once and then exit. However, when you run npm run test:dev, Jest will enter "watch" mode and continuously watch for changes to your test files or the code being tested. When a file changes, Jest will re-run the affected tests. This makes it faster and more convenient to develop and test your code.
mdsign marked this conversation as resolved.
Show resolved Hide resolved

To run the test and to create a test coverage report

**_npm run test:coverage_**
mdsign marked this conversation as resolved.
Show resolved Hide resolved

Also note that you can pass additional arguments to Jest using -- followed by the argument(s) you want to pass. For example, you could run npm run test --coverage to generate a test coverage report.
mdsign marked this conversation as resolved.
Show resolved Hide resolved