4 Node.js bloatware testing frameworks: Ava, Jasmine, Jest, Mocha
There are many frameworks for testing a Node.js project. Too many. And most of them require huge dependencies. Check the brief report below to find out.
Table of contents
Introduction
I wrote a simple function that will be tested by the frameworks:
The source code is available in my GitHub repo.
AVA
Project: https://www.npmjs.com/package/ava
Note: I'll soon be sharing short, practical tips on Angular — a good way to pick up something new if you're interested.
Version: 3.2.0
Dependencies: 52
Used in: 380 npm packages
Source code: 48k uses, 17k stars, 153 issues
Installation:
Size on disk: 8141 kB, 3002 files, 470 folders
Run tests:
Sample test:
Jest
Project: https://www.npmjs.com/package/jest
Version: 25.1.0
Dependencies: 3
Used in: 6557 npm packages
Source code: 1.6M uses, 30k stars, 811 issues
Installation:
Size on disk: 37,230 kB, 7211 files, 1174 folders
Run tests:
Sample test:
Jasmine
Project: https://www.npmjs.com/package/jasmine
Version: 3.5.0
Dependencies: 2
Used in: 915 npm packages
Source code: 794k uses, 334 stars, 3 issues
Installation:
jasmine initcreates sample configuration inspec/support/jasmine.json
Size on disk: 524 kB, 108 files, 38 folders
Run tests:
Sample test:
Jasmine + Karma
Karma is a test runner that allows running tests in your browser. It’s useful when testing applications with user interface, like Angular. Karma is usually paired with Jasmine, though it works also with Mocha and other frameworks.
Project: https://www.npmjs.com/package/karma
Version: 4.4.1
Dependencies: 26
Used in: 1029 npm packages
Source code: 1.3M uses, 11k stars, 360 issues
Installation:
jasmine-coreis the subset of Jasminekarma-jasmineis the Karma’s adapter for Jasminekarma initcreates configuration inkarma.conf.js
Size on disk: 10,165 kB, 2494 files, 306 folders
Run tests:
Note: it took me a while to configure standalone Karma. Here are the relevant steps:
- The test will be executed in a browser that does not understand importing files (the test file was supposed to import calculator). One of the solutions is using RequireJS.
- Include RequireJS while configuring Karma.
- It should add (but not install) the package
karma-requirejs. Install it along withrequirejsby typing:npm install -D requirejs - These packages added 1256 kB, 13 files, 4 folders.
- Rewrite
calculator.jsto the RequireJS format by replacingmodule.exports = dividewithdefine(() => divide) - Rewrite the test file by replacing
const divide = require('../calculator')withdefine(['calculator'], divide => { ... }) - Because I didn’t include the chrome launcher for minimal setup, I had to open the test URL (printed in the console) in a browser manually.
Sample test:
Mocha
Project: https://www.npmjs.com/package/mocha
Version: 7.0.1
Dependencies: 24
Used in: 7245 npm packages
Source code: 961k uses, 19k stars, 266 issues
Installation:
Size on disk: 6439 kB, 2639 files, 221 folders
Run tests:
Sample test:
Mocha + Chai
Chai is an assertion library that can be used with many testing frameworks. However, it’s most commonly used with Mocha. Instead of writing assert.equal(foo, 'bar'), one can write in three styles thanks to Chai:
1. Assert style:
2. Expect style (fluent):
3. Should style (fluent):
Project: https://www.npmjs.com/package/chai
Version: 4.2.0
Dependencies: 6
Used in: 6551 npm packages
Source code: 708k uses, 7k stars, 27 issues
Installation:
Size on disk: 7317 kB, 2718 files, 233 folders
Run tests:
Sample test:
Mocha + Chai + Sinon
Sinon is a universal library that provides support for test spies, stubs and mocks. It is compatible with many frameworks. Jasmine and Jest have built-in support for fakes, AVA and Mocha do not, and Sinon is often recommended for Mocha.
Project: https://www.npmjs.com/package/sinon
Version: 8.1.1
Dependencies: 7
Used in: 2228 npm packages
Source code: 301k uses, 8k stars, 30 issues
Installation:
Size on disk: 15,990 kB, 2893 files, 270 folders
Run tests:
Sample test:
Titef
For comparison, I checked one of the simplest test frameworks. It lacks features, the most burdensome is support for testing only one file, but for simple solutions it should be sufficient.
Project: https://www.npmjs.com/package/titef
Version: 2.2.2
Dependencies: 0
Used in: 0 npm packages
Source code: 1 uses, 14 stars, 5 issues
Installation:
Size on disk: 447 kB, 49 files, 31 folders
Run tests:
Sample test:
Summary
Framework | Size [kB] | Files | Popularity npm | Popularity GitHub |
AVA | 8141 | 3002 | 380 | 48k |
Jest | 37230 | 7211 | 6557 | 1.6M |
Jasmine | 524 | 108 | 915 | 794k |
Jasmine + Karma | 10165 | 2494 | 1029 | 1.3M |
Mocha | 6439 | 2639 | 7245 | 961k |
Mocha + Chai | 7317 | 2718 | 6551 | 708k |
Mocha + Chai + Sinon | 15990 | 2893 | 2228 | 301k |
Titef | 447 | 49 | 0 | 1 |