Skip to content

Commit 4afd815

Browse files
committed
feat(test): karma integration and unit test sample
Closes #10
1 parent 04a4bbb commit 4afd815

File tree

5 files changed

+115
-4
lines changed

5 files changed

+115
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Error.stackTraceLimit = Infinity;
2+
3+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
4+
5+
__karma__.loaded = function() {};
6+
7+
System.config({
8+
packages: {
9+
'base/dist/app': {
10+
defaultExtension: false,
11+
format: 'register',
12+
map: Object.keys(window.__karma__.files)
13+
.filter(onlyAppFiles)
14+
.reduce(function(pathsMapping, appPath) {
15+
var moduleName = appPath.replace(/^\/base\/dist\/app\//, './').replace(/\.js$/, '');
16+
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
17+
return pathsMapping;
18+
}, {})
19+
}
20+
}
21+
});
22+
23+
System.import('angular2/src/core/dom/browser_adapter').then(function(browser_adapter) {
24+
// TODO: once beta is out we should change this code to use a "test platform"
25+
browser_adapter.BrowserDomAdapter.makeCurrent();
26+
}).then(function() {
27+
return Promise.all(
28+
Object.keys(window.__karma__.files)
29+
.filter(onlySpecFiles)
30+
.map(function(moduleName) {
31+
return System.import(moduleName);
32+
}));
33+
}).then(function() {
34+
__karma__.start();
35+
}, function(error) {
36+
__karma__.error(error.stack || error);
37+
});
38+
39+
function onlyAppFiles(filePath) {
40+
return /^\/base\/dist\/app\/(?!spec)([a-z0-9]+)\.js$/.test(filePath);
41+
}
42+
43+
function onlySpecFiles(path) {
44+
return /\.spec\.js$/.test(path);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = function(config) {
2+
config.set({
3+
basePath: '',
4+
frameworks: ['jasmine'],
5+
files: [
6+
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
7+
{pattern: 'node_modules/angular2/bundles/angular2.js', included: true, watched: true},
8+
{pattern: 'node_modules/angular2/bundles/testing.js', included: true, watched: true},
9+
10+
{pattern: 'karma-test-shim.js', included: true, watched: true},
11+
12+
// paths loaded via module imports
13+
{pattern: 'dist/**/*.js', included: false, watched: true},
14+
15+
// paths loaded via Angular's component compiler
16+
// (these paths need to be rewritten, see proxies section)
17+
{pattern: 'dist/**/*.html', included: false, watched: true},
18+
{pattern: 'dist/**/*.css', included: false, watched: true},
19+
20+
// paths to support debugging with source maps in dev tools
21+
{pattern: 'dist/**/*.ts', included: false, watched: false},
22+
{pattern: 'dist/**/*.js.map', included: false, watched: false}
23+
],
24+
proxies: {
25+
// required for component assests fetched by Angular's compiler
26+
"/app/": "/base/dist/app/"
27+
},
28+
exclude: [],
29+
preprocessors: {},
30+
reporters: ['progress'],
31+
port: 9876,
32+
colors: true,
33+
logLevel: config.LOG_INFO,
34+
autoWatch: true,
35+
browsers: ['Chrome'],
36+
singleRun: false
37+
});
38+
};

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"devDependencies": {
1010
"angular-cli": "0.0.*",
1111
"angular-cli-github-pages": "^0.2.0",
12-
"ember-cli-inject-live-reload": "^1.3.0"
12+
"ember-cli-inject-live-reload": "^1.3.0",
13+
"jasmine-core": "^2.3.4",
14+
"karma": "^0.13.15",
15+
"karma-chrome-launcher": "^0.2.1",
16+
"karma-jasmine": "^0.3.6"
1317
}
1418
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {describe, it, expect, beforeEachProviders, inject} from 'angular2/testing';
2+
import {<%= jsComponentName %>App} from '../app/<%= htmlComponentName %>';
3+
4+
beforeEachProviders(() => [<%= jsComponentName %>App]);
5+
6+
describe('App: <%= jsComponentName %>', () => {
7+
it('should have the `defaultMeaning` as 42', inject([<%= jsComponentName %>App], (app) => {
8+
expect(app.defaultMeaning).toBe(42);
9+
}));
10+
11+
describe('#meaningOfLife', () => {
12+
it('should get the meaning of life', inject([<%= jsComponentName %>App], (app) => {
13+
expect(app.meaningOfLife()).toBe('The meaning of life is 42');
14+
expect(app.meaningOfLife(22)).toBe('The meaning of life is 22');
15+
}));
16+
});
17+
18+
it('should be almost passing', function() {
19+
expect(1 + 1).toBe(1);
20+
});
21+
});
22+

addon/ng2/blueprints/ng2/files/src/app/__name__.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {Component} from 'angular2/angular2';
99
pipes: []
1010
})
1111
export class <%= jsComponentName %>App {
12-
13-
constructor() {}
14-
12+
defaultMeaning: number = 42;
13+
14+
meaningOfLife(meaning) {
15+
return `The meaning of life is ${meaning || this.defaultMeaning}`;
16+
}
1517
}

0 commit comments

Comments
 (0)