Skip to content

Commit

Permalink
feat(test): karma integration and unit test sample
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
cironunes committed Nov 26, 2015
1 parent 04a4bbb commit 4afd815
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 4 deletions.
45 changes: 45 additions & 0 deletions addon/ng2/blueprints/ng2/files/karma-test-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Error.stackTraceLimit = Infinity;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

__karma__.loaded = function() {};

System.config({
packages: {
'base/dist/app': {
defaultExtension: false,
format: 'register',
map: Object.keys(window.__karma__.files)
.filter(onlyAppFiles)
.reduce(function(pathsMapping, appPath) {
var moduleName = appPath.replace(/^\/base\/dist\/app\//, './').replace(/\.js$/, '');
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
return pathsMapping;
}, {})
}
}
});

System.import('angular2/src/core/dom/browser_adapter').then(function(browser_adapter) {
// TODO: once beta is out we should change this code to use a "test platform"
browser_adapter.BrowserDomAdapter.makeCurrent();
}).then(function() {
return Promise.all(
Object.keys(window.__karma__.files)
.filter(onlySpecFiles)
.map(function(moduleName) {
return System.import(moduleName);
}));
}).then(function() {
__karma__.start();
}, function(error) {
__karma__.error(error.stack || error);
});

function onlyAppFiles(filePath) {
return /^\/base\/dist\/app\/(?!spec)([a-z0-9]+)\.js$/.test(filePath);
}

function onlySpecFiles(path) {
return /\.spec\.js$/.test(path);
}
38 changes: 38 additions & 0 deletions addon/ng2/blueprints/ng2/files/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/angular2.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/testing.js', included: true, watched: true},

{pattern: 'karma-test-shim.js', included: true, watched: true},

// paths loaded via module imports
{pattern: 'dist/**/*.js', included: false, watched: true},

// paths loaded via Angular's component compiler
// (these paths need to be rewritten, see proxies section)
{pattern: 'dist/**/*.html', included: false, watched: true},
{pattern: 'dist/**/*.css', included: false, watched: true},

// paths to support debugging with source maps in dev tools
{pattern: 'dist/**/*.ts', included: false, watched: false},
{pattern: 'dist/**/*.js.map', included: false, watched: false}
],
proxies: {
// required for component assests fetched by Angular's compiler
"/app/": "/base/dist/app/"
},
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
6 changes: 5 additions & 1 deletion addon/ng2/blueprints/ng2/files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"devDependencies": {
"angular-cli": "0.0.*",
"angular-cli-github-pages": "^0.2.0",
"ember-cli-inject-live-reload": "^1.3.0"
"ember-cli-inject-live-reload": "^1.3.0",
"jasmine-core": "^2.3.4",
"karma": "^0.13.15",
"karma-chrome-launcher": "^0.2.1",
"karma-jasmine": "^0.3.6"
}
}
22 changes: 22 additions & 0 deletions addon/ng2/blueprints/ng2/files/src/app/__name__.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {describe, it, expect, beforeEachProviders, inject} from 'angular2/testing';
import {<%= jsComponentName %>App} from '../app/<%= htmlComponentName %>';

beforeEachProviders(() => [<%= jsComponentName %>App]);

describe('App: <%= jsComponentName %>', () => {
it('should have the `defaultMeaning` as 42', inject([<%= jsComponentName %>App], (app) => {
expect(app.defaultMeaning).toBe(42);
}));

describe('#meaningOfLife', () => {
it('should get the meaning of life', inject([<%= jsComponentName %>App], (app) => {
expect(app.meaningOfLife()).toBe('The meaning of life is 42');
expect(app.meaningOfLife(22)).toBe('The meaning of life is 22');
}));
});

it('should be almost passing', function() {
expect(1 + 1).toBe(1);
});
});

8 changes: 5 additions & 3 deletions addon/ng2/blueprints/ng2/files/src/app/__name__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {Component} from 'angular2/angular2';
pipes: []
})
export class <%= jsComponentName %>App {

constructor() {}

defaultMeaning: number = 42;

meaningOfLife(meaning) {
return `The meaning of life is ${meaning || this.defaultMeaning}`;
}
}

0 comments on commit 4afd815

Please sign in to comment.