From 17471a579b0650e54b322120b1e6ca85de0846c5 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sun, 8 Jul 2018 21:24:13 -0700 Subject: [PATCH] fix autoInject tests --- .babelrc | 3 +- lib/autoInject.js | 23 +++++--- test/autoInject.js | 107 ++++++++++++---------------------- test/es2017/asyncFunctions.js | 10 ++-- 4 files changed, 57 insertions(+), 86 deletions(-) diff --git a/.babelrc b/.babelrc index 3a1b0d44a..913eb6aef 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 935c83536..004cfdadd 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 5a3528214..bb3270b0b 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 dddce22d7..22468f815 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); }); });