Skip to content

Commit 9cf843d

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 f751830 commit 9cf843d

File tree

10 files changed

+138
-67
lines changed

10 files changed

+138
-67
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ 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" == "linux" ]]; then export DISPLAY=:99.0; fi
16+
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi
1217
- nvm install $NODE_VERSION
1318
- npm link npm
1419
- npm config set spin false

README.md

Lines changed: 2 additions & 9 deletions
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.conf.js

Lines changed: 5 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"jasmine-core": "^2.3.4",
2626
"karma": "^0.13.15",
2727
"karma-chrome-launcher": "^0.2.1",
28+
"karma-firefox-launcher": "^0.1.7",
2829
"karma-jasmine": "^0.3.6",
2930
"protractor": "^3.0.0",
3031
"typescript": "^1.7.3"
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
<div *ngIf="<%= camelizedModuleName %>">
2-
<h3>"{{editName}}"</h3>
3-
<div>
4-
<label>Id:</label>
5-
{{<%= camelizedModuleName %>.id}}
1+
<div>
2+
<div *ngIf="<%= camelizedModuleName %>">
3+
<h3>"{{editName}}"</h3>
4+
<div>
5+
<label>Id:</label>
6+
{{<%= camelizedModuleName %>.id}}
7+
</div>
8+
<div>
9+
<label>Name:</label>
10+
<input [(ngModel)]="editName" placeholder="name"/>
11+
</div>
12+
<button (click)="save()">Save</button>
13+
<button (click)="cancel()">Cancel</button>
614
</div>
7-
<div>
8-
<label>Name:</label>
9-
<input [(ngModel)]="editName" placeholder="name"/>
10-
</div>
11-
<button (click)="save()">Save</button>
12-
<button (click)="cancel()">Cancel</button>
1315
</div>

addon/ng2/commands/test.js

Lines changed: 71 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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/acceptance/install.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ describe('Acceptance: ng install', function() {
3232

3333
function parsePackage(packageName) {
3434
var packagePath = path.resolve(process.cwd(), 'node_modules', packageName, packageName + '.ts');
35-
3635
if (!existsSync(packagePath)) {
3736
return false;
3837
}
3938

4039
var contents = fs.readFileSync(packagePath, 'utf8');
4140
var data = {};
42-
4341
data.Directive = [];
4442
data.Pipe = [];
4543
data.Provider = [];

tests/e2e/e2e_workflow.spec.js

Lines changed: 37 additions & 41 deletions
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

@@ -55,19 +66,17 @@ describe('Basic end-to-end Workflow', function () {
5566
});
5667
});
5768

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

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

7078
it('Can create a test component using `ng generate component test-component`', function() {
79+
this.timeout(10000);
7180
return ng([
7281
'generate',
7382
'component',
@@ -81,15 +90,12 @@ describe('Basic end-to-end Workflow', function () {
8190
});
8291
});
8392

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

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

@@ -106,15 +112,12 @@ describe('Basic end-to-end Workflow', function () {
106112
});
107113
});
108114

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

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

@@ -131,15 +134,12 @@ describe('Basic end-to-end Workflow', function () {
131134
});
132135
});
133136

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

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

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

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

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

175+
// Clean `tmp` folder
178176
process.chdir(path.resolve(root, '..'));
179177
sh.rm('-rf', './tmp'); // tmp.teardown takes too long
180-
181-
done();
182178
});
183179
});
184180

0 commit comments

Comments
 (0)