> For the complete documentation index, see [llms.txt](https://guide-angular.wishtack.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide-angular.wishtack.io/angular/testing/unit-testing/unit-test-synchrone.md).

# Unit-Test Synchrone

## Ajout d'un Test

Pour ajouter un test, il suffit de créer un fichier avec l'extension `.spec.ts` dans le dossier `src`. Plus exactement, **la convention est de créer ce fichier dans le même dossier que le fichier contenant le code source à tester**.

Il suffit d'utiliser les 3 fonctions suivantes pour implémenter un premier test :

* `describe` : pour définir une suite *(ou groupe)* de "specs".
* `it` : pour définir une "spec" *(ou un test)*.
* `expect` : pour implémenter les assertions.

{% tabs %}
{% tab title="calculator.spec.ts" %}

```typescript
import { Calculator } from './calculator';

describe('Calculator', () => {

    it('should evaluate 2 + 3 + 4 to 9', () => {

        const calculator = new Calculator();

        expect(calculator.evaluate('2 + 3 + 4')).toEqual(9);

    });

});
```

{% endtab %}
{% endtabs %}

![Output Console](/files/-LDM38SuY48b5Bj0XFIr)

![Output HTML](/files/-LDM359Tvb5m7EpwYi7B)

## `beforeEach` & `afterEach`

Comme dans tous les frameworks de tests unitaires, on peut définir des logique de "**setup**" et de "**tear down**" avec respectivement les fonctions `beforeEach` et `afterEach`.

* **`beforeEach`** : permet d'inscrire une fonction de "setup" qui sera appelée avant chaque "spec". Les fonctions de "setup" permettent de **préparer un environnement sain** pour chaque "spec".&#x20;
* **`afterEach`** : permet d'inscrire une fonction de "tear down" qui sera appelée après chaque "spec". Les fonctions de "tear down" permettent de **nettoyer l'environnement** ou encore **exécuter des assertions** pour s'assurer que les "tests" n'ont pas d'effets de bord *(requête HTTP "mocked" non exécutées ou sans réponse)*.

A titre d'exemple, nous pouvons utiliser `beforeEach` afin de **factoriser l'instanciation** de la classe `Calculator` et surtout d'avoir **une nouvelle instance pour chaque "spec"**.

{% tabs %}
{% tab title="calculator.spec.ts" %}

```typescript
import { Calculator } from './calculator';
​
describe('Calculator', () => {

    let calculator: Calculator;

    beforeEach(() => {
        calculator = new Calculator();
    });
​
    it('should evaluate 2 + 3 + 4 to 9', () => {
        expect(calculator.evaluate('2 + 3 + 4')).toEqual(9);​
    });
​
});
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Les fonctions `beforeEach` et `afterEach` peuvent être appelées plus d'une fois afin de définir **plusieurs fonctions de "setup" et de "tear down"**.\
Ces fonctions seront **appelées dans l'ordre** de déclaration.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://guide-angular.wishtack.io/angular/testing/unit-testing/unit-test-synchrone.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
