Skip to main content
Version: 25.x

Rezolvarea problemelor

Uh oh, something went wrong? Use this guide to resolve issues with Jest.

Testele cad şi nu ştii de ce

Try using the debugging support built into Node. Puneţi o declaraţie debugger; în oricare din teste, şi apoi, în proiectul vostru, rulați:

node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here]
or on Windows
node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here]

This will run Jest in a Node process that an external debugger can connect to. Note that the process will pause until the debugger has connected to it.

To debug in Google Chrome (or any Chromium-based browser), open your browser and go to chrome://inspect and click on "Open Dedicated DevTools for Node", which will give you a list of available node instances you can connect to. Click on the address displayed in the terminal (usually something like localhost:9229) after running the above command, and you will be able to debug Jest using Chrome's DevTools.

The Chrome Developer Tools will be displayed, and a breakpoint will be set at the first line of the Jest CLI script (this is done to give you time to open the developer tools and to prevent Jest from executing before you have time to do so). Faceţi clic pe butonul care are simbolul "play" în partea superioară dreaptă a ecranului pentru a continua execuția. Când Jest execută testul care conţine declaraţia debugger, execuția se va opri şi puteţi examina stiva de apeluri şi scope-ul curent.

note

The --runInBand cli option makes sure Jest runs the test in the same process rather than spawning processes for individual tests. În mod normal Jest paralelizează testele pe mai multe procese, dar este dificilă depanarea mai multor procese care rulează în acelaşi timp.

Depanarea în VS Code

There are multiple ways to debug Jest tests with Visual Studio Code's built-in debugger.

Să ataşaţi debugger-ul incorporat, rulaţi testele în felul următor:

node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here]
or on Windows
node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here]

Apoi ataşaţi debugger-ul VS Code folosind următoarea configurare launch.json:

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229
}
]
}

Pentru lansarea automată şi ataşarea la un proces care execută testele, utilizaţi următoarea configuraţie:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

or the following for Windows:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/jest/bin/jest.js",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

Dacă utilizaţi pachetul de la Facebook create-react-app, puteţi depana testele Jest cu următoarea configurație:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug CRA Tests",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
"args": [
"test",
"--runInBand",
"--no-cache",
"--env=jsdom",
"--watchAll=false"
],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

Mai multe informaţii despre depanarea Node pot fi găsite aici.

Depanarea în WebStorm

WebStorm has built-in support for Jest. Read Testing With Jest in WebStorm to learn more.

Probleme de caching

The transform script was changed or Babel was updated and the changes aren't being recognized by Jest?

Încercați din nou cu --no-cache. Jest cache-uie fişierele modulelor transformate pentru a accelera execuția testelor. If you are using your own custom transformer, consider adding a getCacheKey function to it: getCacheKey in Relay.

Promisiuni nerezolvate

În cazul în care o promisiune nu se rezolvă deloc, această eroare ar putea fi aruncată:

- Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.`

Acest lucru este cauzat cel mai frecvent de implementări contradictorii pentru Promise. Consider replacing the global promise implementation with your own, for example global.Promise = jest.requireActual('promise'); and/or consolidate the used Promise libraries to a single one.

If your test is long running, you may want to consider to increase the timeout by calling jest.setTimeout

jest.setTimeout(10000); // 10 second timeout

Probleme cu Watchman

Try running Jest with --no-watchman or set the watchman configuration option to false.

De asemenea, consultaţi depanarea watchman.

Testele sunt extrem de lente pe Docker și/sau pe serverul de Integrare Continuă (CI).

While Jest is most of the time extremely fast on modern multi-core computers with fast SSDs, it may be slow on certain setups as our users have discovered.

Based on the findings, one way to mitigate this issue and improve the speed by up to 50% is to run tests sequentially.

Pentru aceasta puteţi rula teste în acelaşi proces folosind --runInBand:

# Using Jest CLI
jest --runInBand

# Using yarn test (e.g. with create-react-app)
yarn test --runInBand

Another alternative to expediting test execution time on Continuous Integration Servers such as Travis-CI is to set the max worker pool to ~4. În mod specific pe Travis-CI, acest lucru poate reduce timpul de execuție a testelor la jumătate. Notă: Planul gratuit de la Travis CI disponibil pentru proiecte open source include doar 2 nuclee CPU.

# Using Jest CLI
jest --maxWorkers=4

# Using yarn test (e.g. with create-react-app)
yarn test --maxWorkers=4

coveragePathIgnorePatterns seems to not have any effect.

Make sure you are not using the babel-plugin-istanbul plugin. Jest wraps Istanbul, and therefore also tells Istanbul what files to instrument with coverage collection. When using babel-plugin-istanbul, every file that is processed by Babel will have coverage collection code, hence it is not being ignored by coveragePathIgnorePatterns.

Defining Tests

Tests must be defined synchronously for Jest to be able to collect your tests.

As an example to show why this is the case, imagine we wrote a test like so:

// Don't do this it will not work
setTimeout(() => {
it('passes', () => expect(1).toBe(1));
}, 0);

When Jest runs your test to collect the tests it will not find any because we have set the definition to happen asynchronously on the next tick of the event loop. This means when you are using test.each you cannot set the table asynchronously within a beforeEach / beforeAll.

Încă nu ați rezolvat problema?

Consultaţi ajutor.