Skip to content

Commit

Permalink
Configure Karma as an option for test environment
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandosouza committed Jul 25, 2017
1 parent 35efc6b commit 0e8a47f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 52 deletions.
21 changes: 12 additions & 9 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ module.exports = yeoman.generators.Base.extend({
return true;
}
},
{
type: 'list',
name: 'testEnviroment',
message: 'Which test enviroment do you want to use?',
choices: ['Karma', 'Jest'],
default: 'Jest'
},
{
type: 'confirm',
name: 'isNodeModule',
message: 'Is this component supposed to run on node environment? (that is, should other modules be able to "require" and use it?)',
default: false
},
{
type: 'confirm',
name: 'defaultKarmaConfig',
message: 'Do you want to use the default karma configuration? (if so, the karma.conf.js file won\'t be generated, since the gulp tasks will handle the config)'
},
{
type: 'input',
name: 'repoOwner',
Expand Down Expand Up @@ -103,7 +105,7 @@ module.exports = yeoman.generators.Base.extend({
this.capitalizeName = _.startCase(componentName);
this.kebabCaseName = _.kebabCase(componentName);

this.defaultKarmaConfig = props.defaultKarmaConfig;
this.testEnviroment = props.testEnviroment;
this.isNodeModule = props.isNodeModule;
this.repoName = 'metal-' + this.kebabCaseName;
this.repoOwner = props.repoOwner;
Expand Down Expand Up @@ -159,13 +161,14 @@ module.exports = yeoman.generators.Base.extend({
this.fs.copyTpl(
this.templatePath('__tests__/_Boilerplate.js'), this.destinationPath('__tests__/' + this.componentName + '.js'),
{
componentName: this.componentName
componentName: this.componentName,
testEnviroment: this.testEnviroment
}
);
this.fs.copy(
this.templatePath('__tests__/jshintrc'), this.destinationPath('__tests__/.jshintrc')
);
if (!this.defaultKarmaConfig) {
if (this.testEnviroment === 'Karma') {
this.fs.copy(
this.templatePath('_karma.conf.js'), this.destinationPath('karma.conf.js')
);
Expand All @@ -188,7 +191,7 @@ module.exports = yeoman.generators.Base.extend({
{
buildFormat: this.buildFormat,
componentName: this.componentName,
defaultKarmaConfig: this.defaultKarmaConfig,
testEnviroment: this.testEnviroment,
repoName: this.repoName,
repoOwner: this.repoOwner,
repoDescription: this.repoDescription,
Expand Down
5 changes: 3 additions & 2 deletions app/templates/__tests__/_Boilerplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import <%= componentName %> from '../src/<%= componentName %>';

describe('<%= componentName %>', function() {
it('should be tested', function() {
expect('No tests for this module yet.').toBe('Everything is ok.');
it('should be tested', function() {<% if (testEnviroment === 'Jest') { %>
expect('No tests for this module yet.').toBe('Everything is ok.');<% } else { %>
assert.fail('No tests for this module yet.');<% } %>
});
});
59 changes: 37 additions & 22 deletions app/templates/_karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
'use strict';

var babelOptions = {
presets: ['metal'],
sourceMap: 'both'
};
const extractTextPlugin = require("extract-text-webpack-plugin");
const karmaWebpack = require('karma-webpack');
const sourceMapLoader = require("karma-sourcemap-loader");
const metalKarmaConfig = require("metal-karma-config");

module.exports = function (config) {
config.set({
frameworks: ['mocha', 'chai', 'source-map-support', 'commonjs'],

files: [
'node_modules/metal-soy-bundle/build/bundle.js',
'node_modules/html2incdom/src/*.js',
'node_modules/metal*/src/**/*.js',
'src/**/*.js',
'test/**/*.js'
metalKarmaConfig(config);
config.plugins.push(karmaWebpack, sourceMapLoader);
config.set({
frameworks: ['mocha', 'chai', 'sinon'],

files: [
'__tests__/**/*.js'
],

preprocessors: {
'src/**/*.js': ['babel', 'commonjs'],
'node_modules/html2incdom/src/*.js': ['babel', 'commonjs'],
'node_modules/metal-soy-bundle/build/bundle.js': ['commonjs'],
'node_modules/metal*/src/**/*.js': ['babel', 'commonjs'],
'test/**/*.js': ['babel', 'commonjs']
webpack: {
module: {
rules: [
{
test: /\.scss$/,
use: extractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
}
]
},
plugins: [
new extractTextPlugin("styles.css"),
]
},

webpackMiddleware: {
noInfo: true
},

preprocessors: {
'__tests__/**/*.js': ['webpack', 'sourcemap']
},

browsers: ['Chrome'],
singleRun: true,

babelPreprocessor: {options: babelOptions}
});
browsers: ['Chrome']
});
};
31 changes: 12 additions & 19 deletions app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"prepublish": "npm run compile",<% } %><% if (templateLanguage === 'Soy') { %>
"soy": "metalsoy --soyDeps node_modules/metal*/src/**/*.soy node_modules/clay*/src/**/*.soy",<% } %>
"start": "http-server . -p 4000",<% if (templateLanguage === 'Soy') { %>
"test": "npm run soy && jest"<% } else { %>
"test": "jest"<% } %>
"test": "npm run soy && <% if (testEnviroment === 'Karma') { %>karma start<% } else { %>jest<% } %>"<% } else { %>
"test": "<% if (testEnviroment === 'Karma') { %>karma start<% } else { %>jest<% } %>"<% } %>
},
"keywords": [
"metal"
Expand All @@ -50,27 +50,20 @@
"babel-core": "^6.25.0",
"babel-loader": "^7.0.0",
"babel-plugin-transform-node-env-inline": "^0.1.1",
"babel-preset-metal": "^4.0.0",
<% if (templateLanguage === 'JSX') { %>
"babel-preset-metal-jsx": "^0.0.2",<% } %><% if (!defaultKarmaConfig) { %>
"chai": "^2.3.0",<% } %><% if (superClass === 'Component') { %>
"css-loader": "^0.28.4"<% } %>,
"babel-preset-metal": "^4.0.0",<% if (templateLanguage === 'JSX') { %>
"babel-preset-metal-jsx": "^0.0.2",<% } %><% if (superClass === 'Component') { %>
"css-loader": "^0.28.4"<% } %>,<% if (testEnviroment === 'Karma') { %>
"extract-text-webpack-plugin": "^3.0.0",<% } %>
"http-server": "^0.9.0"<% if (superClass === 'Component') { %>,
"identity-obj-proxy": "^3.0.0"<% } %>,
"jest": "^20.0.4",<% if (buildFormat === 'jquery') { %>
"jquery": "^2.2.0",<% } %>
"jquery": "^2.2.0",<% } %><% if (testEnviroment === 'Karma') { %>
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.4",<% } else { %>
"metal-lerna-jest-resolver": "^1.0.0",
"metal-jest-serializer": "^1.0.1",<% if (buildFormat === 'jquery') { %>
"metal-jquery-adapter": "^2.0.0",<% } %><% if (!defaultKarmaConfig) { %>
"karma": "^0.13.9",
"karma-babel-preprocessor": "^6.0.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^0.2.0",
"karma-commonjs": "0.0.13",
"karma-coverage": "^0.5.1",
"karma-mocha": "^0.2.0",
"karma-source-map-support": "1.0.0",
"mocha": "^2.2.5"<% } %><% if (templateLanguage === 'Soy') { %>,
"metal-jest-serializer": "^1.0.1"<% } %><% if (buildFormat === 'jquery') { %>,
"metal-jquery-adapter": "^2.0.0",<% } %><% if (testEnviroment === 'Karma') { %>
"metal-karma-config": "^2.3.1"<% } %><% if (templateLanguage === 'Soy') { %>,
"metal-tools-soy": "^4.2.1"<% } %><% if (superClass === 'Component') { %>,
"node-sass": "^4.5.3",
"sass-loader": "^6.0.6",
Expand Down

0 comments on commit 0e8a47f

Please sign in to comment.