starting to work on JSVerify & Mocha based unit tests for our JS code base

This commit is contained in:
El RIDO 2017-01-29 14:31:44 +01:00
parent 8029c2819f
commit fdef8bc5be
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
6 changed files with 133 additions and 7 deletions

4
.gitattributes vendored
View File

@ -1,5 +1,9 @@
doc/ export-ignore
tst/ export-ignore
js/.istanbul.yml export-ignore
js/test.js export-ignore
js/mocha-3.2.0.js export-ignore
css/mocha-3.2.0.css export-ignore
.codeclimate.yml export-ignore
.csslintrc export-ignore
.dockerignore export-ignore

1
.gitignore vendored
View File

@ -26,6 +26,7 @@ vendor/**/build_phar.php
composer.lock
# Ignore unit testing logs, api docs and eclipse project files
js/node_modules/
tst/log/
.settings
.buildpath

7
js/.istanbul.yml Normal file
View File

@ -0,0 +1,7 @@
---
instrumentation:
excludes:
- jquery-3.1.1.js
baseline-file: ../tst/log/js-coverage-baseline.json
reporting:
dir: ../tst/log/js-coverage-report

78
js/test.js Normal file
View File

@ -0,0 +1,78 @@
'use strict';
var jsc = require('jsverify');
before(function () {
this.jsdom = require('jsdom-global')();
global.$ = global.jQuery = require('./jquery-3.1.1');
global.sjcl = require('./sjcl-1.0.4');
global.Base64 = require('./base64-2.1.9');
global.RawDeflate = require('./rawdeflate-0.5');
require('./rawinflate-0.3');
require('./privatebin');
})
after(function () {
this.jsdom();
})
describe('helper', function () {
describe('secondsToHuman', function () {
jsc.property('returns an array with a number and a word', 'integer', function (number) {
var result = $.PrivateBin.helper.secondsToHuman(number);
return Array.isArray(result) &&
result.length === 2 &&
result[0] === parseInt(result[0], 10) &&
typeof result[1] === 'string';
});
jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[0] === number;
});
jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[1] === 'second';
});
jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / 60);
});
jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[1] === 'minute';
});
jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
});
jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[1] === 'hour';
});
jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
});
jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[1] === 'day';
});
// max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
});
jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
return $.PrivateBin.helper.secondsToHuman(number)[1] === 'month';
});
});
describe('hashToParameterString', function () {
jsc.property('returns strings', 'dict nestring', function (dict) {
return typeof $.PrivateBin.helper.hashToParameterString(dict) === 'string';
});
});
describe('parameterStringToHash', function () {
jsc.property('returns objects', 'string', function (string) {
return typeof $.PrivateBin.helper.parameterStringToHash(string) === 'object';
});
jsc.property('decodes hashes generated with hashToParameterString', 'dict nestring', function (dict) {
var result = $.PrivateBin.helper.parameterStringToHash($.PrivateBin.helper.hashToParameterString(dict));
// padding for URL shorteners
dict.p = 'p';
return JSON.stringify(result) === JSON.stringify(dict);
});
});
});

View File

@ -1,20 +1,56 @@
Running unit tests
==================
Running PHP unit tests
======================
In order to run these tests, you will need to install the following packages
and its dependencies:
* phpunit
* php-gd
* php-sqlite3
* php-xdebug
* php-xdebug (for code coverage reports)
Example for Debian and Ubuntu:
```sh
$ sudo aptitude install phpunit php-gd php-sqlite php-xdebug
```console
$ sudo apt install phpunit php-gd php-sqlite php-xdebug
```
To run the tests, just change into this directory and run phpunit:
```sh
```console
$ cd PrivateBin/tst
$ phpunit
```
Running JavaScript unit tests
=============================
In order to run these tests, you will need to install the following packages
and its dependencies:
* npm
Then you can use the node package manager to install the latest stable release
of mocha and istanbul (for code coverage reports) globally and jsVerify, jsdom
and jsdom-global locally:
```console
$ npm install -g mocha istanbul
$ cd PrivateBin/js
$ npm install jsverify jsdom jsdom-global
```
Example for Debian and Ubuntu, including steps to allow current user to install
node modules globally:
```console
$ sudo apt install npm
$ sudo mkdir /usr/local/lib/node_modules
$ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
$ ln -s /usr/bin/nodejs /usr/local/bin/node
$ npm install -g mocha istanbul
$ cd PrivateBin/js
$ npm install jsverify jsdom jsdom-global
```
To run the tests, just change into the `js` directory and run istanbul:
```console
$ cd PrivateBin/js
$ istanbul cover _mocha
```

View File

@ -13,7 +13,7 @@
</filter>
<logging>
<log type="coverage-clover" target="log/coverage-clover.xml" />
<log type="coverage-html" target="log/coverage-report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80" />
<log type="coverage-html" target="log/php-coverage-report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80" />
<log type="testdox-html" target="log/testdox.html" />
</logging>
</phpunit>