Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

test(utils|ms|*): test existing units

Ariel Mashrakib70ef7574739
diff --git a/lib/utils.js b/lib/utils.js
index f300cd95..d3efc42b 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -58,7 +58,7 @@ exports.forEach = function(arr, fn, scope){
exports.map = function(arr, fn, scope){
var result = [];
for (var i = 0, l = arr.length; i < l; i++)
- result.push(fn.call(scope, arr[i], i));
+ result.push(fn.call(scope, arr[i], i, arr));
return result;
};
diff --git a/test/ms.js b/test/ms.js
new file mode 100644
index 00000000..1fd9740a
--- /dev/null
+++ b/test/ms.js
@@ -0,0 +1,82 @@
+'use strict';
+var ms = require('../lib/ms');
+
+describe('.ms()', function() {
+ // Helpers
+ var time = {
+ minutes: function(n) { return n * 60 * 1000; },
+ hours: function (n) { return n * this.minutes(60); },
+ days: function(n) { return n * this.hours(24); },
+ years: function(n) { return n * this.days(365.25); }
+ };
+ describe('get a value that less than 1 second', function() {
+ it('should return milliseconds representation', function() {
+ ms(200).should.equal('200ms');
+ ms(30).should.equal('30ms');
+ ms(2000).should.not.equal('2000ms');
+ });
+ });
+
+ describe('seconds representation', function() {
+ it('should return short format', function() {
+ ms(2000).should.equal('2s');
+ });
+
+ it('should return long format', function() {
+ ms(2000, { long: true }).should.equal('2 seconds');
+ ms(1000, { long: true }).should.equal('1 second');
+ ms(1010, { long: true }).should.equal('1 second');
+ });
+ });
+
+ describe('minutess representation', function() {
+ it('should return short format', function() {
+ ms(time.minutes(1)).should.equal('1m');
+ });
+
+ it('should return long format', function() {
+ ms(time.minutes(1), { long: true }).should.equal('1 minute');
+ ms(time.minutes(3), { long: true }).should.equal('3 minutes');
+ });
+ });
+
+ describe('hours representation', function() {
+ it('should return short format', function() {
+ ms(time.hours(1)).should.equal('1h');
+ });
+
+ it('should return long format', function() {
+ ms(time.hours(1), { long: true }).should.equal('1 hour');
+ ms(time.hours(3), { long: true }).should.equal('3 hours');
+ });
+ });
+
+ describe('days representation', function() {
+ it('should return short format', function() {
+ ms(time.days(1)).should.equal('1d');
+ });
+
+ it('should return long format', function() {
+ ms(time.days(1), { long: true }).should.equal('1 day');
+ ms(time.days(3), { long: true }).should.equal('3 days');
+ });
+ });
+
+ describe('Getting string value', function() {
+ it('should return the milliseconds representation(Number)', function() {
+ ms('1 second').should.equal(1000);
+
+ ms('1 minute').should.equal(time.minutes(1));
+ ms('6 minutes').should.equal(time.minutes(6));
+
+ ms('1 hour').should.equal(time.hours(1));
+ ms('5 hours').should.equal(time.hours(5));
+
+ ms('1 day').should.equal(time.days(1));
+ ms('3 days').should.equal(time.days(3));
+
+ ms('1 year').should.equal(time.years(1));
+ ms('2 years').should.equal(time.years(2));
+ });
+ });
+});
diff --git a/test/reporters/json.js b/test/reporters/json.js
index f071e754..e7b89557 100644
--- a/test/reporters/json.js
+++ b/test/reporters/json.js
@@ -13,7 +13,7 @@ describe('json reporter', function(){
suite = new Suite('JSON suite', 'root');
runner = new Runner(suite);
var mochaReporter = new mocha._reporter(runner);
- })
+ });
it('should have 1 test failure', function(done){
var testTitle = 'json test 1';
@@ -37,7 +37,7 @@ describe('json reporter', function(){
done();
});
- })
+ });
it('should have 1 test pending', function(done) {
var testTitle = 'json test 1';
@@ -58,4 +58,4 @@ describe('json reporter', function(){
});
})
-})
+});
diff --git a/test/reporters/nyan.js b/test/reporters/nyan.js
index 8a544589..e574808d 100644
--- a/test/reporters/nyan.js
+++ b/test/reporters/nyan.js
@@ -27,4 +27,4 @@ describe('nyan face', function () {
nyanCat.face.call(nyanCat).should.equal('( - .-)');
done();
});
-})
+});
diff --git a/test/utils.js b/test/utils.js
index 3810c777..f60b42fc 100644
--- a/test/utils.js
+++ b/test/utils.js
@@ -1,26 +1,67 @@
var mocha = require('..');
var utils = mocha.utils;
-var clean = utils.clean;
-var isBuffer = utils.isBuffer;
describe('utils', function() {
- describe('.clean()', function(){
- it('should remove the wrapping function declaration', function(){
+ describe('.clean()', function() {
+ var clean = utils.clean;
+ it('should remove the wrapping function declaration', function() {
clean('function (one, two, three) {\n//code\n}').should.equal('//code');
});
- it('should remove space character indentation from the function body', function(){
+ it('should remove space character indentation from the function body', function() {
clean(' //line1\n //line2').should.equal('//line1\n //line2');
});
- it('should remove tab character indentation from the function body', function(){
+ it('should remove tab character indentation from the function body', function() {
clean('\t//line1\n\t\t//line2').should.equal('//line1\n\t//line2');
});
});
- describe('.isBuffer()', function(){
+
+ describe('.isBuffer()', function() {
+ var isBuffer = utils.isBuffer;
it('should test if object is a Buffer', function() {
isBuffer(new Buffer([0x01])).should.equal(true);
isBuffer({}).should.equal(false);
})
});
+
+ describe('.map()', function() {
+ var map = utils.map;
+ it('should behave same as Array.prototype.map', function() {
+ var arr = [1, 2, 3];
+ map(arr, JSON.stringify).should.eql(arr.map(JSON.stringify));
+ });
+
+ it('should call the callback with 3 arguments[currentValue, index, array]', function() {
+ var index = 0;
+ map([1, 2, 3], function(e, i, arr) {
+ e.should.equal(arr[index]);
+ i.should.equal(index++);
+ })
+ });
+
+ it('should apply with the given scope', function() {
+ var scope = {};
+ map(['a', 'b', 'c'], function() {
+ this.should.equal(scope);
+ }, scope);
+ });
+ });
+
+ describe('.parseQuery()', function() {
+ var parseQuery = utils.parseQuery;
+ it('should get queryString and return key-value object', function() {
+ parseQuery('?foo=1&bar=2&baz=3').should.eql({
+ foo: 1,
+ bar: 2,
+ baz: 3
+ });
+
+ parseQuery('?r1=^@(?!.*\\)$)&r2=m{2}&r3=^co.*').should.eql({
+ r1: '^@(?!.*\\)$)',
+ r2: 'm{2}',
+ r3: '^co.*'
+ });
+ })
+ });
});