Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest runs 40 times slower than mocha

I've opened a ticket, but it got closed immediately without any information if this is normal and acceptable;

I'll post the question here.


Thinking that this is normal could be one of the biggest mistakes of my career so far. I was tinkering with CodeSandbox and noticed how instantaneous the tests were compared to what I was used to. Set up a basic template project, and here are my results:

{
  "name": "jest-performance-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "mocha"
  },
  "dependencies": {
    "chai": "^4.2.0",
    "jest": "^23.0.0"
  },
  "devDependencies": {
    "mocha": "^7.1.0"
  }
}

jest.config.js

{
  "testRegex": "__tests__/*",
  "testEnvironment": "node",
}

Several dozen duplicated tests with

const {expect} = require('chai')

it ('should run the test', async () => {
  expect (true).to.be.true
})

//...

in each

running jest --watch, making a change and saving:

jest results

running yarn run test --watch (mocha --watch), making a change and saving:

enter image description here

--runInBand makes it even worse.

I get it, I'm stuck with 5y/o Windows laptop on HDD and all that, but 40 times?

I have projects with pretty intricate jest setups, and tried to solve the issue of performance many times over the last year or so.

Is there anything I can do to not switch to mocha/chai? I don't feel like running a single jest test anymore.

like image 774
Ivan Zhoga Avatar asked Mar 11 '20 06:03

Ivan Zhoga


People also ask

Is Jest slower than mocha?

Jest runs 40 times slower than mocha.

Is Jest faster than Jasmine?

We love Jest because it's developer experience is superb, however, on our very large monorepo with ~7000+ test specs, Jest runs about 7 times slower than Jasmine.

Why does Jest take so long to start?

This is mostly startup time because jest reports the test itself running in ~100ms. We would like to get this time down to allow for a better developer experience.


1 Answers

I use both in my projects, and yes, the performance difference is very real.

I use jest only when it offers enhanced support for front-end modules - it is very good for React for example. Otherwise I always use mocha.

jest uses Babel to transpile all TypeScript code - including node_modules when they need to be transpiled - to JavaScript. mocha uses ts-node to directly run TypeScript.

This is the reason for the difference.

like image 107
mmomtchev Avatar answered Sep 20 '22 06:09

mmomtchev