Skip to content

Commit a734a31

Browse files
Add Karma/Jasmine/Chai test starting point to Angular2Spa
1 parent ea81671 commit a734a31

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference path="../../../../node_modules/@types/jasmine/index.d.ts" />
2+
import { assert } from 'chai';
3+
import { CounterComponent } from './counter.component';
4+
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
5+
6+
let fixture: ComponentFixture<CounterComponent>;
7+
8+
describe('Counter component', () => {
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({ declarations: [CounterComponent] });
11+
fixture = TestBed.createComponent(CounterComponent);
12+
fixture.detectChanges();
13+
});
14+
15+
it('should display a title', async(() => {
16+
const titleText = fixture.nativeElement.querySelector('h2').textContent;
17+
expect(titleText).toEqual('Counter');
18+
}));
19+
20+
it('should start with count 0, then increments by 1 when clicked', async(() => {
21+
const countElement = fixture.nativeElement.querySelector('strong');
22+
expect(countElement.textContent).toEqual('0');
23+
24+
const incrementButton = fixture.nativeElement.querySelector('button');
25+
incrementButton.click();
26+
fixture.detectChanges();
27+
expect(countElement.textContent).toEqual('1');
28+
}));
29+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Load required polyfills and testing libraries
2+
import 'angular2-universal-polyfills';
3+
import 'zone.js/dist/long-stack-trace-zone';
4+
import 'zone.js/dist/proxy.js';
5+
import 'zone.js/dist/sync-test';
6+
import 'zone.js/dist/jasmine-patch';
7+
import 'zone.js/dist/async-test';
8+
import 'zone.js/dist/fake-async-test';
9+
import * as testing from '@angular/core/testing';
10+
import * as testingBrowser from '@angular/platform-browser-dynamic/testing';
11+
12+
// There's no typing for the `__karma__` variable. Just declare it as any
13+
declare var __karma__: any;
14+
declare var require: any;
15+
16+
// Prevent Karma from running prematurely
17+
__karma__.loaded = function () {};
18+
19+
// First, initialize the Angular testing environment
20+
testing.getTestBed().initTestEnvironment(
21+
testingBrowser.BrowserDynamicTestingModule,
22+
testingBrowser.platformBrowserDynamicTesting()
23+
);
24+
25+
// Then we find all the tests
26+
const context = require.context('../', true, /\.spec\.ts$/);
27+
28+
// And load the modules
29+
context.keys().map(context);
30+
31+
// Finally, start Karma to run the tests
32+
__karma__.start();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Karma configuration file, see link for more information
2+
// https://karma-runner.github.io/0.13/config/configuration-file.html
3+
4+
module.exports = function (config) {
5+
config.set({
6+
basePath: '.',
7+
frameworks: ['jasmine'],
8+
files: [
9+
'../../wwwroot/dist/vendor.js',
10+
'./boot-tests.ts'
11+
],
12+
preprocessors: {
13+
'./boot-tests.ts': ['webpack']
14+
},
15+
reporters: ['progress'],
16+
port: 9876,
17+
colors: true,
18+
logLevel: config.LOG_INFO,
19+
autoWatch: true,
20+
browsers: ['Chrome'],
21+
singleRun: false,
22+
webpack: require('../../webpack.config.js').filter(config => config.target !== 'node'), // Test against client bundle, because tests run in a browser
23+
webpackMiddleware: { stats: 'errors-only' }
24+
});
25+
};

templates/Angular2Spa/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "Angular2Spa",
33
"version": "0.0.0",
4+
"scripts": {
5+
"test": "karma start ClientApp/test/karma.conf.js"
6+
},
47
"dependencies": {
58
"@angular/common": "2.0.2",
69
"@angular/compiler": "2.0.2",
@@ -42,5 +45,17 @@
4245
"webpack-merge": "^0.14.1",
4346
"webpack-node-externals": "^1.4.3",
4447
"zone.js": "^0.6.25"
48+
},
49+
"devDependencies": {
50+
"@types/chai": "^3.4.34",
51+
"@types/jasmine": "^2.5.37",
52+
"chai": "^3.5.0",
53+
"jasmine-core": "^2.5.2",
54+
"karma": "^1.3.0",
55+
"karma-chai": "^0.1.0",
56+
"karma-chrome-launcher": "^2.0.0",
57+
"karma-cli": "^1.0.1",
58+
"karma-jasmine": "^1.0.2",
59+
"karma-webpack": "^1.8.0"
4560
}
4661
}

templates/Angular2Spa/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;
77

88
// Configuration in common to both client-side and server-side bundles
99
var sharedConfig = {
10+
context: __dirname,
1011
resolve: { extensions: [ '', '.js', '.ts' ] },
1112
output: {
1213
filename: '[name].js',

0 commit comments

Comments
 (0)