Skip to content

Commit c066ff2

Browse files
committed
test(Karma): unit tests
Unit tests implemented with Karma and Webpack
1 parent a80feec commit c066ff2

File tree

8 files changed

+2048
-53
lines changed

8 files changed

+2048
-53
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ notifications:
66
node_js:
77
- '7'
88
- '6'
9-
- '4'
109

1110
before_script:
1211
- npm prune
1312

13+
script:
14+
- npm run lint
15+
- npm run test:headless
16+
1417
after_success:
18+
- npm run coveralls
1519
- npm run build_prod
1620
- npm run semantic-release
1721

config/karma.conf.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* angular2-expandable-list
3+
*
4+
* Copyright 2017, @andreasonny83, All rights reserved.
5+
*
6+
* @author: @andreasonny83 <andreasonny83@gmail.com>
7+
*/
8+
9+
module.exports = function(config) {
10+
var testWebpackConfig = require('./webpack.test.js')({env: 'test'});
11+
12+
var configuration = {
13+
14+
// base path that will be used to resolve all patterns (e.g. files, exclude)
15+
basePath: '',
16+
17+
/*
18+
* Frameworks to use
19+
*
20+
* available frameworks: https://npmjs.org/browse/keyword/karma-adapter
21+
*/
22+
frameworks: ['jasmine'],
23+
24+
// list of files to exclude
25+
exclude: [ ],
26+
27+
/*
28+
* list of files / patterns to load in the browser
29+
*
30+
* we are building the test environment in ./spec-bundle.js
31+
*/
32+
files: [ { pattern: './spec-bundle.js', watched: false } ],
33+
34+
/*
35+
* preprocess matching files before serving them to the browser
36+
* available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
37+
*/
38+
preprocessors: {
39+
'./spec-bundle.js': ['coverage', 'webpack', 'sourcemap']
40+
},
41+
42+
// Webpack Config at ./webpack.test.js
43+
webpack: testWebpackConfig,
44+
45+
coverageReporter: {
46+
type: 'in-memory'
47+
},
48+
49+
remapCoverageReporter: {
50+
'text-summary': null,
51+
json: './coverage/coverage.json',
52+
html: './coverage/html',
53+
lcovonly: './coverage/lcov.info',
54+
},
55+
56+
// Webpack please don't spam the console when running in karma!
57+
webpackMiddleware: {
58+
noInfo: true,
59+
stats: {
60+
chunks: false
61+
}
62+
},
63+
64+
/*
65+
* test results reporter to use
66+
*
67+
* possible values: 'dots', 'progress'
68+
* available reporters: https://npmjs.org/browse/keyword/karma-reporter
69+
*/
70+
reporters: [ 'mocha', 'coverage', 'remap-coverage' ],
71+
72+
// web server port
73+
port: 9876,
74+
75+
// enable / disable colors in the output (reporters and logs)
76+
colors: true,
77+
78+
/*
79+
* level of logging
80+
* possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
81+
*/
82+
logLevel: config.LOG_INFO,
83+
84+
// enable / disable watching file and executing tests whenever any file changes
85+
autoWatch: true,
86+
87+
/*
88+
* start these browsers
89+
* available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
90+
*/
91+
browsers: [
92+
'Chrome'
93+
],
94+
95+
browserConsoleLogOptions: {
96+
terminal: true,
97+
level: ''
98+
},
99+
100+
/*
101+
* Continuous Integration mode
102+
* if true, Karma captures browsers, runs the tests and exits
103+
*/
104+
singleRun: true
105+
};
106+
107+
config.set(configuration);
108+
};

config/spec-bundle.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* angular2-expandable-list
3+
*
4+
* Copyright 2017, @andreasonny83, All rights reserved.
5+
*
6+
* @author: @andreasonny83 <andreasonny83@gmail.com>
7+
*/
8+
9+
Error.stackTraceLimit = Infinity;
10+
11+
require('core-js/es6');
12+
require('core-js/es7/reflect');
13+
14+
require('zone.js/dist/zone');
15+
require('zone.js/dist/long-stack-trace-zone');
16+
require('zone.js/dist/proxy'); // since zone.js 0.6.15
17+
require('zone.js/dist/sync-test');
18+
require('zone.js/dist/jasmine-patch'); // put here since zone.js 0.6.14
19+
require('zone.js/dist/async-test');
20+
require('zone.js/dist/fake-async-test');
21+
22+
// RxJS
23+
require('rxjs/Rx');
24+
25+
var testing = require('@angular/core/testing');
26+
var browser = require('@angular/platform-browser-dynamic/testing');
27+
28+
testing.TestBed.initTestEnvironment(
29+
browser.BrowserDynamicTestingModule,
30+
browser.platformBrowserDynamicTesting()
31+
);
32+
33+
/*
34+
* Ok, this is kinda crazy. We can use the context method on
35+
* require that webpack created in order to tell webpack
36+
* what files we actually want to require or import.
37+
* Below, context will be a function/object with file names as keys.
38+
* Using that regex we are saying look in ../src then find
39+
* any file that ends with spec.ts and get its path. By passing in true
40+
* we say do this recursively
41+
*/
42+
var testContext = require.context('../src', true, /\.spec\.ts/);
43+
44+
/*
45+
* get all the files, for each file, call the context function
46+
* that will require the file and load it up here. Context will
47+
* loop and require those spec files here
48+
*/
49+
function requireAll(requireContext) {
50+
return requireContext.keys().map(requireContext);
51+
}
52+
53+
// requires and returns all modules that match
54+
var modules = requireAll(testContext);

config/webpack.test.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* angular2-expandable-list
3+
*
4+
* Copyright 2017, @andreasonny83, All rights reserved.
5+
*
6+
* @author: @andreasonny83 <andreasonny83@gmail.com>
7+
*/
8+
9+
const helpers = require('./helpers');
10+
const path = require('path');
11+
12+
/**
13+
* Webpack Plugins
14+
*/
15+
const ProvidePlugin = require('webpack/lib/ProvidePlugin');
16+
const DefinePlugin = require('webpack/lib/DefinePlugin');
17+
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
18+
19+
const ENV = process.env.ENV = process.env.NODE_ENV = 'test';
20+
21+
/**
22+
* Webpack configuration
23+
*
24+
* See: http://webpack.github.io/docs/configuration.html#cli
25+
*/
26+
process.noDeprecation = true;
27+
28+
module.exports = function (options) {
29+
return {
30+
31+
devtool: 'inline-source-map',
32+
33+
resolve: {
34+
extensions: ['.ts', '.js'],
35+
modules: [
36+
helpers.root('node_modules'),
37+
helpers.root('src'),
38+
]
39+
},
40+
41+
module: {
42+
rules: [
43+
{
44+
enforce: 'pre',
45+
test: /\.js$/,
46+
loader: 'source-map-loader',
47+
exclude: [
48+
// these packages have problems with their sourcemaps
49+
helpers.root('node_modules/rxjs'),
50+
helpers.root('node_modules/@angular'),
51+
]
52+
},
53+
54+
{
55+
test: /\.ts$/,
56+
use: [
57+
{
58+
loader: 'awesome-typescript-loader',
59+
options: {
60+
configFileName: helpers.root('src/tsconfig.json'),
61+
sourceMap: false,
62+
inlineSourceMap: true,
63+
compilerOptions: {
64+
removeComments: true
65+
}
66+
}
67+
},
68+
{
69+
loader: 'angular2-template-loader'
70+
}
71+
],
72+
exclude: [
73+
/\.e2e\.ts$/
74+
]
75+
},
76+
77+
{
78+
test: /\.(css|html)$/,
79+
loader: 'raw-loader'
80+
},
81+
82+
{
83+
enforce: 'post',
84+
test: /\.(js|ts)$/,
85+
use: [
86+
{
87+
loader: 'istanbul-instrumenter-loader',
88+
options: {
89+
esModules: true,
90+
}
91+
}
92+
],
93+
include: helpers.root('src'),
94+
exclude: [
95+
/\.(e2e|spec)\.ts$/,
96+
/node_modules/
97+
]
98+
}
99+
]
100+
},
101+
102+
plugins: [
103+
new DefinePlugin({
104+
'ENV': JSON.stringify(ENV),
105+
'process.env': {
106+
'ENV': JSON.stringify(ENV),
107+
'NODE_ENV': JSON.stringify(ENV)
108+
}
109+
}),
110+
111+
new ContextReplacementPlugin(
112+
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
113+
helpers.root('src'),
114+
{ }
115+
),
116+
],
117+
118+
node: {
119+
global: true,
120+
process: false,
121+
crypto: 'empty',
122+
module: false,
123+
clearImmediate: false,
124+
setImmediate: false
125+
}
126+
};
127+
}

package.json

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
"build": "webpack --config config/webpack.lib.js",
88
"build_prod": "npm run inline && npm run ngc && npm run build",
99
"commit": "git-cz",
10+
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
1011
"demo": "webpack-dev-server --inline --progress --open --port 9007 --config config/webpack.demo.js --content-base demo",
1112
"inline": "ng2-inline -o lib -f -b src src/**/*.ts",
13+
"lint": "tslint \"src/**/*.ts\" -e \"src/**/*.d.ts\"",
1214
"ngc": "ngc -p tsconfig.json",
13-
"precommit": "npm run test",
14-
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
15+
"precommit": "npm run lint && npm run test:headless",
16+
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
17+
"test:headless": "npm run test -- --browsers PhantomJS",
18+
"test": "karma start config/karma.conf.js",
19+
"test:watch": "npm run test -- --no-single-run"
1520
},
1621
"repository": {
1722
"type": "git",
@@ -50,15 +55,31 @@
5055
"@types/webpack": "2.2.8",
5156
"angular2-inline-template-style": "1.0.2",
5257
"angular2-template-loader": "0.6.2",
58+
"autoprefixer": "^6.7.7",
5359
"awesome-typescript-loader": "3.1.0",
5460
"codelyzer": "2.0.0",
5561
"commitizen": "2.9.6",
5662
"core-js": "2.4.1",
63+
"coveralls": "^2.12.0",
5764
"cz-conventional-changelog": "1.2.0",
5865
"husky": "0.13.1",
66+
"istanbul-instrumenter-loader": "^2.0.0",
67+
"jasmine-core": "^2.5.2",
68+
"karma": "^1.5.0",
69+
"karma-chrome-launcher": "^2.0.0",
70+
"karma-coverage": "^1.1.1",
71+
"karma-jasmine": "^1.1.0",
72+
"karma-mocha-reporter": "^2.2.2",
73+
"karma-phantomjs-launcher": "^1.0.4",
74+
"karma-remap-coverage": "^0.1.4",
75+
"karma-sourcemap-loader": "^0.3.7",
76+
"karma-webpack": "^2.0.3",
77+
"postcss-loader": "^1.3.3",
5978
"raw-loader": "0.5.1",
6079
"rxjs": "5.2.0",
6180
"semantic-release": "6.3.2",
81+
"source-map-loader": "^0.2.0",
82+
"tslint": "^4.5.1",
6283
"typescript": "2.2.1",
6384
"webpack": "2.2.1",
6485
"webpack-dev-server": "2.4.1",

0 commit comments

Comments
 (0)