Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit dd7b517

Browse files
committed
docs(testing): add tests that involve Angular 2
1 parent bd07936 commit dd7b517

17 files changed

+1032
-257
lines changed

public/docs/_examples/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ tsconfig.json
1010
tslint.json
1111
npm-debug*.
1212
**/protractor.config.js
13+
_test-output
+76-53
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,91 @@
1-
// Tun on full stack traces in errors to help debugging
2-
Error.stackTraceLimit=Infinity;
1+
/*global jasmine, __karma__, window*/
2+
(function () {
33

4+
// Error.stackTraceLimit = Infinity;
45

56
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
67

7-
// // Cancel Karma's synchronous start,
8-
// // we will call `__karma__.start()` later, once all the specs are loaded.
9-
__karma__.loaded = function() {};
10-
11-
12-
System.config({
13-
packages: {
14-
'base/app': {
15-
defaultExtension: false,
16-
// removed because of issues with raw .js files not being found.
17-
// format: 'register',
18-
map: Object.keys(window.__karma__.files).
19-
filter(onlyAppFiles).
20-
reduce(function createPathRecords(pathsMapping, appPath) {
21-
// creates local module name mapping to global path with karma's fingerprint in path, e.g.:
22-
// './hero.service': '/base/src/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
23-
var moduleName = appPath.replace(/^\/base\/app\//, './').replace(/\.js$/, '');
24-
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
25-
return pathsMapping;
26-
}, {})
27-
28-
}
29-
}
30-
});
31-
32-
// old code from angular 44
33-
// System.import('angular2/src/core/dom/browser_adapter').then(function(browser_adapter) {
34-
// new path for angular 51
35-
System.import('angular2/src/platform/browser/browser_adapter').then(function(browser_adapter) {
36-
browser_adapter.BrowserDomAdapter.makeCurrent();
37-
}).then(function() {
8+
// Cancel Karma's synchronous start,
9+
// we call `__karma__.start()` later, once all the specs are loaded.
10+
__karma__.loaded = function () { };
11+
12+
// SET THE RUNTIME APPLICATION ROOT HERE
13+
var appRoot ='app'; // no trailing slash!
14+
15+
// RegExp for client application base path within karma (which always starts 'base\')
16+
var karmaBase = '^\/base\/'; // RegEx string for base of karma folders
17+
var appPackage = 'base/' + appRoot; //e.g., base/app
18+
var appRootRe = new RegExp(karmaBase + appRoot + '\/');
19+
var onlyAppFilesRe = new RegExp(karmaBase + appRoot + '\/(?!.*\.spec\.js$)([a-z0-9-_\.\/]+)\.js$');
20+
21+
var moduleNames = [];
22+
23+
// Configure systemjs packages to use the .js extension for imports from the app folder
24+
var packages = {};
25+
packages[appPackage] = {
26+
defaultExtension: false,
27+
format: 'register',
28+
map: Object.keys(window.__karma__.files)
29+
.filter(onlyAppFiles)
30+
// Create local module name mapping to karma file path for app files
31+
// with karma's fingerprint in query string, e.g.:
32+
// './hero.service': '/base/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
33+
.reduce(function (pathsMapping, appPath) {
34+
var moduleName = appPath.replace(appRootRe, './').replace(/\.js$/, '');
35+
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath];
36+
return pathsMapping;
37+
}, {})
38+
}
39+
40+
System.config({ packages: packages });
41+
42+
// Configure Angular for the browser and
43+
// with test versions of the platform providers
44+
System.import('angular2/testing')
45+
.then(function (testing) {
46+
return System.import('angular2/platform/testing/browser')
47+
.then(function (providers) {
48+
testing.setBaseTestProviders(
49+
providers.TEST_BROWSER_PLATFORM_PROVIDERS,
50+
providers.TEST_BROWSER_APPLICATION_PROVIDERS
51+
);
52+
});
53+
})
54+
55+
// Load all spec files
56+
// (e.g. 'base/app/hero.service.spec.js')
57+
.then(function () {
3858
return Promise.all(
39-
Object.keys(window.__karma__.files) // All files served by Karma.
40-
.filter(onlySpecFiles)
41-
// .map(filePath2moduleName) // Normalize paths to module names.
42-
.map(function(moduleName) {
43-
// loads all spec files via their global module names (e.g. 'base/src/app/hero.service.spec')
44-
return System.import(moduleName);
45-
}));
59+
Object.keys(window.__karma__.files)
60+
.filter(onlySpecFiles)
61+
.map(function (moduleName) {
62+
moduleNames.push(moduleName);
63+
return System.import(moduleName);
64+
}));
4665
})
47-
.then(function() {
48-
__karma__.start();
49-
}, function(error) {
50-
__karma__.error(error.stack || error);
51-
});
5266

67+
.then(success, fail);
5368

54-
function filePath2moduleName(filePath) {
55-
return filePath.
56-
replace(/^\//, ''). // remove / prefix
57-
replace(/\.\w+$/, ''); // remove suffix
58-
}
59-
69+
////// Helpers //////
6070

6171
function onlyAppFiles(filePath) {
62-
return /^\/base\/app\/.*\.js$/.test(filePath) && !onlySpecFiles(filePath);
72+
return onlyAppFilesRe.test(filePath);
6373
}
6474

65-
6675
function onlySpecFiles(filePath) {
6776
return /\.spec\.js$/.test(filePath);
6877
}
78+
79+
function success () {
80+
console.log(
81+
'Spec files loaded:\n ' +
82+
moduleNames.join('\n ') +
83+
'\nStarting Jasmine testrunner');
84+
__karma__.start();
85+
}
86+
87+
function fail(error) {
88+
__karma__.error(error.stack || error);
89+
}
90+
91+
})();

public/docs/_examples/karma.conf.js

+61-22
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,82 @@
11
module.exports = function(config) {
2-
config.set({
32

4-
basePath: '',
3+
var appBase = 'app/'; // transpiled app JS files
4+
var appAssets ='base/app/'; // component assets fetched by Angular's compiler
55

6+
config.set({
7+
basePath: '',
68
frameworks: ['jasmine'],
9+
plugins: [
10+
require('karma-jasmine'),
11+
require('karma-chrome-launcher'),
12+
require('karma-htmlfile-reporter')
13+
],
14+
15+
customLaunchers: {
16+
// From the CLI. Not used here but interesting
17+
// chrome setup for travis CI using chromium
18+
Chrome_travis_ci: {
19+
base: 'Chrome',
20+
flags: ['--no-sandbox']
21+
}
22+
},
723

824
files: [
9-
// paths loaded by Karma
10-
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
11-
{pattern: 'node_modules/angular2/bundles/angular2.js', included: true, watched: true},
12-
{pattern: 'node_modules/angular2/bundles/testing.js', included: true, watched: true},
13-
{pattern: 'karma-test-shim.js', included: true, watched: true},
14-
{pattern: 'app/test/*.js', included: true, watched: true},
25+
// Angular and shim libraries loaded by Karma
26+
{ pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: true, watched: true },
27+
{ pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true },
28+
{ pattern: 'node_modules/es6-shim/es6-shim.js', included: true, watched: true },
29+
{ pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', included: true, watched: true },
30+
{ pattern: 'node_modules/rxjs/bundles/Rx.js', included: true, watched: true },
31+
{ pattern: 'node_modules/angular2/bundles/angular2.js', included: true, watched: true },
32+
{ pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true },
1533

16-
// paths loaded via module imports
17-
{pattern: 'app/**/*.js', included: false, watched: true},
34+
// External libraries loaded by Karma
35+
{ pattern: 'node_modules/angular2/bundles/http.dev.js', included: true, watched: true },
36+
{ pattern: 'node_modules/angular2/bundles/router.dev.js', included: true, watched: true },
37+
{ pattern: 'node_modules/a2-in-memory-web-api/web-api.js', included: true, watched: true },
1838

19-
// paths loaded via Angular's component compiler
39+
// Configures module loader w/ app and specs, then launch karma
40+
{ pattern: 'karma-test-shim.js', included: true, watched: true },
41+
42+
// transpiled application & spec code paths loaded via module imports
43+
{pattern: appBase + '**/*.js', included: false, watched: true},
44+
45+
// asset (HTML & CSS) paths loaded via Angular's component compiler
2046
// (these paths need to be rewritten, see proxies section)
21-
{pattern: 'app/**/*.html', included: false, watched: true},
22-
{pattern: 'app/**/*.css', included: false, watched: true},
47+
{pattern: appBase + '**/*.html', included: false, watched: true},
48+
{pattern: appBase + '**/*.css', included: false, watched: true},
2349

24-
// paths to support debugging with source maps in dev tools
25-
{pattern: 'app/**/*.ts', included: false, watched: false},
26-
{pattern: 'app/**/*.js.map', included: false, watched: false}
50+
// paths for debugging with source maps in dev tools
51+
{pattern: appBase + '**/*.ts', included: false, watched: false},
52+
{pattern: appBase + '**/*.js.map', included: false, watched: false}
2753
],
2854

29-
// proxied base paths
55+
// proxied base paths for loading assets
3056
proxies: {
31-
// required for component assests fetched by Angular's compiler
32-
"/app/": "/base/app/"
57+
// required for component assets fetched by Angular's compiler
58+
"/app/": appAssets
59+
},
60+
61+
exclude: [],
62+
preprocessors: {},
63+
reporters: ['progress', 'html'],
64+
65+
// HtmlReporter configuration
66+
htmlReporter: {
67+
// Open this file to see results in browser
68+
outputFile: '_test-output/tests.html',
69+
70+
// Optional
71+
pageTitle: 'Unit Tests',
72+
subPageTitle: __dirname
3373
},
3474

35-
reporters: ['progress'],
36-
port: 9877,
75+
port: 9876,
3776
colors: true,
3877
logLevel: config.LOG_INFO,
3978
autoWatch: true,
4079
browsers: ['Chrome'],
41-
singleRun: true
80+
singleRun: false
4281
})
4382
}

public/docs/_examples/karma.js.conf.js

-68
This file was deleted.

public/docs/_examples/karma.ts.conf.js

-70
This file was deleted.

0 commit comments

Comments
 (0)