diff --git a/README.md b/README.md index f17338a..d225dff 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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/) @@ -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) diff --git a/dist/js-package-sample.js b/dist/js-package-sample.js index b6cfc33..1a93cf2 100644 --- a/dist/js-package-sample.js +++ b/dist/js-package-sample.js @@ -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 @@ -23,7 +23,7 @@ 'use strict'; var userAgent, - repeaterId; + repeater; return { getUserAgent: function() { @@ -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) { diff --git a/dist/js-package-sample.min.js b/dist/js-package-sample.min.js index 1d69dfc..c346cef 100644 --- a/dist/js-package-sample.min.js +++ b/dist/js-package-sample.min.js @@ -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)}}}); \ No newline at end of file +!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)}}}); \ No newline at end of file diff --git a/gruntfile.js b/gruntfile.js index f3415b5..e197572 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -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' @@ -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: { @@ -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'); @@ -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']); }; diff --git a/gulpfile.js b/gulpfile.js index 57fb674..dbc0883 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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' @@ -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() { diff --git a/package.json b/package.json index 4684c36..5973b87 100644 --- a/package.json +++ b/package.json @@ -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", @@ -29,7 +33,7 @@ "grunt-contrib-jshint": "*", "grunt-contrib-uglify": "*", "grunt-contrib-watch": "*", - "grunt-simple-mocha": "*", + "grunt-mocha-test": "*", "gulp": "*", "gulp-concat": "*", "gulp-header": "*", @@ -40,7 +44,8 @@ "istanbul": "*", "jsdom": "*", "mocha": "*", - "sinon": "*" + "sinon": "*", + "testem": "*" }, "dependencies": { "jquery": "*" diff --git a/src/index.js b/src/index.js index 72eb89e..e3ba284 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,7 @@ 'use strict'; var userAgent, - repeaterId; + repeater; return { getUserAgent: function() { @@ -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) { diff --git a/test.html b/test.html index 87d8ff8..88f3e45 100644 --- a/test.html +++ b/test.html @@ -15,7 +15,8 @@ - + + diff --git a/test/index.js b/test/index.test.js similarity index 69% rename from test/index.js rename to test/index.test.js index c0e2c89..97c538f 100644 --- a/test/index.js +++ b/test/index.test.js @@ -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( - 'hello world' - ).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); @@ -41,4 +24,8 @@ describe('JS Package Sample', function() { done(); }); }); + + it('jQuery has ajax?', function() { + expect(jQuery.ajax).to.not.undefined; + }); }); diff --git a/test/setup.js b/test/setup.js new file mode 100644 index 0000000..fb7e2d2 --- /dev/null +++ b/test/setup.js @@ -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( + 'hello world' + ).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; +} diff --git a/testem.yml b/testem.yml new file mode 100644 index 0000000..ddd9d60 --- /dev/null +++ b/testem.yml @@ -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