Skip to content

Commit 9c6cad9

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 e78adef commit 9c6cad9

File tree

6 files changed

+98
-42
lines changed

6 files changed

+98
-42
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: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,13 @@ The build artifacts will be stored in the `dist/` directory.
7171

7272
### Running tests
7373

74-
Before running the tests make sure that the project is built. To build the
75-
project once you can use:
76-
7774
```bash
78-
ng build
75+
ng test
7976
```
8077

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

84-
This will be easier when the command
85-
[ng test](https://github.com/angular/angular-cli/issues/70) is implemented.
80+
End to end tests will be available via `ng test` once implemented ([reference](https://github.com/angular/angular-cli/issues/103))
8681

8782

8883
### Deploying the app via GitHub Pages

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
@@ -44,14 +44,12 @@ describe('Acceptance: ng install', function() {
4444

4545
function parsePackage(packageName) {
4646
var packagePath = path.resolve(process.cwd(), 'node_modules', packageName, packageName + '.ts');
47-
4847
if (!existsSync(packagePath)) {
4948
return false;
5049
}
5150

5251
var contents = fs.readFileSync(packagePath, 'utf8');
5352
var data = {};
54-
5553
data.Directive = [];
5654
data.Pipe = [];
5755
data.Provider = [];

tests/e2e/e2e_workflow.spec.js

Lines changed: 25 additions & 30 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,20 +128,18 @@ describe('Basic end-to-end Workflow', function () {
131128
});
132129
});
133130

134-
it('Perform `ng test`', function(done) {
131+
it('Perform `ng test` adding a pipe', function() {
135132
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);
135+
'test',
136+
'--single-run'
137+
]).then(function(result) {
138+
expect(result.exitCode).to.be.equal(0);
139+
142140
// Clean `tmp` folder
143-
144141
process.chdir(path.resolve(root, '..'));
145142
sh.rm('-rf', './tmp'); // tmp.teardown takes too long
146-
147-
done();
148143
});
149144
});
150145

0 commit comments

Comments
 (0)