Getting Started
Install Jest using yarn:
yarn add --dev jest
Or npm:
npm install --save-dev jest
Note: Jest documentation uses yarn commands, but npm will also work. You can compare yarn and npm commands in the yarn docs, here.
Let's get started by writing a test for a hypothetical function that adds two numbers. First, create a sum.js file:
function sum(a, b) {
return a + b;
}
module.exports = sum;
Then, create a file named sum.test.js. This will contain our actual test:
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Adăugați secțiunea următoare în fișierul vostru package.json:
{
"scripts": {
"test": "jest"
}
}
Finally, run yarn test or npm run test and Jest will print this message:
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)
Tocmai ați scris cu succes primul test folosind Jest!
Testul acesta a folosit expect și toBe pentru a testa dacă două valori sunt identice. Pentru a afla mai multe lucruri pe care Jest le poate testa, vezi Utilizarea regulilor de potrivire.
Rularea din linia de comandă
You can run Jest directly from the CLI (if it's globally available in your PATH, e.g. by yarn global add jest or npm install jest --global) with a variety of useful options.
Iată cum să executaţi Jest pe fişierele my-test, folosind config.json ca fişier de configurare şi afişarea unei notificări native de sistem după rulare:
jest my-test --notify --config=config.json
If you'd like to learn more about running jest through the command line, take a look at the Jest CLI Options page.
Informații suplimentare
Folosind Babel
Pentru a utiliza Babel, instalaţi pachetele babel-jest şi regenerator-runtime:
yarn add --dev babel-jest babel-core regenerator-runtime
Note: If you are using Babel version 7 then you need to install
babel-jest,babel-core@^7.0.0-bridge.0and@babel/corewith the following command:
yarn add --dev babel-jest babel-core@^7.0.0-bridge.0 @babel/core regenerator-runtime
You will need to use `babel.config.js` in order to transpile `node_modules`. See https://babeljs.io/docs/en/next/config-files for more information.
You can also see the example in the Jest repository: https://github.com/facebook/jest/tree/54f4d4ebd3d1a11d65962169f493ce41efdd784f/examples/babel-7
*Notă: Instalarea în mod explicit a `regenerator-runtime` nu este necesară în cazul în care utilizaţi `npm` 3 sau 4 sau Yarn*
Nu uitaţi să adăugaţi fişierul [ `.babelrc`](https://babeljs.io/docs/usage/babelrc/) în directorul rădăcină din proiectul vostru. For example, if you are using ES6 and [React.js](https://facebook.github.io/react/) with the [`babel-preset-env`](https://babeljs.io/docs/plugins/preset-env/) and [`babel-preset-react`](https://babeljs.io/docs/plugins/preset-react/) presets:
```json
{
"presets": ["env", "react"]
}
Acum totul e pregătit pentru a folosi toate caracteristicile ES6 şi sintaxa specifică React.
Note: If you are using a more complicated Babel configuration, using Babel's
envoption, keep in mind that Jest will automatically defineNODE_ENVastest. Nu va folosi sectiuneadevelopmentcum setează Babel în mod implicit, atunci când nu este setat explicitNODE_ENV.Note: If you've turned off transpilation of ES6 modules with the option
{ "modules": false }, you have to make sure to turn this on in your test environment.
{
"presets": [["env", {"modules": false}], "react"],
"env": {
"test": {
"presets": [["env"], "react"]
}
}
}
Notă:
babel-jestse instalează automat când instalaţi Jest şi va transforma automat fişierele în cazul în care o configuraţie Babel există în proiectul vostru. Pentru a evita acest comportament, aveţi posibilitatea să reiniţializaţi explicit opţiunea de configuraretransform:
// package.json
{
"jest": {
"transform": {}
}
}
Folosind Webpack
Jest poate fi utilizat în proiecte care folosesc webpack pentru gestionarea fișierelor statice, stiluri şi compilare. webpack oferă unele provocări unice față de alte instrumente. Urmați ghidul webpack pentru a începe.
Using parcel
Jest can be used in projects that use parcel-bundler to manage assets, styles, and compilation similar to webpack. Parcel requires zero configuration. Refer to the official docs to get started.
Folosind TypeScript
Pentru a utiliza TypeScript în teste utilizaţi ts-jest.
You may also want to install the @types/jest module for the version of Jest you're using. This will help provide full typing when writing your tests with TypeScript.
For
@types/*modules it's recommended to try to match the version of the associated module. For example, if you are using26.4.0ofjestthen using26.4.xof@types/jestis ideal. In general, try to match the major (26) and minor (4) version as closely as possible.
yarn add --dev @types/jest