summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2018-07-08 21:24:13 -0700
committerAlexander Early <alexander.early@gmail.com>2018-07-08 21:24:13 -0700
commit17471a579b0650e54b322120b1e6ca85de0846c5 (patch)
treedcec6c2a14d68920b6aa8e4b14b035afd65e36d1
parent7e28bed76388312dafd29daa2504de12783eb8b9 (diff)
downloadasync-17471a579b0650e54b322120b1e6ca85de0846c5.tar.gz
fix autoInject tests
-rw-r--r--.babelrc3
-rw-r--r--lib/autoInject.js23
-rw-r--r--test/autoInject.js107
-rw-r--r--test/es2017/asyncFunctions.js10
4 files changed, 57 insertions, 86 deletions
diff --git a/.babelrc b/.babelrc
index 3a1b0d4..913eb6a 100644
--- a/.babelrc
+++ b/.babelrc
@@ -1,6 +1,5 @@
{
- "presets": ["es2015"],
-
+ "plugins": ["transform-es2015-modules-commonjs"],
"env": {
"test": {
"plugins": ["istanbul"]
diff --git a/lib/autoInject.js b/lib/autoInject.js
index 935c835..004cfda 100644
--- a/lib/autoInject.js
+++ b/lib/autoInject.js
@@ -2,19 +2,24 @@ import auto from './auto';
import wrapAsync from './internal/wrapAsync';
import { isAsync } from './internal/wrapAsync';
-var FN_ARGS = /^(?:async\s+)?(function)?\s*[^(]*\(\s*([^)]*)\)/m;
+var FN_ARGS = /^(?:async\s+)?(?:function)?\s*[^(]*\(\s*([^)]+)\s*\)(?:\s*{)/m;
+var ARROW_FN_ARGS = /^(?:async\s+)?\(?\s*([^)^=]+)\s*\)?(?:\s*=>)/m;
var FN_ARG_SPLIT = /,/;
var FN_ARG = /(=.+)?(\s*)$/;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
function parseParams(func) {
- func = func.toString().replace(STRIP_COMMENTS, '');
- func = func.match(FN_ARGS)[2].replace(' ', '');
- func = func ? func.split(FN_ARG_SPLIT) : [];
- func = func.map((arg) => {
- return arg.replace(FN_ARG, '').trim();
- });
- return func;
+ const src = func.toString().replace(STRIP_COMMENTS, '');
+ let match = src.match(FN_ARGS)
+ if (!match) {
+ match = src.match(ARROW_FN_ARGS);
+ }
+ if (!match) throw new Error('could not parse args in autoInject\nSource:\n' + src)
+ let [, args] = match
+ return args
+ .replace(/\s/g, '')
+ .split(FN_ARG_SPLIT)
+ .map((arg) => arg.replace(FN_ARG, '').trim());
}
/**
@@ -120,7 +125,7 @@ export default function autoInject(tasks, callback) {
newTasks[key] = taskFn;
} else {
params = parseParams(taskFn);
- if (taskFn.length === 0 && !fnIsAsync && params.length === 0) {
+ if ((taskFn.length === 0 && !fnIsAsync) && params.length === 0) {
throw new Error("autoInject task functions require explicit parameters.");
}
diff --git a/test/autoInject.js b/test/autoInject.js
index 5a35282..bb3270b 100644
--- a/test/autoInject.js
+++ b/test/autoInject.js
@@ -1,3 +1,4 @@
+/* eslint prefer-arrow-callback: 0, object-shorthand: 0 */
var async = require('../lib');
var {expect} = require('chai');
@@ -19,12 +20,12 @@ describe('autoInject', () => {
callback(null, 2);
}, 50);
},
- task3(task2, callback){
+ task3: function (task2, callback){
expect(task2).to.equal(2);
callOrder.push('task3');
callback(null, 3);
},
- task4(task1, task2, callback){
+ task4: function task4(task1, task2, callback){
expect(task1).to.equal(1);
expect(task2).to.equal(2);
callOrder.push('task4');
@@ -44,6 +45,7 @@ describe('autoInject', () => {
}
},
(err, results) => {
+ expect(results).to.eql({task1: 1, task2: 2, task3: 3, task4: 4, task5: 5, task6: 6})
expect(results.task6).to.equal(6);
expect(callOrder).to.eql(['task2','task3','task6','task5','task1','task4']);
done();
@@ -67,7 +69,8 @@ describe('autoInject', () => {
callOrder.push('task3');
cb(null, 3);
}
- }, () => {
+ }, (err, results) => {
+ expect(results).to.eql({task1: 1, task2: 2, task3: 3})
expect(callOrder).to.eql(['task1','task3','task2']);
done();
});
@@ -85,76 +88,38 @@ describe('autoInject', () => {
}, done);
});
- it('should throw error for function without explicit parameters', (done) => {
- try {
- async.autoInject({
- a (){}
- });
- } catch (e) {
- // It's ok. It detected a void function
- return done();
- }
-
- // If didn't catch error, then it's a failed test
- done(true)
+ it('should throw error for function without explicit parameters', () => {
+ expect(() => async.autoInject({
+ a () {}
+ })).to.throw()
});
- var arrowSupport = true;
- try {
- new Function('x => x');
- } catch (e) {
- arrowSupport = false;
- }
-
- if (arrowSupport) {
- // Needs to be run on ES6 only
-
- /* eslint {no-eval: 0}*/
- eval("(function() { " +
- " it('should work with es6 arrow syntax', function (done) { " +
- " async.autoInject({ " +
- " task1: (cb) => cb(null, 1), " +
- " task2: ( task3 , cb ) => cb(null, 2), " +
- " task3: cb => cb(null, 3) " +
- " }, (err, results) => { " +
- " expect(results.task1).to.equal(1); " +
- " expect(results.task3).to.equal(3); " +
- " done(); " +
- " }); " +
- " }); " +
- "}) "
- )();
- }
-
-
- var defaultSupport = true;
- try {
- eval('function x(y = 1){ return y }');
- }catch (e){
- defaultSupport = false;
- }
+ it('should work with es6 arrow syntax', (done) => {
+ async.autoInject({
+ task1: (cb) => cb(null, 1),
+ task2: ( task3 , cb ) => cb(null, 2),
+ task3: cb => cb(null, 3)
+ }, (err, results) => {
+ expect(results.task1).to.equal(1);
+ expect(results.task3).to.equal(3);
+ done();
+ });
+ });
- if(arrowSupport && defaultSupport){
- // Needs to be run on ES6 only
- /* eslint {no-eval: 0}*/
- eval("(function() { " +
- " it('should work with es6 obj method syntax', function (done) { " +
- " async.autoInject({ " +
- " task1 (cb){ cb(null, 1) }, " +
- " task2 ( task3 , cb ) { cb(null, 2) }, " +
- " task3 (cb) { cb(null, 3) }, " +
- " task4 ( task2 , cb ) { cb(null) }, " +
- " task5 ( task4 = 4 , cb ) { cb(null, task4 + 1) } " +
- " }, (err, results) => { " +
- " expect(results.task1).to.equal(1); " +
- " expect(results.task3).to.equal(3); " +
- " expect(results.task4).to.equal(undefined); " +
- " expect(results.task5).to.equal(5); " +
- " done(); " +
- " }); " +
- " }); " +
- "}) "
- )();
- }
+ it('should work with es6 default param syntax', (done) => {
+ async.autoInject({
+ task1 (cb){ cb(null, 1) },
+ task2 ( task3 , cb ) { cb(null, 2) },
+ task3 (cb) { cb(null, 3) },
+ task4 ( task2 , cb ) { cb(null) },
+ task5 ( task4 = 4 , cb ) { cb(null, task4 + 1) }
+ }, (err, results) => {
+ expect(results.task1).to.equal(1);
+ expect(results.task3).to.equal(3);
+ expect(results.task4).to.equal(undefined);
+ expect(results.task5).to.equal(5);
+ done();
+ });
+ });
});
diff --git a/test/es2017/asyncFunctions.js b/test/es2017/asyncFunctions.js
index dddce22..22468f8 100644
--- a/test/es2017/asyncFunctions.js
+++ b/test/es2017/asyncFunctions.js
@@ -300,22 +300,24 @@ module.exports = function () {
});
});
+ /* eslint prefer-arrow-callback: 0, object-shorthand: 0 */
it('should handle async functions in autoInject', (done) => {
async.autoInject({
- async a () {
+ z: async function(){ return 0},
+ a: async function a () {
return await Promise.resolve(1);
},
- async b (a) {
+ b: async function (a) {
return await Promise.resolve(a + 1);
},
c: async (a, b) => {
return await Promise.resolve(a + b);
},
- d: async (c) => {
+ d: async c => {
return await Promise.resolve(c + 1);
}
}, (err, result) => {
- expect(result).to.eql({a: 1, b: 2, c: 3, d: 4});
+ expect(result).to.eql({z: 0, a: 1, b: 2, c: 3, d: 4});
done(err);
});
});