Skip to content

Commit e90272e

Browse files
committed
Changed test configurations to more robust and maintainable ones.
- These are made according to the Angular quickstart.
1 parent b24c9ed commit e90272e

File tree

5 files changed

+225
-107
lines changed

5 files changed

+225
-107
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ node_js:
33
- '5'
44
before_install:
55
- npm install -g codeclimate-test-reporter
6-
before_script:
7-
- npm run typings
86
script: npm test -- --browsers PhantomJS
97
after_script:
108
- npm run coverage-remap

karma-test-shim.js

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,79 @@ Error.stackTraceLimit = Infinity;
33

44
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
55

6+
var builtPath = '/base/lib/';
7+
68
// // Cancel Karma's synchronous start,
79
// // we will call `__karma__.start()` later, once all the specs are loaded.
8-
__karma__.loaded = function() {};
10+
__karma__.loaded = function () {
11+
};
12+
13+
function isJsFile(path) {
14+
return path.slice(-3) == '.js';
15+
}
16+
17+
function isSpecFile(path) {
18+
return /\.spec\.(.*\.)?js$/.test(path);
19+
}
20+
21+
function isBuiltFile(path) {
22+
return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath);
23+
}
24+
25+
var allSpecFiles = Object.keys(window.__karma__.files)
26+
.filter(isSpecFile)
27+
.filter(isBuiltFile);
928

1029
System.config({
30+
baseURL: '/base',
31+
// Extend usual application package list with test folder
1132
packages: {
12-
'base/lib': {
13-
defaultExtension: false,
14-
format: 'cjs',
15-
map: Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {})
16-
},
17-
'@angular/core': {
18-
main: 'bundles/core.umd.js',
19-
defaultExtension: 'js'
20-
},
21-
'@angular/common': {
22-
main: 'bundles/common.umd.js',
23-
defaultExtension: 'js'
24-
},
25-
'@angular/platform-browser': {
26-
main: 'bundles/platform-browser.umd.js',
27-
defaultExtension: 'js'
28-
},
29-
'rxjs': {
30-
main: 'Rx.js',
31-
defaultExtension: 'js'
32-
},
33-
'traceur': {
34-
main: 'traceur.js',
33+
'testing': {
34+
main: 'index.js',
3535
defaultExtension: 'js'
3636
}
3737
},
38+
39+
// Assume npm: is set in `paths` in systemjs.config
40+
// Map the angular testing umd bundles
3841
map: {
39-
'lodash': '/base/node_modules/lodash/lodash.js',
40-
'@angular': 'base/node_modules/@angular',
41-
'rxjs': 'base/node_modules/rxjs',
42-
'traceur': 'base/node_modules/traceur/bin'
42+
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
43+
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
44+
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
45+
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
46+
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
47+
'@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
48+
'@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
49+
'@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js'
4350
}
4451
});
4552

46-
System.import('@angular/platform-browser/src/browser/browser_adapter.js')
47-
.then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
48-
.then(function() { return Promise.all(resolveTestFiles()); })
49-
.then(function() { __karma__.start(); }, function(error) { __karma__.error(error.stack || error); });
53+
System.import('systemjs.config.js')
54+
.then(initTestBed)
55+
.then(initTesting);
5056

51-
function createPathRecords(pathsMapping, appPath) {
52-
// creates local module name mapping to global path with karma's fingerprint in path, e.g.:
53-
// './vg-player/vg-player':
54-
// '/base/dist/vg-player/vg-player.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
55-
var moduleName = './' + resolveKeyPathForMapping('base/lib/', appPath);
56-
moduleName = moduleName.replace(/\.js$/, '');
57-
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath];
58-
return pathsMapping;
59-
}
57+
function initTestBed() {
58+
return Promise.all([
59+
System.import('@angular/core/testing'),
60+
System.import('@angular/platform-browser-dynamic/testing')
61+
])
6062

61-
function onlyAppFiles(filePath) {
62-
return /\/base\/lib\/(?!.*\.spec\.js$).*\.js$/.test(filePath);
63-
}
63+
.then(function (providers) {
64+
var coreTesting = providers[0];
65+
var browserTesting = providers[1];
6466

65-
function onlySpecFiles(path) {
66-
return /\.spec\.js$/.test(path);
67+
coreTesting.TestBed.initTestEnvironment(
68+
browserTesting.BrowserDynamicTestingModule,
69+
browserTesting.platformBrowserDynamicTesting());
70+
})
6771
}
6872

69-
function resolveTestFiles() {
70-
return Object.keys(window.__karma__.files) // All files served by Karma.
71-
.filter(onlySpecFiles)
72-
.map(function(moduleName) {
73-
// loads all spec files via their global module names (e.g.
74-
// 'base/dist/vg-player/vg-player.spec')
73+
// Import all spec files and start karma
74+
function initTesting() {
75+
return Promise.all(
76+
allSpecFiles.map(function (moduleName) {
7577
return System.import(moduleName);
76-
});
77-
}
78-
79-
function resolveKeyPathForMapping(basePathWhereToStart, appPath) {
80-
var location = appPath.indexOf(basePathWhereToStart);
81-
if (location > -1) {
82-
return appPath.substring(basePathWhereToStart.length + 1);
83-
} else {
84-
return appPath;
85-
}
78+
})
79+
)
80+
.then(__karma__.start, __karma__.error);
8681
}

karma.conf.js

Lines changed: 129 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,15 @@
1-
module.exports = function(config) {
1+
module.exports = function (config) {
2+
3+
var appBase = 'lib/'; // transpiled app JS and map files
4+
var appSrcBase = 'src/'; // app source TS files
5+
var appAssets = '/base/lib/'; // component assets fetched by Angular's compiler
6+
27
config.set({
38

49
basePath: '.',
510

611
frameworks: ['jasmine'],
712

8-
files: [
9-
// paths loaded by Karma
10-
'node_modules/traceur/bin/traceur-runtime.js',
11-
{pattern: 'node_modules/core-js/client/shim.js', included: true, watched: true},
12-
{pattern: 'node_modules/zone.js/dist/zone.js', included: true, watched: true},
13-
{pattern: 'node_modules/reflect-metadata/Reflect.js', included: true, watched: true},
14-
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
15-
{pattern: 'node_modules/rxjs/bundles/Rx.js', included: true, watched: true},
16-
{pattern: 'node_modules/lodash/lodash.js', included: true, watched: true},
17-
{pattern: 'karma-test-shim.js', included: true, watched: true},
18-
19-
// paths loaded via module imports
20-
{pattern: 'lib/**/*.js', included: false, watched: true},
21-
{pattern: 'node_modules/@angular/**/*.js', included: false, watched: true},
22-
{pattern: 'node_modules/rxjs/**/*.js', included: false, watched: true},
23-
{pattern: 'node_modules/traceur/**/*.js', included: false, watched: true},
24-
25-
// paths to support debugging with source maps in dev tools
26-
{pattern: 'src/**/*.ts', included: false, watched: false},
27-
{pattern: 'lib/**/*.js.map', included: false, watched: false}
28-
],
29-
30-
// proxied base paths
31-
proxies: {
32-
// required for component assests fetched by Angular's compiler
33-
'/src/': '/base/src/'
34-
},
35-
36-
port: 9876,
37-
38-
logLevel: config.LOG_INFO,
39-
40-
colors: true,
41-
42-
autoWatch: true,
43-
44-
browsers: ['Chrome'],
45-
4613
// Karma plugins loaded
4714
plugins: [
4815
'karma-jasmine',
@@ -51,21 +18,140 @@ module.exports = function(config) {
5118
'karma-phantomjs-launcher'
5219
],
5320

54-
// Coverage reporter generates the coverage
55-
reporters: ['progress', 'dots', 'coverage'],
21+
customLaunchers: {
22+
// From the CLI. Not used here but interesting
23+
// chrome setup for travis CI using chromium
24+
Chrome_travis_ci: {
25+
base: 'Chrome',
26+
flags: ['--no-sandbox']
27+
}
28+
},
29+
30+
files: [
31+
// System.js for module loading
32+
'node_modules/systemjs/dist/system.src.js',
33+
34+
// Polyfills
35+
'node_modules/core-js/client/shim.js',
36+
37+
// Zone.js
38+
'node_modules/zone.js/dist/zone.js',
39+
'node_modules/zone.js/dist/long-stack-trace-zone.js',
40+
'node_modules/zone.js/dist/proxy.js',
41+
'node_modules/zone.js/dist/sync-test.js',
42+
'node_modules/zone.js/dist/jasmine-patch.js',
43+
'node_modules/zone.js/dist/async-test.js',
44+
'node_modules/zone.js/dist/fake-async-test.js',
45+
46+
// RxJs
47+
{
48+
pattern: 'node_modules/rxjs/**/*.js',
49+
included: false,
50+
watched: false
51+
},
52+
{
53+
pattern: 'node_modules/rxjs/**/*.js.map',
54+
included: false,
55+
watched: false
56+
},
57+
58+
// Angular 2
59+
{
60+
pattern: 'node_modules/@angular/**/*.js',
61+
included: false,
62+
watched: false
63+
},
64+
{
65+
pattern: 'node_modules/@angular/**/*.js.map',
66+
included: false,
67+
watched: false
68+
},
69+
70+
// Lodash
71+
{
72+
pattern: 'node_modules/lodash/lodash.js',
73+
included: false,
74+
watched: false
75+
},
76+
77+
// The testing library
78+
{
79+
pattern: 'systemjs.config.js',
80+
included: false,
81+
watched: false
82+
},
83+
'karma-test-shim.js',
84+
85+
// transpiled application & spec code paths loaded via module imports
86+
{
87+
pattern: appBase + '**/*.js',
88+
included: false,
89+
watched: true
90+
},
91+
92+
// asset (HTML & CSS) paths loaded via Angular's component compiler
93+
// (these paths need to be rewritten, see proxies section)
94+
{
95+
pattern: appBase + '**/*.html',
96+
included: false,
97+
watched: true
98+
},
99+
{
100+
pattern: appBase + '**/*.css',
101+
included: false,
102+
watched: true
103+
},
104+
105+
// paths for debugging with source maps in dev tools
106+
{
107+
pattern: appSrcBase + '**/*.ts',
108+
included: false,
109+
watched: false
110+
},
111+
{
112+
pattern: appBase + '**/*.js.map',
113+
included: false,
114+
watched: false
115+
}
116+
],
117+
118+
// proxied base paths for loading assets
119+
proxies: {
120+
// required for component assets fetched by Angular's compiler
121+
"/lib/": appAssets
122+
},
123+
124+
exclude: [],
56125

57126
// Source files that you wanna generate coverage for.
58127
// Do not include tests or libraries (these files will be instrumented by Istanbul)
59128
preprocessors: {
60129
'lib/**/!(*spec).js': ['coverage']
61130
},
62131

132+
// Coverage reporter generates the coverage
133+
reporters: ['progress', 'dots', 'coverage'],
134+
63135
coverageReporter: {
64-
reporters:[
65-
{type: 'json', subdir: '.', file: 'coverage-final.json'}
136+
reporters: [
137+
{
138+
type: 'json',
139+
subdir: '.',
140+
file: 'coverage-final.json'
141+
}
66142
]
67143
},
68144

145+
port: 9876,
146+
147+
colors: true,
148+
149+
logLevel: config.LOG_INFO,
150+
151+
autoWatch: true,
152+
153+
browsers: ['Chrome'],
154+
69155
singleRun: true
70156
})
71157
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
},
3737
"homepage": "https://github.com/mariuszfoltak/angular2-datatable#readme",
3838
"devDependencies": {
39+
"@angular/compiler": "^2.0.0",
40+
"@angular/platform-browser-dynamic": "^2.0.0",
3941
"core-js": "^2.4.1",
4042
"http-server": "^0.9.0",
4143
"jasmine-core": "^2.4.1",

systemjs.config.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* System configuration for Angular 2 samples
3+
* Adjust as necessary for your application needs.
4+
*/
5+
(function (global) {
6+
System.config({
7+
paths: {
8+
// paths serve as alias
9+
'npm:': 'node_modules/'
10+
},
11+
// map tells the System loader where to look for things
12+
map: {
13+
// angular bundles
14+
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
15+
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
16+
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
17+
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
18+
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
19+
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
20+
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
21+
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
22+
23+
// other libraries
24+
'rxjs': 'npm:rxjs',
25+
'lodash': 'npm:lodash/lodash.js'
26+
},
27+
// packages tells the System loader how to load when no filename and/or no extension
28+
packages: {
29+
lib: {
30+
defaultExtension: 'js'
31+
},
32+
rxjs: {
33+
defaultExtension: 'js'
34+
}
35+
}
36+
});
37+
})(this);

0 commit comments

Comments
 (0)