Skip to content

Commit

Permalink
using gulp/mocha/chai. adding code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
skratchdot committed Nov 30, 2015
1 parent e99f076 commit 60bbe53
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 189 deletions.
67 changes: 67 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// adapted from: https://github.com/Khan/style-guides/pull/25
{
"plugins": [
],
"ecmaFeatures": {
"modules": true
},
"env": {
"browser": true,
"node": true,
"mocha": true
},
"rules": {
"arrow-parens": [2, "always"],
"arrow-spacing": 2,
"brace-style": 2,
"camelcase": [2, {
"properties": "always"
}],
"comma-dangle": [1, "always-multiline"],
"comma-dangle": [2, "never"],
"comma-spacing": [2, {
"before": false,
"after": true
}],
"eol-last": [0],
"guard-for-in": 2,
//"indent": [2, 4],
"linebreak-style": [2, "unix"],
/*
"max-len": [2, 80, 4, {
"ignoreUrls": true,
"ignorePattern": "^\\s*const\\s.+=\\s*require\\s*\\("
}],
*/
"no-alert": 2,
"no-array-constructor": 2,
"no-const-assign": 2,
"no-debugger": 2,
"no-dupe-keys": 2,
"no-new-object": 2,
"no-spaced-func": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 2,
"no-unreachable": 2,
"no-unused-vars": 1,
"no-var": 0,
//"object-curly-spacing": [2, "always"],
"one-var": [2, "never"],
"prefer-const": 1,
"prefer-template": 2,
"quotes": [2, "single"],
"semi": [2, "always"],
"space-after-keywords": [2, "always"],
"space-before-blocks": 2,
"space-before-function-paren": [2, {
"anonymous": "always",
"named": "never"
}],
"space-infix-ops": 2,
"space-return-throw-case": 2,
"strict": [0, "never"],
"valid-jsdoc": 2
},
"extends": "eslint:recommended"
}
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
/node_modules/
_ignore/
coverage/
node_modules/
.DS_Store
.npm-debug.log
.project
14 changes: 0 additions & 14 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.git*
_ignore/
test/
.DS_Store
.gitignore
.npm-debug.log
.project
.travis.yml
3 changes: 3 additions & 0 deletions .tonic.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var random_useragent = require('random-useragent');
var result = random_useragent.getRandom(); // gets a random user agent string
console.log(result);
15 changes: 12 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
language: node_js
node_js:
- "0.10"
before_install: npm install -g grunt-cli
install: npm install
- 'stable'
- '4.2'
- '4.1'
- '4.0'
- '0.12'
- '0.11'
- '0.10'
before_script:
- npm install -g gulp
script:
- gulp test
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls --verbose
48 changes: 0 additions & 48 deletions Gruntfile.js

This file was deleted.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
[![NPM version](https://badge.fury.io/js/random-useragent.svg)](http://badge.fury.io/js/random-useragent)
[![Build Status](https://travis-ci.org/skratchdot/random-useragent.png?branch=master)](https://travis-ci.org/skratchdot/random-useragent)
[![Code Climate](https://codeclimate.com/github/skratchdot/random-useragent.png)](https://codeclimate.com/github/skratchdot/random-useragent)
[![Coverage Status](https://coveralls.io/repos/skratchdot/random-useragent/badge.png)](https://coveralls.io/r/skratchdot/random-useragent)
[![Coverage Status](https://coveralls.io/repos/skratchdot/random-useragent/badge.svg?branch=master&service=github)](https://coveralls.io/github/skratchdot/random-useragent?branch=master)
[![Dependency Status](https://david-dm.org/skratchdot/random-useragent.svg)](https://david-dm.org/skratchdot/random-useragent)
[![devDependency Status](https://david-dm.org/skratchdot/random-useragent/dev-status.svg)](https://david-dm.org/skratchdot/random-useragent#info=devDependencies)

[![NPM](https://nodei.co/npm/random-useragent.png)](https://npmjs.org/package/random-useragent)


## Description

Expand All @@ -22,6 +24,8 @@ var random_useragent = require('random-useragent');
random_useragent.getRandom(); // gets a random user agent string
```

- [Live example on Tonic](https://tonicdev.com/npm/random-useragent)


## Documentation

Expand Down
1 change: 1 addition & 0 deletions lib/create-data.js → create-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Copyright (c) 2014 skratchdot
* Licensed under the MIT license.
*/
/*eslint prefer-template:0, no-console:0 */
'use strict';

// config
Expand Down
62 changes: 62 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*eslint no-console: 0 */
'use strict';
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var isparta = require('isparta');
var istanbul = require('gulp-istanbul');
var mocha = require('gulp-mocha');
var files = {
lint: ['*.js'],
src: ['index.js'],
test: ['test.js']
};

gulp.task('lint', function () {
return gulp.src(files.lint)
// eslint() attaches the lint output to the eslint property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format('stylish'))
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});

gulp.task('test', function () {
return gulp.src(files.src)
.pipe(istanbul({
instrumenter: isparta.Instrumenter,
includeUntested: true
}))
// Force `require` to return covered files
.pipe(istanbul.hookRequire())
.on('finish', function () {
gulp.src(files.test, {read: false})
// gulp-mocha needs filepaths so you can't have any plugins before it
.pipe(mocha({
reporter: 'spec'
}))
.on('error', function (err) {
console.error(err.toString());
this.emit('end');
})
.pipe(istanbul.writeReports({
dir: './coverage',
reporters: ['lcov', 'json', 'text-summary']
}));
});
});

gulp.task('watch', function () {
gulp.watch([files.lint], ['lint', 'test']);
});

// setup default task
gulp.task('default', ['lint', 'test', 'watch']);

// handle errors
process.on('uncaughtException', function (e) {
console.error(e);
});
File renamed without changes.
50 changes: 23 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
{
"name": "random-useragent",
"version": "0.2.0",
"description": "Get a random useragent (with an optional filter)",
"version": "0.1.2",
"homepage": "https://github.com/skratchdot/random-useragent",
"author": {
"name": "skratchdot",
"email": "git@skratchdot.com"
"main": "index.js",
"scripts": {
"test": "gulp test"
},
"author": "skratchdot",
"license": "MIT",
"bugs": {
"url": "https://github.com/skratchdot/random-useragent/issues"
},
"homepage": "https://github.com/skratchdot/random-useragent",
"repository": {
"type": "git",
"url": "git://github.com/skratchdot/random-useragent.git"
},
"bugs": {
"url": "https://github.com/skratchdot/random-useragent/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/skratchdot/random-useragent/blob/master/LICENSE-MIT"
}
],
"main": "lib/random-useragent",
"engines": {
"node": ">= 0.10.0"
},
"scripts": {
"test": "grunt nodeunit"
},
"dependencies": {
"random-seed": "^0.2.0"
"random-seed": "^0.3.0"
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.11.2",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-watch": "~0.6.1",
"request": "^2.55.0",
"ua-parser-js": "^0.7.6",
"xml2js": "^0.4.8"
"chai": "^3.4.1",
"coveralls": "^2.11.4",
"gulp": "^3.9.0",
"gulp-eslint": "^1.1.1",
"gulp-istanbul": "^0.10.3",
"gulp-mocha": "^2.2.0",
"isparta": "^4.0.0",
"request": "^2.67.0",
"ua-parser-js": "^0.7.9",
"xml2js": "^0.4.15"
},
"tonicExampleFilename": ".tonic.example.js",
"keywords": [
"random",
"useragent",
Expand All @@ -49,4 +45,4 @@
"web",
"developer"
]
}
}
54 changes: 54 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

var expect = require('chai').expect;
var lib = require('./index.js');
var iterations = 100;

describe('random-useragent', function () {
it('don\'t change data', function () {
var d1;
var d2;
var key = 'someNewKey';
d1 = lib.getAllData();
d1[0][key] = 'foo';
d2 = lib.getAllData();
expect(d1[0].hasOwnProperty(key)).to.equal(true);
expect(d2[0].hasOwnProperty(key)).to.equal(false);
});
it('return values', function () {
expect(lib.getRandom()).to.be.a.string;
expect(lib.getRandomData()).to.be.an.object;
expect(lib.getAll()).to.be.an.array;
expect(lib.getAllData()).to.be.an.array;
});
it('randomization works', function (done) {
var i;
for (i = 0; i < iterations; i++) {
expect(lib.getRandom()).to.be.a.string;
expect(lib.getRandomData()).to.be.an.object;
}
done();
});
it('valid filters work', function () {
var filter = function (item) {
return item.browserName === 'Chrome';
};
expect(lib.getRandom(filter)).to.be.a.string;
expect(lib.getRandomData(filter)).to.be.an.object;
expect(lib.getAll(filter)).to.be.an.array;
expect(lib.getAllData(filter)).to.be.an.array;
expect(lib.getAll(filter)).to.have.length.gt(0);
expect(lib.getAllData(filter)).to.have.length.gt(0);
});
it('invalid filters work', function () {
var filter = function (item) {
return item.browserName === 'Some Fake Browser';
};
expect(lib.getRandom(filter)).to.be.null;
expect(lib.getRandomData(filter)).to.be.null;
expect(lib.getAll(filter)).to.be.an.array;
expect(lib.getAllData(filter)).to.be.an.array;
expect(lib.getAll(filter)).to.have.length(0);
expect(lib.getAllData(filter)).to.have.length(0);
});
});

0 comments on commit 60bbe53

Please sign in to comment.