4 Node.js bloatware testing frameworks: Ava, Jasmine, Jest, Mocha

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.

Introduction

I wrote a simple function that will be tested by the frameworks:

JS
calculator.js

The source code is available in my GitHub repo.

AVA

Project: https://www.npmjs.com/package/ava

Version: 3.2.0

Dependencies: 52

Used in: 380 npm packages

Source code: 48k uses, 17k stars, 153 issues

Installation:

Shell

Size on disk: 8141 kB, 3002 files, 470 folders

Run tests:

Shell

Sample test:

JS
tests.js

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:

Shell

Size on disk: 37,230 kB, 7211 files, 1174 folders

Run tests:

Shell

Sample test:

JS
test.js

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:

Shell
  • jasmine init creates sample configuration in spec/support/jasmine.json

Size on disk: 524 kB, 108 files, 38 folders

Run tests:

Shell

Sample test:

JS
spec/calc.spec.js

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:

Shell
  • jasmine-core is the subset of Jasmine
  • karma-jasmine is the Karma’s adapter for Jasmine
  • karma init creates configuration in karma.conf.js

Size on disk: 10,165 kB, 2494 files, 306 folders

Run tests:

Shell

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 with requirejs by typing: npm install -D requirejs
  • These packages added 1256 kB, 13 files, 4 folders.
  • Rewrite calculator.js to the RequireJS format by replacing module.exports = divide with define(() => divide)
  • Rewrite the test file by replacing const divide = require('../calculator') with define(['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:

JS
calc.spec.js

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:

Shell

Size on disk: 6439 kB, 2639 files, 221 folders

Run tests:

Shell

Sample test:

JS
test.js

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:

JS

2. Expect style (fluent):

JS

3. Should style (fluent):

JS

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:

Shell

Size on disk: 7317 kB, 2718 files, 233 folders

Run tests:

Shell

Sample test:

JS
testc.js

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:

Shell

Size on disk: 15,990 kB, 2893 files, 270 folders

Run tests:

Shell

Sample test:

JS
testc.js

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:

Shell

Size on disk: 447 kB, 49 files, 31 folders

Run tests:

Shell

Sample test:

JS
testc.js

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

Leave a Reply

avatar
  Subscribe  
Notify of