From e5cf3ad164768243750f28a65a30ff12beef7993 Mon Sep 17 00:00:00 2001 From: mpuittinen Date: Sun, 11 Nov 2018 20:41:55 +0200 Subject: [PATCH] Make testRunner configurable (to prepare for jest runner) --- index.js | 61 +++++++++++-------- templates/test-template.ejs | 5 +- test/mochaPlugin.js | 6 ++ test/mochaRunner.js | 14 +++++ test/test-aws.nodejs8.10-mocha/serverless.yml | 4 ++ test/test-service-options/serverless.yml | 4 ++ 6 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 test/mochaRunner.js diff --git a/index.js b/index.js index c99a1ec..71ff5c2 100644 --- a/index.js +++ b/index.js @@ -13,8 +13,6 @@ const utils = require('./utils'); const BbPromise = require('bluebird'); const yamlEdit = require('yaml-edit'); const execSync = require('child_process').execSync; -const TestRunner = require('./mocha-runner'); -const testRunner = new TestRunner(); const testTemplateFile = path.join('templates', 'test-template.ejs'); const functionTemplateFile = path.join('templates', 'function-template.ejs'); @@ -33,7 +31,8 @@ class mochaPlugin { constructor(serverless, options) { this.serverless = serverless; this.options = options; - + this.testRunner = null; + this.commands = { create: { commands: { @@ -190,6 +189,8 @@ class mochaPlugin { } else { nodeVersion = process.versions; } + this.initRunner(myModule.config); + nodeVersion = nodeVersion.replace(/\.[^.]*$/, ''); if (`nodejs${nodeVersion}` !== inited.provider.runtime) { let errorMsg = `Tests being run with nodejs${nodeVersion}, `; @@ -225,7 +226,7 @@ class mochaPlugin { const testPath = funcs[func].tddPlugin.testPath; if (fse.existsSync(testPath)) { - testRunner.addFile(testPath); + this.testRunner.addFile(testPath); } } }); @@ -246,11 +247,11 @@ class mochaPlugin { } }); } - testRunner.reporter(reporter, reporterOptions); + this.testRunner.reporter(reporter, reporterOptions); } if (myModule.options.grep) { - testRunner.grep(myModule.options.grep); + this.testRunner.grep(myModule.options.grep); } // set the SERVERLESS_TEST_ROOT variable to define root for tests @@ -294,7 +295,7 @@ class mochaPlugin { }); } - testRunner.run(myModule, myModule.options, testFileMap); + this.testRunner.run(myModule, myModule.options, testFileMap); return null; }, error => myModule.serverless.cli.log(error)); @@ -416,6 +417,33 @@ class mochaPlugin { this.serverless.cli.log(`Created function file "${path.join(handlerDir, handlerFile)}"`); return BbPromise.resolve(); } + + initRunner(config) { + if (! config.testFramework ) { + throw(new Error(`Parameter testFramework not set`)); + } + const TestRunner = require(`./${config.testFramework}-runner`); + this.testRunner = new TestRunner(); + return this.testRunner; + } + + getWrapper(modName, modPath, handler) { + let wrapped; + // TODO: make this fetch the data from serverless.yml + + if (process.env.SERVERLESS_MOCHA_PLUGIN_LIVE) { + const mod = initLiveModule(modName); + wrapped = lambdaWrapper.wrap(mod); + } else { + /* eslint-disable global-require */ + const mod = require(process.env.SERVERLESS_TEST_ROOT + modPath); + /* eslint-enable global-require */ + wrapped = lambdaWrapper.wrap(mod, { + handler, + }); + } + return wrapped; + }; createFunction() { this.serverless.cli.log('Generating function...'); @@ -492,7 +520,6 @@ class mochaPlugin { module.exports = mochaPlugin; module.exports.lambdaWrapper = lambdaWrapper; -module.exports.runner = testRunner; const initLiveModule = module.exports.initLiveModule = (modName) => { const functionName = [ @@ -507,21 +534,3 @@ const initLiveModule = module.exports.initLiveModule = (modName) => { }; }; -module.exports.runner = testRunner; -module.exports.getWrapper = (modName, modPath, handler) => { - let wrapped; - // TODO: make this fetch the data from serverless.yml - - if (process.env.SERVERLESS_MOCHA_PLUGIN_LIVE) { - const mod = initLiveModule(modName); - wrapped = lambdaWrapper.wrap(mod); - } else { - /* eslint-disable global-require */ - const mod = require(process.env.SERVERLESS_TEST_ROOT + modPath); - /* eslint-enable global-require */ - wrapped = lambdaWrapper.wrap(mod, { - handler, - }); - } - return wrapped; -}; diff --git a/templates/test-template.ejs b/templates/test-template.ejs index b8a43aa..8bc913b 100644 --- a/templates/test-template.ejs +++ b/templates/test-template.ejs @@ -3,8 +3,9 @@ // tests for <%= functionName %> // Generated by serverless-tdd-plugin -const tddPlugin = require('serverless-tdd-plugin'); -const expect = tddPlugin.runner.getChai().expect; +const TddPlugin = require('serverless-tdd-plugin'); +const tddPlugin = new TddPlugin(); +const expect = tddPlugin.initRunner({testFramework: 'mocha'}).getChai().expect; let wrapped = tddPlugin.getWrapper('<%= functionName %>', '/<%= functionPath %>', '<%= handlerName %>'); describe('<%= functionName %>', () => { diff --git a/test/mochaPlugin.js b/test/mochaPlugin.js index f49f14f..360a399 100644 --- a/test/mochaPlugin.js +++ b/test/mochaPlugin.js @@ -27,4 +27,10 @@ describe('mochaPlugin', () => { 'create:function:create', ]); }); + + it('initRunner returns the test framework runner', () => { + const mochaPlugin = new MochaPlugin({}, {}); + const testRunner = mochaPlugin.initRunner({testFramework: 'mocha'}); + expect(typeof(testRunner)).to.eql('object'); + }); }); diff --git a/test/mochaRunner.js b/test/mochaRunner.js new file mode 100644 index 0000000..2fc800a --- /dev/null +++ b/test/mochaRunner.js @@ -0,0 +1,14 @@ +'use strict'; + +const expect = require('chai').expect; +const path = require('path'); +const fse = require('fs-extra'); +const TestRunner = require('../mocha-runner'); + +describe('mochaRunner', () => { + it('mochaRunner provides chai with getChai', () => { + const testRunner = new TestRunner(); + const chai = testRunner.getChai(); + expect(typeof(chai)).to.eql('object'); + }); +}); diff --git a/test/test-aws.nodejs8.10-mocha/serverless.yml b/test/test-aws.nodejs8.10-mocha/serverless.yml index c7b08d6..5843aa0 100644 --- a/test/test-aws.nodejs8.10-mocha/serverless.yml +++ b/test/test-aws.nodejs8.10-mocha/serverless.yml @@ -87,3 +87,7 @@ functions: plugins: - serverless-tdd-plugin + +custom: + serverless-tdd-plugin: + testFramework: mocha diff --git a/test/test-service-options/serverless.yml b/test/test-service-options/serverless.yml index af9f0e4..84f039a 100644 --- a/test/test-service-options/serverless.yml +++ b/test/test-service-options/serverless.yml @@ -90,3 +90,7 @@ functions: plugins: - serverless-tdd-plugin - serverless-webpack + +custom: + serverless-tdd-plugin: + testFramework: mocha \ No newline at end of file