Cómo migrar de mocha a broma en 15 sencillos pasos, y por qué


Durante mucho tiempo miré el marco de prueba de broma, que tiene una gran cantidad de todo tipo de chips sabrosos, uno de los cuales es la ejecución de prueba de subprocesos múltiples. Siempre que tuviera un proyecto para 5000 pruebas unitarias, la migración prometió ser extremadamente útil. A continuación, describiré 14 pasos simples por los cuales logré migrar, aunque con algunas reservas, y lo que obtuvimos como resultado. Spoiler: todo salió muy bien.


Base


— , , . — — . , , 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 , — , .

Sí, si es así, aquí hay un enlace a mi tenedor moca corredor .
Puedes ponerlo con el comando


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

Y en la configuración registre cómo


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

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


All Articles