Skip to content

Commit 4f7c84a

Browse files
committedJul 19, 2017
feat(Tests): Minimal working configuration for Unit Tests and coverage with Karma, TypeScript and WebPack
1 parent d10600a commit 4f7c84a

7 files changed

+131
-107
lines changed
 

‎jsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
// See https://go.microsoft.com/fwlink/?LinkId=759670
33
// for the documentation about the jsconfig.json format
4+
"sourceMap": true,
45
"compilerOptions": {
56
"target": "es6",
67
"module": "commonjs",

‎karma.conf.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
// Karma configuration
2-
// Generated on Wed Feb 01 2017 12:12:42 GMT+0100 (Paris, Madrid)
31
var webpackConf = require('./webpack.config.js');
4-
delete webpackConf.entry
5-
62
module.exports = function (config) {
73
config.set({
8-
basePath: './src',
9-
frameworks: ['jasmine', 'karma-typescript'],
10-
files: [{ pattern: './**/*.spec.ts', watched: false }],
11-
preprocessors: {
12-
'index.ts': ['webpack', 'karma-typescript', 'coverage'],
13-
'**/*.spec.ts': ['webpack', 'karma-typescript', 'coverage']
4+
basePath:'',
5+
frameworks: ['jasmine'],
6+
files: [{ pattern: './spec-bundle.js', watched: false }],
7+
preprocessors: { './spec-bundle.js': ['coverage', 'webpack', 'sourcemap'] },
8+
webpack: {
9+
module: webpackConf.module,
10+
resolve: webpackConf.resolve
1411
},
15-
webpack: webpackConf, // Pass your webpack.config.js file's content
1612
webpackMiddleware: {
1713
noInfo: true,
1814
stats: 'errors-only'
@@ -21,7 +17,7 @@ module.exports = function (config) {
2117
// optionally, configure the reporter
2218
coverageReporter: {
2319
// specify a common output directory
24-
dir: 'build/reports/coverage',
20+
dir: './tests/build/reports/coverage',
2521
reporters: [
2622
// reporters not supporting the `file` property
2723
{ type: 'html', subdir: 'report-html' },
@@ -54,12 +50,12 @@ module.exports = function (config) {
5450
logLevel: config.LOG_INFO,
5551
autoWatch: true,
5652
browsers: ['Chrome'],
57-
singleRun: false,
53+
singleRun: true,
5854
concurrency: Infinity
5955
});
6056

57+
6158
if (process.env.TRAVIS) {
6259
config.browsers = ['Chrome_travis_ci'];
63-
config.singleRun = true;
6460
}
6561
};

‎package.json

+27-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"start": "npm run server:dev",
88
"server": "npm run server:dev",
9-
"server:dev": "webpack-dashboard -- webpack-dev-server --config ./webpack.config.js --port 3000 --host 0.0.0.0 --hot --inline --progress --profile --watch --content-base dist/",
9+
"server:dev": "webpack-dashboard -- webpack-dev-server --config ./webpack.config.js --inline --progress --watch",
1010
"server:prod": "cross-env NODE_ENV=production webpack-dashboard -- webpack-dev-server --config ./webpack.config.js --port 3000 --host 0.0.0.0 --hot --inline --progress --profile --watch --content-base dist/",
1111
"build": "npm run build:dev",
1212
"build:dev": "webpack --config ./webpack.config.js --progress --profile --color --display-error-details --display-cached",
@@ -21,38 +21,38 @@
2121
"author": "yrenaudin",
2222
"license": "ISC",
2323
"devDependencies": {
24-
"@types/jasmine": "^2.5.41",
25-
"@types/lodash": "^4.14.50",
26-
"cross-env": "^3.1.4",
27-
"css-loader": "^0.26.1",
24+
"@types/jasmine": "^2.5.53",
25+
"@types/lodash": "^4.14.70",
26+
"awesome-typescript-loader": "^3.2.1",
27+
"cross-env": "^5.0.1",
28+
"css-loader": "^0.28.4",
2829
"ejs-loader": "^0.3.0",
29-
"eslint": "^3.14.0",
30-
"expose-loader": "^0.7.1",
31-
"html-loader": "^0.4.4",
32-
"html-webpack-plugin": "^2.26.0",
33-
"jasmine": "^2.5.3",
34-
"jasmine-core": "^2.5.2",
35-
"karma": "^1.4.1",
30+
"eslint": "^4.2.0",
31+
"expose-loader": "^0.7.3",
32+
"html-loader": "^0.4.5",
33+
"html-webpack-plugin": "^2.29.0",
34+
"jasmine": "^2.6.0",
35+
"jasmine-core": "^2.6.4",
36+
"karma": "^1.7.0",
3637
"karma-babel-preprocessor": "^6.0.1",
37-
"karma-chrome-launcher": "^2.0.0",
38+
"karma-chrome-launcher": "^2.2.0",
3839
"karma-coverage": "^1.1.1",
39-
"karma-firefox-launcher": "^1.0.0",
40+
"karma-firefox-launcher": "^1.0.1",
4041
"karma-jasmine": "^1.1.0",
4142
"karma-jasmine-html-reporter": "^0.2.2",
42-
"karma-phantomjs-launcher": "^1.0.2",
43+
"karma-phantomjs-launcher": "^1.0.4",
4344
"karma-sourcemap-loader": "^0.3.7",
44-
"karma-spec-reporter": "0.0.26",
45-
"karma-typescript": "^2.1.7",
46-
"karma-webpack": "^2.0.2",
47-
"rimraf": "^2.5.4",
48-
"style-loader": "^0.13.1",
49-
"ts-loader": "^2.0.0",
50-
"tslint": "^4.3.1",
51-
"tslint-loader": "^3.3.0",
52-
"typescript": "^2.1.5",
53-
"webpack": "^2.2.1",
54-
"webpack-dashboard": "^0.2.1",
55-
"webpack-dev-server": "2.2.0"
45+
"karma-spec-reporter": "0.0.31",
46+
"karma-webpack": "^2.0.4",
47+
"rimraf": "^2.6.1",
48+
"source-map-loader": "^0.2.1",
49+
"style-loader": "^0.18.2",
50+
"tslint": "^5.5.0",
51+
"tslint-loader": "^3.5.3",
52+
"typescript": "^2.4.1",
53+
"webpack": "^3.3.0",
54+
"webpack-dashboard": "^0.4.0",
55+
"webpack-dev-server": "2.5.1"
5656
},
5757
"dependencies": {
5858
"lodash": "^4.17.4"

‎spec-bundle.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Error.stackTraceLimit = Infinity;
2+
3+
var testContext = require.context('./src', true, /\.spec\.ts/);
4+
5+
function requireAll(requireContext) {
6+
return requireContext.keys().map(requireContext);
7+
}
8+
9+
var modules = requireAll(testContext);

‎src/index.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
describe('TypeScript WebPack Starter Tests', ()=> {
2-
it('A good way to start building an awesome lib is by doing Unit Tests 👌🏽', ()=> {
1+
describe('TypeScript WebPack Starter Tests', () => {
2+
it('A good way to start building an awesome lib is by doing Unit Tests 👌🏽', () => {
33
expect(true).toBe(true);
4-
})
4+
});
55
})

‎tsconfig.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
{
22
"compilerOptions": {
33
"target": "es5",
4-
"module": "es6",
5-
"moduleResolution": "node",
6-
"sourceMap": true
4+
"module": "commonjs",
5+
"sourceMap": true,
6+
"emitDecoratorMetadata": true,
7+
"experimentalDecorators": true,
8+
"lib": [
9+
"es2015",
10+
"dom"
11+
],
12+
"noImplicitAny": true,
13+
"suppressImplicitAnyIndexErrors": true
714
},
815
"exclude": [
916
"node_modules",

‎webpack.config.js

+71-60
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,77 @@
1-
const webpack = require('webpack');
2-
const path = require('path');
3-
const HtmlWebpackPlugin = require('html-webpack-plugin');
4-
const DashboardPlugin = require('webpack-dashboard/plugin');
5-
const nodeEnv = process.env.NODE_ENV || 'development';
6-
const isProd = nodeEnv === 'production';
1+
const webpack = require("webpack");
2+
const path = require("path");
3+
const HtmlWebpackPlugin = require("html-webpack-plugin");
4+
const DashboardPlugin = require("webpack-dashboard/plugin");
5+
const nodeEnv = process.env.NODE_ENV || "development";
6+
const isProd = nodeEnv === "production";
77

88
var config = {
9-
devtool: isProd ? 'hidden-source-map' : 'cheap-eval-source-map',
10-
context: path.resolve('./src'),
11-
entry: {
12-
app: './index.ts',
13-
vendor: './vendor.ts'
14-
},
15-
output: {
16-
path: path.resolve('./dist'),
17-
filename: '[name].bundle.js',
18-
sourceMapFilename: '[name].map',
19-
devtoolModuleFilenameTemplate: function (info) {
20-
return "file:///" + info.absoluteResourcePath;
21-
}
22-
},
23-
module: {
24-
rules: [
25-
{ enforce: 'pre', test: /\.ts$/, exclude: ["node_modules"], loader: 'ts-loader' },
26-
{ test: /\.html$/, loader: "html" },
27-
{ test: /\.css$/, loaders: ['style', 'css'] }
28-
]
29-
},
30-
resolve: {
31-
extensions: [".ts", ".js"],
32-
modules: [path.resolve('./src'), 'node_modules']
33-
},
34-
plugins: [
35-
new webpack.DefinePlugin({
36-
'process.env': { // eslint-disable-line quote-props
37-
NODE_ENV: JSON.stringify(nodeEnv)
38-
}
39-
}),
40-
new HtmlWebpackPlugin({
41-
title: 'Typescript Webpack Starter',
42-
template: '!!ejs-loader!src/index.html'
43-
}),
44-
new webpack.optimize.CommonsChunkPlugin({
45-
name: 'vendor',
46-
minChunks: Infinity,
47-
filename: 'vendor.bundle.js'
48-
}),
49-
new webpack.optimize.UglifyJsPlugin({
50-
compress: { warnings: false },
51-
output: { comments: false },
52-
sourceMap: false
53-
}),
54-
new DashboardPlugin(),
55-
new webpack.LoaderOptionsPlugin({
56-
options: {
57-
tslint: {
58-
emitErrors: true,
59-
failOnHint: true
60-
}
61-
}
62-
})
9+
devtool: isProd ? "hidden-source-map" : "source-map",
10+
context: path.resolve("./src"),
11+
entry: {
12+
app: "./index.ts",
13+
vendor: "./vendor.ts"
14+
},
15+
output: {
16+
path: path.resolve("./dist"),
17+
filename: "[name].bundle.js",
18+
sourceMapFilename: "[name].bundle.map",
19+
devtoolModuleFilenameTemplate: function (info) {
20+
return "file:///" + info.absoluteResourcePath;
21+
}
22+
},
23+
module: {
24+
rules: [
25+
{
26+
enforce: "pre",
27+
test: /\.ts?$/,
28+
exclude: ["node_modules"],
29+
use: ["awesome-typescript-loader", "source-map-loader"]
30+
},
31+
{ test: /\.html$/, loader: "html-loader" },
32+
{ test: /\.css$/, loaders: ["style-loader", "css-loader"] }
6333
]
34+
},
35+
resolve: {
36+
extensions: [".ts", ".js"]
37+
},
38+
plugins: [
39+
new webpack.DefinePlugin({
40+
"process.env": {
41+
// eslint-disable-line quote-props
42+
NODE_ENV: JSON.stringify(nodeEnv)
43+
}
44+
}),
45+
new HtmlWebpackPlugin({
46+
title: "Typescript Webpack Starter",
47+
template: "!!ejs-loader!src/index.html"
48+
}),
49+
new webpack.optimize.CommonsChunkPlugin({
50+
name: "vendor",
51+
minChunks: Infinity,
52+
filename: "vendor.bundle.js"
53+
}),
54+
new webpack.optimize.UglifyJsPlugin({
55+
compress: { warnings: false },
56+
output: { comments: false },
57+
sourceMap: true
58+
}),
59+
new DashboardPlugin(),
60+
new webpack.LoaderOptionsPlugin({
61+
options: {
62+
tslint: {
63+
emitErrors: true,
64+
failOnHint: true
65+
}
66+
}
67+
})
68+
],
69+
devServer: {
70+
contentBase: path.join(__dirname, "dist/"),
71+
compress: true,
72+
port: 3000,
73+
hot: true
74+
}
6475
};
6576

6677
module.exports = config;

0 commit comments

Comments
 (0)
Please sign in to comment.