Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# JS Package Sample [![Build Status](https://travis-ci.org/yupmin/js-package-sample.svg?branch=master)](https://travis-ci.org/yupmin/js-package-sample) [![Coverage Status](https://coveralls.io/repos/yupmin/js-package-sample/badge.svg?branch=master&service=github)](https://coveralls.io/github/yupmin/js-package-sample?branch=master)
# JS Package Sample [![Build Status](https://travis-ci.org/yupmin/js-package-sample.svg?branch=master)](https://travis-ci.org/yupmin/js-package-sample) [![Coverage Status](https://coveralls.io/repos/yupmin/js-package-sample/badge.svg?branch=master&service=github)](https://coveralls.io/github/yupmin/js-package-sample?branch=master) [![Npmjs](https://img.shields.io/npm/v/js-package-sample.svg)](https://www.npmjs.com/package/js-package-sample) [![Npmjs](https://img.shields.io/npm/dt/js-package-sample.svg)](https://www.npmjs.com/package/js-package-sample)

For web component development, sample package

Expand All @@ -11,6 +11,7 @@ For web component development, sample package
- [Chai](http://chaijs.com/)
- [Sinon](http://sinonjs.org/)
- [JSDom](https://github.com/tmpvar/jsdom)
- [Testem](https://github.com/testem/testem)

## Coverage tools
- [istanbul](https://gotwarlost.github.io/istanbul/)
Expand All @@ -29,6 +30,19 @@ grunt [command]
```
### Command
- watch
- test
- build
- clean
- test
- test:dev
- test:node

### Testing using testem
```bash
`npm bin`\testem launchers
`npm bin`\testem
```

### Reference Site
- [http://www.kenpowers.net/blog/testing-in-browsers-and-node/](http://www.kenpowers.net/blog/testing-in-browsers-and-node/)
- [http://alistapart.com/article/writing-testable-javascript](http://alistapart.com/article/writing-testable-javascript)
- [https://github.com/knpwrs/testing-in-browsers-and-node](https://github.com/knpwrs/testing-in-browsers-and-node)
8 changes: 4 additions & 4 deletions dist/js-package-sample.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Copyright (c) 2015
* js-package-sample - Javascript Package Sample
* Built on 2015-12-17
* Built on 2015-12-24
*
* @version 0.0.5
* @link https://github.com/yupmin/js-package-sample.git
Expand All @@ -23,7 +23,7 @@
'use strict';

var userAgent,
repeaterId;
repeater;

return {
getUserAgent: function() {
Expand All @@ -45,11 +45,11 @@
},

getRepeaterId: function() {
return repeaterId;
return repeater;
},

repeaterGetUserAgent: function(cb) {
repeaterId = setInterval(function(self) {
repeater = setInterval(function(self) {
try {
cb(null, self.getUserAgent());
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions dist/js-package-sample.min.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (c) 2015
* js-package-sample - Javascript Package Sample
* Built on 2015-12-17
* Built on 2015-12-24
*
* @version 0.0.5
* @link https://github.com/yupmin/js-package-sample.git
* @license MIT
*/

!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],b):"object"==typeof exports?module.exports=b(require("jquery")):a.JsPackageSample=b(a.jQuery)}(this,function(a){"use strict";var b,c;return{getUserAgent:function(){return b},setUserAgent:function(a){b=a},timerGetUserAgent:function(a){setTimeout(function(b){try{a(null,b.getUserAgent())}catch(c){a(c)}},1e3,this)},getRepeaterId:function(){return c},repeaterGetUserAgent:function(a){c=setInterval(function(b){try{a(null,b.getUserAgent())}catch(c){a(c)}},1e3,this)}}});
!function(e,t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof exports?module.exports=t(require("jquery")):e.JsPackageSample=t(e.jQuery)}(this,function(e){"use strict";var t,n;return{getUserAgent:function(){return t},setUserAgent:function(e){t=e},timerGetUserAgent:function(e){setTimeout(function(t){try{e(null,t.getUserAgent())}catch(n){e(n)}},1e3,this)},getRepeaterId:function(){return n},repeaterGetUserAgent:function(e){n=setInterval(function(t){try{e(null,t.getUserAgent())}catch(n){e(n)}},1e3,this)}}});
36 changes: 17 additions & 19 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ module.exports = function(grunt) {
files: [
{
expand: true,
cwd: 'node_modules/mocha/',
cwd: 'node_modules/mocha',
src: ['mocha.js'],
dest: 'public/assets/js',
filter: 'isFile'
},
{
expand: true,
cwd: 'node_modules/mocha/',
cwd: 'node_modules/mocha',
src: ['mocha.css'],
dest: 'public/assets/css',
filter: 'isFile'
},
{
expand: true,
cwd: 'node_modules/mocha/',
src: ['mocha.*'],
cwd: 'node_modules/jquery/dist',
src: ['jquery.js'],
dest: 'public/assets/js',
filter: 'isFile'
},
{
expand: true,
cwd: 'node_modules/chai/',
cwd: 'node_modules/chai',
src: ['chai.js'],
dest: 'public/assets/js',
filter: 'isFile'
Expand Down Expand Up @@ -85,18 +85,16 @@ module.exports = function(grunt) {
}
}
},
simplemocha: {
options: {
globals: ['expect'],
timeout: 3000,
ignoreLeak: false,
ui: 'bdd',
reporter: 'tap'
},
all: {
src: [
'test/*.js'
]
mochaTest: {
test: {
options: {
timeout: 3000,
ignoreLeak: false,
ui: 'bdd',
require: 'test/setup',
reporter: 'spec'
},
src: ['test/*.test.js']
}
},
watch: {
Expand All @@ -113,7 +111,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-simple-mocha');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
Expand All @@ -124,7 +122,7 @@ module.exports = function(grunt) {

// grunt.registerTask('watch', ['jshint', 'simplemocha']);

grunt.registerTask('test', ['simplemocha']);
grunt.registerTask('test', ['mochaTest']);

grunt.registerTask('build', ['concat', 'uglify']);
};
12 changes: 9 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var fs = require('fs'),
''].join('\n') + '\n';

gulp.task('jshint:dev', function() {
gulp.src(['gulpfile.js', 'tests/**/*.js'])
gulp.src(['gulpfile.js', 'test/**/*.js'])
.pipe(jshint({
maxlen: 80,
quotmark: 'single'
Expand All @@ -40,8 +40,14 @@ gulp.task('jshint:app', function () {
});

gulp.task('test', function() {
gulp.src(['test/**/*.js'])
.pipe(mocha());
gulp.src(['test/*.test.js'])
.pipe(mocha({
timeout: 3000,
ignoreLeak: false,
ui: 'bdd',
require: ['./test/setup'],
reporter: 'spec'
}));
});

gulp.task('copy', function() {
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
"description": "Javascript Package Sample",
"main": "dist/js-package-sample.js",
"scripts": {
"test": "`npm bin`/mocha test",
"test": "`npm bin`/testem ci",
"test:dev": "`npm bin`/testem",
"test:node": "`npm bin`/mocha -r test/setup.js -R tap test/*.test.js",
"test-travis": "`npm bin`/istanbul cover `npm bin`/_mocha -- -R spec ./test/*",
"build": "`npm bin`/grunt",
"clean": "`npm bin`/grunt clean"
},
"keywords": [
"test"
"test",
"package",
"sample"
],
"author": "yupmin@gmail.com",
"license": "MIT",
Expand All @@ -29,7 +33,7 @@
"grunt-contrib-jshint": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-watch": "*",
"grunt-simple-mocha": "*",
"grunt-mocha-test": "*",
"gulp": "*",
"gulp-concat": "*",
"gulp-header": "*",
Expand All @@ -40,7 +44,8 @@
"istanbul": "*",
"jsdom": "*",
"mocha": "*",
"sinon": "*"
"sinon": "*",
"testem": "*"
},
"dependencies": {
"jquery": "*"
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'use strict';

var userAgent,
repeaterId;
repeater;

return {
getUserAgent: function() {
Expand All @@ -35,11 +35,11 @@
},

getRepeaterId: function() {
return repeaterId;
return repeater;
},

repeaterGetUserAgent: function(cb) {
repeaterId = setInterval(function(self) {
repeater = setInterval(function(self) {
try {
cb(null, self.getUserAgent());
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion test.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<script src="public/assets/js/jquery.js"></script>
<script src="src/index.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test/index.js"></script>
<script src="test/setup.js"></script>
<script src="test/index.test.js"></script>
<script>mocha.run();</script>
</body>
</html>
21 changes: 4 additions & 17 deletions test/index.js → test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
var expect;

if (typeof module !== 'undefined' && module.exports) { // Node.js
var sinon = require('sinon'),
jsdom = require('jsdom');

expect = require('chai').expect;

GLOBAL.window = jsdom.jsdom(
'<html><head></head><body>hello world</body></html>'
).defaultView;

var JsPackageSample = require('../src/index');
} else {
expect = chai.expect;
}

describe('JS Package Sample', function() {
it('setUserAgent function', function() {
expect(JsPackageSample.setUserAgent('test')).to.equal(undefined);
Expand All @@ -41,4 +24,8 @@ describe('JS Package Sample', function() {
done();
});
});

it('jQuery has ajax?', function() {
expect(jQuery.ajax).to.not.undefined;
});
});
18 changes: 18 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Export modules to global scope as necessary (only for testing)
if (typeof process !== 'undefined') {
// We are in node. Require modules.
expect = require('chai').expect;
sinon = require('sinon');
jsdom = require('jsdom');
window = jsdom.jsdom(
'<html><head></head><body>hello world</body></html>'
).defaultView;
jQuery = require('jquery');
JsPackageSample = require('..');
isBrowser = false;
} else {
// We are in the browser. Set up variables like above using served js files.
expect = chai.expect;
// num and sinon already exported globally in the browser.
isBrowser = true;
}
15 changes: 15 additions & 0 deletions testem.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
framework: mocha
src_files:
- public/assets/js/chai.js
- public/assets/js/sinon.js
- public/assets/js/jquery.js
- src/index.js
- test/setup.js
- test/*.test.js
launchers:
Node:
command: 'npm run test:node'
protocol: tap
launch_in_dev:
- Node
- Chrome