Skip to content

Commit

Permalink
Make testRunner configurable (to prepare for jest runner)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpuittinen committed Nov 11, 2018
1 parent 95627eb commit e5cf3ad
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
61 changes: 35 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -33,7 +31,8 @@ class mochaPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;

this.testRunner = null;

this.commands = {
create: {
commands: {
Expand Down Expand Up @@ -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}, `;
Expand Down Expand Up @@ -225,7 +226,7 @@ class mochaPlugin {
const testPath = funcs[func].tddPlugin.testPath;

if (fse.existsSync(testPath)) {
testRunner.addFile(testPath);
this.testRunner.addFile(testPath);
}
}
});
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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...');
Expand Down Expand Up @@ -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 = [
Expand All @@ -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;
};
5 changes: 3 additions & 2 deletions templates/test-template.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/mochaPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
14 changes: 14 additions & 0 deletions test/mochaRunner.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
4 changes: 4 additions & 0 deletions test/test-aws.nodejs8.10-mocha/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ functions:

plugins:
- serverless-tdd-plugin

custom:
serverless-tdd-plugin:
testFramework: mocha
4 changes: 4 additions & 0 deletions test/test-service-options/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ functions:
plugins:
- serverless-tdd-plugin
- serverless-webpack

custom:
serverless-tdd-plugin:
testFramework: mocha

0 comments on commit e5cf3ad

Please sign in to comment.