Skip to content

Testing

Florent DALIGAND edited this page Apr 12, 2017 · 11 revisions

Presentation

As we are in web development world, there have plenty of libraries and framework that do nearly that do you want, but not exactly. Write unit tests during development is a very good practice but can become a nightmare into JavaScript world. Based on this question on stackoverflow, I decided to use sinon-chrome.

Testing Framework

I decided to us Mocha as my testing framework because it seems to be the more used currently.

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.

Mocha needs npm to be installed

Mocha dependencies

NPM

To install all dependencies before launching test, just run npm install. Npm will automatically check the dependencies of the application and install it for you. We need Node.js to use npm*

Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Installing mocha

npm install --save-dev mocha

mocha_install

Assertion Framework

As per testing framework, I decided to use the more trendy assertion framework, Chai.

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

npm install mocha chai --save-dev

chai_install

Chrome mocks

In order to test chrome API out of the browser, we need a "mock" of it.

Mocks for chrome.* extensions api via sinon stubs.

npm install sinon-chrome --save-dev

sinon-chrome_install

Documentation

  • Useful link to calledOnce, calledTwice and so on

Run test

Test script is set-up in package.json file. By default, mocha looks for the glob ./test/*.js and ./test/*.coffee, so you may want to put your tests in test/ folder.

  "scripts": {
    "test": "mocha"
  }

On root folder of the project, type command :

npm test

Run test with web-ext

Following this page, mozilla core developer are working on a tool to facilitate the build, test and release of web extension. Investigation should be done on this tool. And complete test suite of the application will be launched. This issue is particularly helpful to understand unit test with web-extension

Clone this wiki locally