Skip to content

doc(testing): adding new structure #230

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
46 changes: 44 additions & 2 deletions testing/unit-testing-jest/docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
## Whats is testing and why is important?
# Introduction
### General concept and benefits

Essentially, a unit test is a method that instantiates a small portion of our application and checks its behavior independently of other parts.

Testing is a method to check if the code is working as expected. Testing the code helps to detect bugs on time and prevent the appearance of new bugs in the future. Also, testing gives confidence to our code. We can expect that they don't find problems in QA tests.

## Benefits
* Fewer bugs in production
* Fewer code regressions
* More maintainable code
* Faster refactoring and upgrades
* Less dead code
* You can see more on this [article](https://jestjs.io/docs/using-matchers).


### Principles

**Easy to write:** it should be easy to code all of those test routines without enormous effort.

**Readable:** the intent of a unit test should be clear. A good unit test tells a story about some behavioral aspect of our application, so it should be easy to understand which scenario is being tested and — if the test fails — easy to detect how to address the problem.

**Reliable:** unit tests should fail only if there’s a bug in the system under test. Good unit tests should be reproducible and independent from external factors such as the environment or running order.

**Fast:** developers write unit tests so they can repeatedly run them and check that no bugs have been introduced. Slow unit tests may also indicate that either the system under test, or the test itself, interacts with external systems, making it environment-dependent.

> Unit tests do NOT test the interaction with external modules. Everything related to real dependencies is tested in an **integration tests**.

### Unit test structure (AAA pattern)
- Arrange: initializes the objects and establishes the values of the data that we are going to use in the Test that contains it.
- Act: makes the call to the method to be tested with the parameters prepared for this purpose.
- Assert: checks that the executed test method behaves as we expected it to.

````ts
🏷️ describe('Class, Module, Service or Component to test', () => {
//test
🌱️ it('should do ...', () => {
// Arrange
const service = new ServiceToTest();
// Act
const value = service.fn();
// Assert
🔍️️ expect(value)️.🧪toBe('expected value');
});
});
````
🏷 First use ```describe```to define the scope of the tests, this can contain one o more test, inside, define the name of the class that you wanna test, next put a call back ```,() =>```

🌱 Now use ``it`` to define your test, inside describe the specific name of your test, and next put a call back ```,() =>```

🔍️️ The soul of your test is the ```assertion ```, which is defined as ```expect()``` followed by the variable that you want to match.

🧪 Every assertion is paired with a ```matcher```. It allows you to evaluate the result of a variable. You can get all the matcher of Jest on this [link](https://jestjs.io/docs/using-matchers).
29 changes: 0 additions & 29 deletions testing/unit-testing-jest/docs/test-structure.md

This file was deleted.

60 changes: 27 additions & 33 deletions testing/unit-testing-jest/docs/unit-test-jest.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
# Unit test with Jest

## Topics
- Unit test with Jest
- [Whats is testing? and why is important?](./introduction.md#whats-is-testing-and-why-is-important)
- [Benefits](./introduction.md#benefits)
- [Options to test?](./options-to-test.md)
- [Jest](./jest.md)
- [Basic structure of a test](./test-structure.md)
- [Jest hooks](./jest-hooks.md)
- [Repeating setUp](./jest-hooks.md#repeating-setup)
- [One time setUp](./jest-hooks.md#one-time-setup)
- [Angular default test configuration](./angular-configuration.md)
- [Understanding TestBed :test_tube:](./angular-configuration.md#understanding-testbed-test_tube)
- [Fixture and Component :statue_of_liberty:](./angular-configuration.md#fixture-and-component-statue_of_liberty)
- [Init the fixture](./angular-configuration.md#init-the-fixture)
- [Init the component](./angular-configuration.md#init-the-component)
- [fixture.detectChanges()](./angular-configuration.md#fixturedetectchanges)
- [How to spy functions calls](./spy-local.md)
- [Spy local function](./spy-local.md#spy-local-function)
- [Spy external function](./spy-external.md)
- [Router](./spy-external.md#router)
- [ActivatedRouter](./spy-external.md#activatedrouter)
- [Private functions](./spy-external.md#private-functions)
- [How to get elements from the DOM on testing](./getting-dom-elements.md)
- [Getting element](./getting-dom-elements.md#getting-element)
- [Testing a class](./testing-class.md)
- [HttpClientTestingModule](./http-test.md)
- [Issues](./issues.md)
- [i18n dependency](./issues.md#i18n-dependency)
- [Navigator dependency](./issues.md#navigator-dependency)
- [Error with the navigator](./issues.md#error-with-the-navigator)
- [Error with the ChangeDetectionStrategy](./issues.md#error-with-the-changedetectionstrategy)
# Unit test with Jest
### Introduction
1. [General concepts and benefits](./introduction.md#general-concept-and-benefits)
2. [Testing Principles](./introduction.md#principless)
3. [Unit test structure (AAA pattern)](./introduction.md#unit-test-structure-aaa-pattern)
### Angular Testing
1. Test structure.
2. Angular Testing utility APIs (https://angular.io/guide/testing-utility-apis).
3. Testing Modules
### Jest Framework
1. Jest (Benefits and concepts).
2. How to migrate or install Jest in an Angular project? (https://jestjs.io/docs/migration-guide)
3. Jest Hooks.
4. Mock functions and spies.
5. Matchers (https://jestjs.io/docs/using-matchers).
### Testing Angular Projects
1. Class
2. Basic Component
3. Service
4. Interceptors
5. Pipe
6. Directive
7. Routing
8. Guards
### Others
1. Testing private functions
2. Angular Testing Library