Skip to content

Commit

Permalink
fix autoInject tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aearly committed Jul 9, 2018
1 parent 7e28bed commit 17471a5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 86 deletions.
3 changes: 1 addition & 2 deletions .babelrc
@@ -1,6 +1,5 @@
{
"presets": ["es2015"],

"plugins": ["transform-es2015-modules-commonjs"],
"env": {
"test": {
"plugins": ["istanbul"]
Expand Down
23 changes: 14 additions & 9 deletions lib/autoInject.js
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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.");
}

Expand Down
107 changes: 36 additions & 71 deletions test/autoInject.js
@@ -1,3 +1,4 @@
/* eslint prefer-arrow-callback: 0, object-shorthand: 0 */
var async = require('../lib');
var {expect} = require('chai');

Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -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();
});
Expand All @@ -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();
});
});
});
10 changes: 6 additions & 4 deletions test/es2017/asyncFunctions.js
Expand Up @@ -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);
});
});
Expand Down

0 comments on commit 17471a5

Please sign in to comment.