Skip to content

Commit

Permalink
setup test infrastructure (swimlane#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
juristr committed Aug 21, 2016
1 parent db19079 commit 9f241d6
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 1 deletion.
32 changes: 32 additions & 0 deletions config/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* taken from angular2-webpack-starter
*/
var path = require('path');

// Helper functions
var ROOT = path.resolve(__dirname, '..');

function hasProcessFlag(flag) {
return process.argv.join('').indexOf(flag) > -1;
}

function isWebpackDevServer() {
return process.argv[1] && !! (/webpack-dev-server$/.exec(process.argv[1]));
}

function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [ROOT].concat(args));
}

function checkNodeImport(context, request, cb) {
if (!path.isAbsolute(request) && request.charAt(0) !== '.') {
cb(null, 'commonjs ' + request); return;
}
cb();
}

exports.hasProcessFlag = hasProcessFlag;
exports.isWebpackDevServer = isWebpackDevServer;
exports.root = root;
exports.checkNodeImport = checkNodeImport;
70 changes: 70 additions & 0 deletions config/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module.exports = function(config) {
var testWebpackConfig = require('./webpack.test.js');

var configuration = {
basePath: '',

frameworks: ['jasmine'],

// list of files to exclude
exclude: [ ],

/*
* list of files / patterns to load in the browser
*
* we are building the test environment in ./spec-bundle.js
*/
files: [ { pattern: './config/spec-bundle.js', watched: false } ],

preprocessors: { './config/spec-bundle.js': ['coverage', 'webpack', 'sourcemap'] },

// Webpack Config at ./webpack.test.js
webpack: testWebpackConfig,

coverageReporter: {
dir : 'coverage/',
reporters: [
{ type: 'text-summary' },
{ type: 'json' },
{ type: 'html' }
]
},

// Webpack please don't spam the console when running in karma!
webpackServer: { noInfo: true },

reporters: [ 'mocha', 'coverage' ],

// web server port
port: 9876,

colors: true,

/*
* level of logging
* possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
*/
logLevel: config.LOG_INFO,

autoWatch: false,

browsers: [
'Chrome'
],

customLaunchers: {
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},

singleRun: true
};

if(process.env.TRAVIS){
configuration.browsers = ['Chrome_travis_ci'];
}

config.set(configuration);
};
54 changes: 54 additions & 0 deletions config/spec-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Adapted from the angular2-webpack-starter kit.
*
* This file creates a bundle for testing, since tests are also
* written in ES6/Typescript. It's the entry point loaded by webpack.test.js
*/

Error.stackTraceLimit = Infinity;

require('core-js/es6');
require('core-js/es7/reflect');

// Typescript emit helpers polyfill
require('ts-helpers');

require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('zone.js/dist/sync-test');

// RxJS
require('rxjs/Rx');

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.TestBed.initTestEnvironment(
browser.BrowserDynamicTestingModule,
browser.platformBrowserDynamicTesting());

/*
* Ok, this is kinda crazy. We can use the the context method on
* require that webpack created in order to tell webpack
* what files we actually want to require or import.
* Below, context will be an function/object with file names as keys.
* using that regex we are saying look in ./src/app and ./test then find
* any file that ends with spec.js and get its path. By passing in true
* we say do this recursively
*/
var testContext = require.context('../src', true, /\.spec\.ts/);

/*
* get all the files, for each file, call the context function
* that will require the file and load it up here. Context will
* loop and require those spec files here
*/
function requireAll(requireContext) {
return requireContext.keys().map(requireContext);
}

// requires and returns all modules that match
var modules = requireAll(testContext);
108 changes: 108 additions & 0 deletions config/webpack.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Adapted from angular2-webpack-starter
*/

const helpers = require('./helpers');

/**
* Webpack Plugins
*/
const ProvidePlugin = require('webpack/lib/ProvidePlugin');
const DefinePlugin = require('webpack/lib/DefinePlugin');

const ENV = process.env.ENV = process.env.NODE_ENV = 'test';

module.exports = {

/**
* Source map for Karma from the help of karma-sourcemap-loader & karma-webpack
*
* Do not change, leave as is or it wont work.
* See: https://github.com/webpack/karma-webpack#source-maps
*/
devtool: 'inline-source-map',


resolve: {
extensions: ['', '.ts', '.js'],
root: helpers.root('src')
},

module: {

preLoaders: [
{
test: /\.ts$/,
loader: 'tslint-loader',
exclude: [helpers.root('node_modules')]
},
{
test: /\.js$/,
loader: 'source-map-loader',
exclude: [
// these packages have problems with their sourcemaps
helpers.root('node_modules/rxjs'),
helpers.root('node_modules/@angular')
]
}
],

loaders: [
{
test: /\.ts$/,
loader: 'awesome-typescript-loader',
query: {
compilerOptions: {

// Remove TypeScript helpers to be injected
// below by DefinePlugin
removeComments: true
}
},
exclude: [/\.e2e\.ts$/]
},

{
test: /\.scss$/,
loaders: ['style', 'css?sourceMap', 'postcss?sourceMap', 'sass?sourceMap']
}
],

postLoaders: [
{
test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader',
include: helpers.root('src'),
exclude: [
/\.(e2e|spec)\.ts$/,
/node_modules/
]
}
]
},

plugins: [
new DefinePlugin({
'ENV': JSON.stringify(ENV),
'process.env': {
'ENV': JSON.stringify(ENV)
}
})
],

tslint: {
emitErrors: false,
failOnHint: false,
resourcePath: 'src'
},


node: {
global: 'window',
process: false,
crypto: 'empty',
module: false,
clearImmediate: false,
setImmediate: false
}

};
9 changes: 9 additions & 0 deletions demo/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import '../src/viz.scss';
<h3>Bar chart</h3>
<h4>Vertical</h4>
<bar-vertical
[view]="[700,200]"
[scheme]="{domain: ['#5AA454', '#A10A28', '#C7B42C', '#AAAAAA']}"
[results]="barData">
</bar-vertical>
<hr />
<bar-vertical
[view]="[700,200]"
[scheme]="{domain: ['#5AA454', '#A10A28', '#C7B42C', '#AAAAAA']}"
Expand Down
2 changes: 2 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Look in ./config for karma.conf.js
module.exports = require('./config/karma.conf.js');
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --progress --watch --inline",
"test": "karma start",
"test:watch": "npm run test -- --auto-watch --no-single-run",
"build": "webpack --progress --profile"
},
"repository": {
Expand Down Expand Up @@ -42,13 +44,23 @@
},
"devDependencies": {
"@types/core-js": "0.9.29",
"@types/jasmine": "^2.2.29",
"angular2-template-loader": "0.4.0",
"autoprefixer": "6.4.0",
"awesome-typescript-loader": "2.1.1",
"clean-webpack-plugin": "^0.1.10",
"codelyzer": "0.0.26",
"css-loader": "0.23.1",
"html-webpack-plugin": "2.22.0",
"istanbul-instrumenter-loader": "^0.2.0",
"jasmine-core": "^2.4.1",
"karma": "^1.2.0",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"karma-mocha-reporter": "^2.1.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.8.0",
"node-sass": "3.8.0",
"postcss-loader": "0.9.1",
"raw-loader": "0.5.1",
Expand All @@ -60,7 +72,7 @@
"tslint-loader": "2.1.5",
"typescript": "2.0.0",
"webpack": "2.1.0-beta.20",
"webpack-dev-server": "1.14.1",
"webpack-dev-server": "^2.1.0-beta.0",
"webpack-notifier": "^1.3.0"
}
}
17 changes: 17 additions & 0 deletions src/A2D3Module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="../node_modules/@types/jasmine/index.d.ts" />

import { A2D3Module } from './A2D3Module';

/*
This file is to import the main module. By importing it into this
spec file, all the attached components get traversed and recognized
in the code coverage stats.
*/

describe('A2D3 Module', () => {

it('should load', () => {
expect(A2D3Module).toBeDefined();
});

});
Loading

0 comments on commit 9f241d6

Please sign in to comment.