Skip to content

Commit 665fbd0

Browse files
committed
feat(command): ng test command runs karma
Overrode the ember-cli test command to initialize the karma server Then start the karma server based upon the karma.conf.js config file closes #70
1 parent 94c9746 commit 665fbd0

File tree

9 files changed

+134
-58
lines changed

9 files changed

+134
-58
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
.idea
33
npm-debug.log
4-
typings/
4+
typings/
5+
tmp/

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ sudo: false
22
env:
33
- NODE_VERSION=4
44
- NODE_VERSION=5
5+
56
os:
67
- linux
78
- osx
89
script: npm run-script test
10+
addons:
11+
firefox: "latest"
912
before_install:
1013
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash; fi
1114
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then source ~/.nvm/nvm-exec; fi
15+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
16+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install Caskroom/cask/firefox; fi
17+
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0; fi
18+
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi
1219
- nvm install $NODE_VERSION
1320
- npm config set spin false
21+
- npm config set progress false
1422
install:
1523
- node --version
1624
- npm --version

README.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,11 @@ ng install ng2-cli-test-lib
137137

138138
### Running unit tests
139139

140-
Before running the tests make sure that the project is built. To build the
141-
project once you can use:
142-
143140
```bash
144-
ng build
141+
ng test
145142
```
146143

147-
With the project built in the `dist/` folder you can just run: `karma start`.
148-
Karma will run the tests and keep the browser open waiting to run again.
144+
Tests will execute after a build is executed via [Karma](http://karma-runner.github.io/0.13/index.html)
149145

150146

151147
### Running end-to-end tests
@@ -161,9 +157,6 @@ $(npm bin)/tsc -p e2e/
161157
Afterwards you only need to run `$(npm bin)/protractor` while serving via
162158
`ng serve`.
163159

164-
This will be easier when the command
165-
[ng test](https://github.com/angular/angular-cli/issues/70) is implemented.
166-
167160

168161
### Deploying the app via GitHub Pages
169162

addon/ng2/blueprints/ng2/files/karma-test-shim.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ System.config({
2020
}
2121
});
2222

23-
System.import('angular2/platform/browser').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();
23+
System.import('angular2/testing').then(function(testing) {
24+
return System.import('angular2/platform/testing/browser').then(function(providers) {
25+
testing.setBaseTestProviders(providers.TEST_BROWSER_PLATFORM_PROVIDERS,
26+
providers.TEST_BROWSER_APPLICATION_PROVIDERS);
27+
});
2628
}).then(function() {
2729
return Promise.all(
2830
Object.keys(window.__karma__.files)

addon/ng2/blueprints/ng2/files/karma.conf.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = function(config) {
44
frameworks: ['jasmine'],
55
plugins: [
66
require('karma-jasmine'),
7-
require('karma-chrome-launcher')
7+
require('karma-chrome-launcher'),
8+
require('karma-firefox-launcher')
89
],
910
files: [
1011
{pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', included: true, watched: true},
@@ -14,6 +15,8 @@ module.exports = function(config) {
1415
{pattern: 'node_modules/angular2/bundles/http.dev.js', included: true, watched: true},
1516
{pattern: 'node_modules/angular2/bundles/router.dev.js', included: true, watched: true},
1617
{pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true},
18+
{pattern: 'node_modules/es6-shim/es6-shim.js', included: true, watched: true},
19+
{pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: true, watched: true},
1720

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

@@ -40,7 +43,7 @@ module.exports = function(config) {
4043
colors: true,
4144
logLevel: config.LOG_INFO,
4245
autoWatch: true,
43-
browsers: ['Chrome'],
46+
browsers: ['Chrome', 'Firefox'],
4447
singleRun: false
4548
});
4649
};

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"es6-shim": "^0.33.3",
2020
"reflect-metadata": "0.1.2",
2121
"rxjs": "5.0.0-beta.0",
22-
"systemjs": "0.19.4"
22+
"systemjs": "0.19.20"
2323
},
2424
"devDependencies": {
2525
"angular-cli": "0.0.*",
@@ -29,6 +29,7 @@
2929
"jasmine-core": "^2.3.4",
3030
"karma": "^0.13.15",
3131
"karma-chrome-launcher": "^0.2.1",
32+
"karma-firefox-launcher": "^0.1.7",
3233
"karma-jasmine": "^0.3.6",
3334
"protractor": "^3.0.0",
3435
"typescript": "^1.7.3",

addon/ng2/commands/test.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
var chalk = require('chalk');
4+
var Command = require('ember-cli/lib/models/command');
5+
var Promise = require('ember-cli/lib/ext/promise');
6+
var Project = require('ember-cli/lib/models/project');
7+
var SilentError = require('silent-error');
8+
var validProjectName = require('ember-cli/lib/utilities/valid-project-name');
9+
var normalizeBlueprint = require('ember-cli/lib/utilities/normalize-blueprint-option');
10+
11+
var TestCommand = require('ember-cli/lib/commands/test');
12+
var win = require('ember-cli/lib/utilities/windows-admin');
13+
var path = require('path');
14+
15+
// require dependencies within the target project
16+
function requireDependency (root, moduleName) {
17+
var packageJson = require(path.join(root, 'node_modules', moduleName, 'package.json'));
18+
var main = path.normalize(packageJson.main);
19+
return require(path.join(root, 'node_modules', moduleName, main));
20+
}
21+
22+
module.exports = TestCommand.extend({
23+
availableOptions: [
24+
{ name: 'single-run', type: Boolean, default: true },
25+
{ name: 'auto-watch', type: Boolean },
26+
{ name: 'browsers', type: String },
27+
{ name: 'colors', type: Boolean },
28+
{ name: 'log-level', type: String },
29+
{ name: 'port', type: Number },
30+
{ name: 'reporters', type: String },
31+
],
32+
33+
run: function(commandOptions, rawArgs) {
34+
var BuildTask = this.tasks.Build;
35+
var buildTask = new BuildTask({
36+
ui: this.ui,
37+
analytics: this.analytics,
38+
project: this.project
39+
});
40+
41+
var buildCommandOptions = {
42+
environment: 'development',
43+
outputPath: 'dist/',
44+
watch: false
45+
};
46+
47+
var projectRoot = this.project.root;
48+
49+
return win.checkWindowsElevation(this.ui)
50+
.then(function() {
51+
return buildTask.run(buildCommandOptions);
52+
})
53+
.then(function(){
54+
return new Promise(function(resolve, reject){
55+
var karma = requireDependency(projectRoot, 'karma');
56+
var karmaConfig = path.join(projectRoot, 'karma.conf');
57+
58+
// Convert browsers from a string to an array
59+
if (commandOptions.browsers){
60+
commandOptions.browsers = commandOptions.browsers.split(',');
61+
}
62+
commandOptions.configFile = karmaConfig;
63+
var karmaServer = new karma.Server(commandOptions, resolve);
64+
65+
karmaServer.start();
66+
});
67+
});
68+
}
69+
});
70+
71+
module.exports.overrideCore = true;

addon/ng2/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = {
88
'new' : require('./commands/new'),
99
'init' : require('./commands/init'),
1010
'install' : require('./commands/install'),
11-
'uninstall' : require('./commands/uninstall')
11+
'uninstall' : require('./commands/uninstall'),
12+
'test' : require('./commands/test')
1213
};
1314
}
1415
};

tests/e2e/e2e_workflow.spec.js

+37-41
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ describe('Basic end-to-end Workflow', function () {
1515

1616
after(conf.restore);
1717

18+
var testArgs = [
19+
'test',
20+
'--single-run'
21+
];
22+
23+
// In travis CI only run tests in Firefox
24+
if (process.env.TRAVIS) {
25+
testArgs.push('--browsers');
26+
testArgs.push('Firefox');
27+
}
28+
1829
it('Installs angular-cli correctly', function() {
1930
this.timeout(300000);
2031

@@ -56,19 +67,17 @@ describe('Basic end-to-end Workflow', function () {
5667
});
5768
});
5869

59-
it('Perform `ng test`', function(done) {
60-
this.timeout(30000);
70+
it('Perform `ng test` after initial build', function() {
71+
this.timeout(300000);
6172

62-
return ng([
63-
'test'
64-
]).then(function(err) {
65-
// TODO when `ng test` will be implemented
66-
//expect(err).to.be.equal(1);
67-
done();
73+
return ng(testArgs)
74+
.then(function(result) {
75+
expect(result.exitCode).to.be.equal(0);
6876
});
6977
});
7078

7179
it('Can create a test component using `ng generate component test-component`', function() {
80+
this.timeout(10000);
7281
return ng([
7382
'generate',
7483
'component',
@@ -82,15 +91,12 @@ describe('Basic end-to-end Workflow', function () {
8291
});
8392
});
8493

85-
it('Perform `ng test`', function(done) {
86-
this.timeout(30000);
94+
it('Perform `ng test` after adding a component', function() {
95+
this.timeout(300000);
8796

88-
return ng([
89-
'test'
90-
]).then(function(err) {
91-
// TODO when `ng test` will be implemented
92-
//expect(err).to.be.equal(1);
93-
done();
97+
return ng(testArgs)
98+
.then(function(result) {
99+
expect(result.exitCode).to.be.equal(0);
94100
});
95101
});
96102

@@ -107,15 +113,12 @@ describe('Basic end-to-end Workflow', function () {
107113
});
108114
});
109115

110-
it('Perform `ng test`', function(done) {
111-
this.timeout(30000);
116+
it('Perform `ng test` after adding a service', function() {
117+
this.timeout(300000);
112118

113-
return ng([
114-
'test'
115-
]).then(function(err) {
116-
// TODO when `ng test` will be implemented
117-
//expect(err).to.be.equal(1);
118-
done();
119+
return ng(testArgs)
120+
.then(function(result) {
121+
expect(result.exitCode).to.be.equal(0);
119122
});
120123
});
121124

@@ -132,15 +135,12 @@ describe('Basic end-to-end Workflow', function () {
132135
});
133136
});
134137

135-
it('Perform `ng test`', function(done) {
136-
this.timeout(30000);
138+
it('Perform `ng test` after adding a pipe', function() {
139+
this.timeout(300000);
137140

138-
return ng([
139-
'test'
140-
]).then(function(err) {
141-
// TODO when `ng test` will be implemented
142-
//expect(err).to.be.equal(1);
143-
done();
141+
return ng(testArgs)
142+
.then(function(result) {
143+
expect(result.exitCode).to.be.equal(0);
144144
});
145145
});
146146

@@ -166,20 +166,16 @@ describe('Basic end-to-end Workflow', function () {
166166
});
167167
});
168168

169-
it('Perform `ng test`', function(done) {
169+
it('Perform `ng test` after adding a route', function() {
170170
this.timeout(300000);
171171

172-
return ng([
173-
'test'
174-
]).then(function(err) {
175-
// TODO when `ng test` will be implemented
176-
//expect(err).to.be.equal(1);
177-
// Clean `tmp` folder
172+
return ng(testArgs)
173+
.then(function(result) {
174+
expect(result.exitCode).to.be.equal(0);
178175

176+
// Clean `tmp` folder
179177
process.chdir(path.resolve(root, '..'));
180178
sh.rm('-rf', './tmp'); // tmp.teardown takes too long
181-
182-
done();
183179
});
184180
});
185181

0 commit comments

Comments
 (0)