How to migrate from mocha to jest in 15 easy steps - and why


For a long time I looked at the jest testing framework, which has a huge amount of all kinds of tasty chips, one of which is multi-threaded test execution. Provided that I had a project for 5000 unit tests, migration promised to be extremely useful. Next, I will describe 14 simple steps for which I managed to migrate - albeit with some reservations - and what we got as a result. Spoiler - everything turned out very cool.


Baseline


β€” , , . β€” β€” . , , 5000 β€” .


mocha, chai, sinon, rewire, nock, nyc β€” , . , :


1) sandbox, , sinon.sandbox.
2) - β€” . , .
3) sinon.useFakeTimers. . .
4) 13 . , , 13 .



, Jest , . puppeteer , ( ) β€” β€” , . β€” . , β€” , .



- mocha β€” , , . , mocha-parallel-tests , . β€” , , . , .


jest-codemods β€” , , β€” sinon β€” 5000 . .


!


1.


awesome-jest, jest-runner-mocha. " !" β€” , , .


.


npm install --save-dev jest jest-runner-mocha

jest-test.config.js


module.exports = {
    runner: 'jest-runner-mocha',
    testRegex: 'tests/.*test_.*\\.js$',
    maxWorkers: 3
};


node --use_strict ./node_modules/.bin/jest --no-cache --config jest-test.config.js

, , .
, β€” jest. β€” .
β€” β€” 90% , ! , β€” , .


2.


, , . , , β€” , . , , . jest-dot-reporter β€” progress bar , β€” .


CLI config:


    reporters: ['jest-dot-reporter'],

, awesome-jest - . β€” .


3. mocha


β€” , . jest-runner-mocha. , mocha 3.5. β€” 7. , Node 4. ,


  • Node 4 mocha jest, yarn,
  • Node 8 End Of Life
  • ,
  • mocha peerDependency,

. . . peerDependency β€” , .


4. clearMocks


, . β€” β€” . β€” , β€” , .


, , clearMocks . , β€” , . jasmine2. β€” .


β€” setupFilesAfterEnv, β€” clearMocks β€” . , . , clearMocks jest β€” sinon, jest .


:


    setupFilesAfterEnv: [ 'lib/clearMocks.js'],
    clearMocks: true,

lib/clearMocks.js


// const jest = require('jest-mock');
const sinon = require('sinon');

module.exports = {
    clearMocks: () => {
        // jest.clearAllMocks(); ,     ,  
        sinon.sandbox.restore();
    }
};

.
. , β€” .


5.


, - . , , ( ). . , . β€” ! , ? , !


6.


, process 500 exit. - .


process.on('exit', () => process.exit());

, , … , , … slowpoke.jpg.


7.


, , , process unCaughtException. , , β€” 7.0.0 7.0.1.


8.


, β€” , . jest-runner-mocha, jest . , β€” β€” . β€” , , yarn.


9.


, postinstall hook jest-runner-mocha:


> core-js@3.6.4 postinstall 
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

, , . . β€” c 23,528,407 , β€” , , , . , 57 , . . . 12 . , . . . . , , .


β€” .


10.


, β€” . CI , , , . - esprint β€” , .


jest-runner-eslint β€” , , . , . , β€” .


, , β€” . Jest , grunt gulp. , , , . Jest , ...


11. CI


CI , - . , , , . β€” !


12.


- , . - , … , yarn, yarn.lock. . .


. yarn β€” yarn . . ? , β€” .


? , yarn package-lock.json, npm-shrinkwrap.json ( ). β€” , shrinkwrap β€” , shrinkwrap. , , . , , . npm prune --production.


, . , , . , . - . , . ?


13.


, β€” . , . , jest , . , .


14.


, , , β€” , . , .


15.


. , . !



?


  1. CI 3 3 . , , .
  2. . β€” . , .
  3. jest , .

ToDo


:


  1. , . , . , mocha , .
  2. jest β€” . β€” , .
  3. , jest-codemods , β€” , .

Yes, if that, here is a link to my fork runner mocha .
You can put it with the command


npm install --save-dev @kernel-panic/jest-runner-mocha

And in the config register how


runner: '@kernel-panic/jest-runner-mocha',

Source: https://habr.com/ru/post/undefined/


All Articles