diff options
-rw-r--r-- | .travis.yml | 6 | ||||
-rw-r--r-- | karma.conf.js | 12 | ||||
-rw-r--r-- | mocha_test/forever.js | 44 | ||||
-rw-r--r-- | mocha_test/support/is_browser.js | 4 | ||||
-rw-r--r-- | package.json | 13 | ||||
-rwxr-xr-x | test/test-async.js | 44 |
6 files changed, 78 insertions, 45 deletions
diff --git a/.travis.yml b/.travis.yml index e5e14f8..e225b8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,3 +5,9 @@ node_js: - "iojs-v2.1.0" sudo: false after_success: npm run coveralls + +# Needed to run Karma with Firefox on Travis +# http://karma-runner.github.io/0.13/plus/travis.html +before_script: + - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..9e048c3 --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,12 @@ +module.exports = function (config) { + config.set({ + browsers: ['Firefox'], + files: ['mocha_test/*.js'], + frameworks: ['browserify', 'mocha'], + preprocessors: { + 'mocha_test/*.js': ['browserify'] + }, + reporters: ['mocha'], + singleRun: true + }); +} diff --git a/mocha_test/forever.js b/mocha_test/forever.js new file mode 100644 index 0000000..3f03b57 --- /dev/null +++ b/mocha_test/forever.js @@ -0,0 +1,44 @@ +var async = require('../lib/async'); +var expect = require('chai').expect; +var isBrowser = require('./support/is_browser'); + +describe('forever', function(){ + context('function is asynchronous', function(){ + it('executes the function over and over until it yields an error', function(done){ + var counter = 0; + addOne = function (callback) { + counter++; + if (counter === 50) { + return callback('too big!'); + } + async.setImmediate(function () { + callback(); + }); + } + async.forever(addOne, function (err) { + expect(err).to.eql('too big!'); + expect(counter).to.eql(50); + done(); + }); + }) + }); + + context('function is synchronous', function(){ + it('does not cause a stack overflow', function(done){ + if (isBrowser()) return done(); // this will take forever in a browser + var counter = 0; + function addOne(callback) { + counter++; + if (counter === 50000) { // needs to be huge to potentially overflow stack in node + return callback('too big!'); + } + callback(); + } + async.forever(addOne, function (err) { + expect(err).to.eql('too big!'); + expect(counter).to.eql(50000); + done(); + }); + }); + }); +}); diff --git a/mocha_test/support/is_browser.js b/mocha_test/support/is_browser.js new file mode 100644 index 0000000..7c765e2 --- /dev/null +++ b/mocha_test/support/is_browser.js @@ -0,0 +1,4 @@ +module.exports = function() { + return (typeof process === "undefined") || + (process + "" !== "[object process]"); // browserify +} diff --git a/package.json b/package.json index 4028e48..d0021ae 100644 --- a/package.json +++ b/package.json @@ -21,12 +21,19 @@ "devDependencies": { "benchmark": "bestiejs/benchmark.js", "bluebird": "^2.9.32", + "chai": "^3.1.0", "coveralls": "^2.11.2", "es6-promise": "^2.3.0", "jscs": "^1.13.1", "jshint": "~2.8.0", + "karma": "^0.13.2", + "karma-browserify": "^4.2.1", + "karma-firefox-launcher": "^0.1.6", + "karma-mocha": "^0.2.0", + "karma-mocha-reporter": "^1.0.2", "lodash": "^3.9.0", "mkdirp": "~0.5.1", + "mocha": "^2.2.5", "native-promise-only": "^0.8.0-a", "nodeunit": ">0.0.0", "nyc": "^2.1.0", @@ -47,7 +54,11 @@ ] }, "scripts": { - "test": "npm run-script lint && nodeunit test/test-async.js", + "mocha-node-test": "mocha mocha_test/", + "mocha-browser-test": "karma start", + "mocha-test": "npm run mocha-node-test && npm run mocha-browser-test", + "nodeunit-test": "nodeunit test/test-async.js", + "test": "npm run-script lint && npm run nodeunit-test && npm run mocha-test", "lint": "jshint lib/*.js test/*.js perf/*.js && jscs lib/*.js test/*.js perf/*.js", "coverage": "nyc npm test && nyc report", "coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls" diff --git a/test/test-async.js b/test/test-async.js index 8a7ea8d..10290ac 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -85,50 +85,6 @@ function isBrowser() { (process + "" !== "[object process]"); // browserify } -exports['forever'] = { - - 'async': function (test) { - test.expect(2); - var counter = 0; - function addOne(callback) { - counter++; - if (counter === 50) { - return callback('too big!'); - } - async.setImmediate(function () { - callback(); - }); - } - async.forever(addOne, function (err) { - test.equal(err, 'too big!'); - test.equal(counter, 50); - test.done(); - }); -}, - - 'sync': function (test) { - if (isBrowser()) { - // this will take forever in a browser - return test.done(); - } - test.expect(2); - var counter = 0; - function addOne(callback) { - counter++; - if (counter === 50000) { // needs to be huge to potentially overflow stack in node - return callback('too big!'); - } - callback(); - } - async.forever(addOne, function (err) { - test.equal(err, 'too big!'); - test.equal(counter, 50000); - test.done(); - }); -} - -}; - exports['applyEach'] = function (test) { test.expect(5); var call_order = []; |