Skip to content

Commit af03742

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 angular#70
1 parent 2991dd7 commit af03742

File tree

6 files changed

+103
-50
lines changed

6 files changed

+103
-50
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/

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,11 @@ The build artifacts will be stored in the `dist/` directory.
115115

116116
### Running unit tests
117117

118-
Before running the tests make sure that the project is built. To build the
119-
project once you can use:
120-
121118
```bash
122-
ng build
119+
ng test
123120
```
124121

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

128124

129125
### Running end-to-end tests
@@ -139,9 +135,6 @@ $(npm bin)/tsc -p e2e/
139135
Afterwards you only need to run `$(npm bin)/protractor` while serving via
140136
`ng serve`.
141137

142-
This will be easier when the command
143-
[ng test](https://github.com/angular/angular-cli/issues/70) is implemented.
144-
145138

146139
### Deploying the app via GitHub Pages
147140

addon/ng2/commands/test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
commandOptions.configFile = karmaConfig;
58+
var karmaServer = new karma.Server(commandOptions, resolve);
59+
60+
karmaServer.start();
61+
});
62+
});
63+
}
64+
});
65+
66+
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: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,18 @@ describe('Basic end-to-end Workflow', function () {
5555
});
5656
});
5757

58-
it('Perform `ng test`', function(done) {
59-
this.timeout(30000);
60-
58+
it('Perform `ng test` after initial build', function() {
59+
this.timeout(300000);
6160
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();
61+
'test',
62+
'--single-run'
63+
]).then(function(result) {
64+
expect(result.exitCode).to.be.equal(0);
6765
});
6866
});
6967

7068
it('Can create a test component using `ng generate component test-component`', function() {
69+
this.timeout(10000);
7170
return ng([
7271
'generate',
7372
'component',
@@ -81,15 +80,14 @@ describe('Basic end-to-end Workflow', function () {
8180
});
8281
});
8382

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

8786
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();
87+
'test',
88+
'--single-run'
89+
]).then(function(result) {
90+
expect(result.exitCode).to.be.equal(0);
9391
});
9492
});
9593

@@ -106,15 +104,14 @@ describe('Basic end-to-end Workflow', function () {
106104
});
107105
});
108106

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

112110
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();
111+
'test',
112+
'--single-run'
113+
]).then(function(result) {
114+
expect(result.exitCode).to.be.equal(0);
118115
});
119116
});
120117

@@ -131,15 +128,14 @@ describe('Basic end-to-end Workflow', function () {
131128
});
132129
});
133130

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

137134
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();
135+
'test',
136+
'--single-run'
137+
]).then(function(result) {
138+
expect(result.exitCode).to.be.equal(0);
143139
});
144140
});
145141

@@ -165,20 +161,18 @@ describe('Basic end-to-end Workflow', function () {
165161
});
166162
});
167163

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

171167
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
168+
'test',
169+
'--single-run'
170+
]).then(function(result) {
171+
expect(result.exitCode).to.be.equal(0);
177172

173+
// Clean `tmp` folder
178174
process.chdir(path.resolve(root, '..'));
179175
sh.rm('-rf', './tmp'); // tmp.teardown takes too long
180-
181-
done();
182176
});
183177
});
184178

0 commit comments

Comments
 (0)