Skip to content

Commit 02a763a

Browse files
committed
feat(test): add karma plugin
1 parent b8f502e commit 02a763a

File tree

6 files changed

+60
-44
lines changed

6 files changed

+60
-44
lines changed

addon/ng2/blueprints/ng2/files/config/karma.conf.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module.exports = function (config) {
22
config.set({
33
basePath: '..',
4-
frameworks: ['jasmine'],
4+
frameworks: ['jasmine', 'angular-cli'],
55
plugins: [
66
require('karma-jasmine'),
7-
require('karma-chrome-launcher')
7+
require('karma-chrome-launcher'),
8+
require('karma-coverage'),
9+
require('angular-cli/plugins/karma')
810
],
911
customLaunchers: {
1012
// chrome setup for travis CI using chromium
@@ -13,8 +15,13 @@ module.exports = function (config) {
1315
flags: ['--no-sandbox']
1416
}
1517
},
16-
files: [],
17-
preprocessors: {},
18+
files: [
19+
{ pattern: './src/test.ts', watched: false }
20+
],
21+
preprocessors: {
22+
'./src/test.ts': ['angular-cli']
23+
},
24+
angularCliConfig: './angular-cli.json',
1825
reporters: ['progress'],
1926
port: 9876,
2027
colors: true,

addon/ng2/blueprints/ng2/files/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"jasmine-spec-reporter": "2.5.0",
4444
"karma": "0.13.22",
4545
"karma-chrome-launcher": "0.2.3",
46+
"karma-coverage": "^1.0.0",
4647
"karma-jasmine": "0.3.8",
4748
"protractor": "3.3.0",
4849
"ts-node": "0.9.1",

addon/ng2/models/webpack-build-test.ts renamed to addon/ng2/models/webpack-build-test.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as webpack from 'webpack';
2-
import * as path from 'path';
3-
import { CliConfig } from './config';
1+
// this config must be JS so that the karma plugin can load it
42

5-
export const getWebpackTestConfig = function(projectRoot: string, sourceDir: string) {
3+
const path = require('path');
4+
5+
const getWebpackTestConfig = function(projectRoot, sourceDir) {
66
return {
77
devtool: 'inline-source-map',
88
context: path.resolve(__dirname, './'),
@@ -89,3 +89,5 @@ export const getWebpackTestConfig = function(projectRoot: string, sourceDir: str
8989
}
9090
};
9191
}
92+
93+
module.exports.getWebpackTestConfig = getWebpackTestConfig;

addon/ng2/tasks/test.ts

-31
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,6 @@ module.exports = Task.extend({
1616
return new Promise((resolve) => {
1717
const karma = requireDependency(projectRoot, 'karma');
1818
const karmaConfig = path.join(projectRoot, this.project.ngConfig.test.karma.config);
19-
const testFile = `./${this.project.ngConfig.defaults.sourceDir}/test.ts`;
20-
21-
options.plugins = [
22-
require('karma-webpack'),
23-
require('karma-jasmine'),
24-
require('karma-chrome-launcher'),
25-
require('karma-coverage'),
26-
require('karma-mocha-reporter'),
27-
require('karma-sourcemap-loader')
28-
];
29-
options.reporters = ['coverage', 'mocha', 'progress'];
30-
31-
// Abstract the webpack concepts from the local karma config.
32-
// Add those details here.
33-
34-
// Single test entry file. Will run the test.ts bundle and track it.
35-
options.files = [{ pattern: testFile, watched: false }];
36-
options.preprocessors = { [testFile]: ['webpack','sourcemap'] };
37-
options.webpack = getWebpackTestConfig(projectRoot, this.project.ngConfig.defaults.sourceDir);
38-
options.webpackMiddleware = {
39-
noInfo: true, // Hide webpack output because its noisy.
40-
stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.
41-
assets: false,
42-
colors: true,
43-
version: false,
44-
hash: false,
45-
timings: false,
46-
chunks: false,
47-
chunkModules: false
48-
}
49-
};
5019

5120
// Convert browsers from a string to an array
5221
if (options.browsers) {

package.json

-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@
5656
"html-webpack-plugin": "^2.19.0",
5757
"istanbul-instrumenter-loader": "^0.2.0",
5858
"json-loader": "^0.5.4",
59-
"karma-chrome-launcher": "^1.0.1",
60-
"karma-coverage": "^1.0.0",
61-
"karma-jasmine": "^1.0.2",
62-
"karma-mocha-reporter": "^2.0.4",
63-
"karma-phantomjs-launcher": "^1.0.0",
6459
"karma-sourcemap-loader": "^0.3.7",
6560
"karma-webpack": "^1.7.0",
6661
"leek": "0.0.21",

plugins/karma.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const path = require('path');
2+
const getWebpackTestConfig = require('../addon/ng2/models/webpack-build-test').getWebpackTestConfig;
3+
4+
const init = (config) => {
5+
6+
// load Angular CLI config
7+
if (!config.angularCliConfig) throw new Error('Missing \'angularCliConfig\' entry in Karma config');
8+
const angularCliConfig = require(path.join(config.basePath, config.angularCliConfig))
9+
10+
// add webpack config
11+
config.webpack = getWebpackTestConfig(config.basePath, angularCliConfig.defaults.sourceDir);
12+
config.webpackMiddleware = {
13+
noInfo: true, // Hide webpack output because its noisy.
14+
stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.
15+
assets: false,
16+
colors: true,
17+
version: false,
18+
hash: false,
19+
timings: false,
20+
chunks: false,
21+
chunkModules: false
22+
}
23+
};
24+
25+
// replace the angular-cli preprocessor with webpack+sourcemap
26+
Object.keys(config.preprocessors)
27+
.filter((file) => config.preprocessors[file].indexOf('angular-cli') !== -1)
28+
.map((file) => config.preprocessors[file])
29+
.map((arr) => arr.splice(arr.indexOf('angular-cli'), 1, 'webpack', 'sourcemap'));
30+
}
31+
32+
init.$inject = ['config']
33+
34+
// dummy preprocessor, just to keep karma from showing a warning
35+
const preprocessor = () => (content, file, done) => done(null, content);
36+
preprocessor.$inject = []
37+
38+
// also export karma-webpack and karma-sourcemap-loader
39+
module.exports = Object.assign({
40+
'framework:angular-cli': ['factory', init],
41+
'preprocessor:angular-cli': ['factory', preprocessor]
42+
}, require('karma-webpack'), require('karma-sourcemap-loader'));

0 commit comments

Comments
 (0)