Skip to content

Commit 3d15e3f

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 10eae9e commit 3d15e3f

File tree

4 files changed

+121
-9
lines changed

4 files changed

+121
-9
lines changed

README.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,13 @@ The build artifacts will be stored in the `dist/` directory.
6464

6565
### Running tests
6666

67-
Before running the tests make sure that the project is built. To build the
68-
project once you can use:
69-
7067
```bash
71-
ng build
68+
ng test
7269
```
7370

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

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

8075

8176
### Deploying the app via GitHub Pages

addon/ng2/commands/test.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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(){
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);
59+
karmaServer.start();
60+
});
61+
});
62+
}
63+
});
64+
65+
module.exports.overrideCore = true;

addon/ng2/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module.exports = {
66
includedCommands: function() {
77
return {
88
'new': require('./commands/new'),
9-
'init': require('./commands/init')
9+
'init': require('./commands/init'),
10+
'test': require('./commands/test')
1011
};
1112
}
1213
};

tests/acceptance/test.spec.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
var fs = require('fs-extra');
4+
var ng = require('../helpers/ng');
5+
var existsSync = require('exists-sync');
6+
var expect = require('chai').expect;
7+
var forEach = require('lodash/collection/forEach');
8+
var walkSync = require('walk-sync');
9+
var Blueprint = require('ember-cli/lib/models/blueprint');
10+
var path = require('path');
11+
var tmp = require('../helpers/tmp');
12+
var root = process.cwd();
13+
var util = require('util');
14+
var conf = require('ember-cli/tests/helpers/conf');
15+
var EOL = require('os').EOL;
16+
var rewire = require('rewire');
17+
18+
xdescribe('Acceptance: ng test', function () {
19+
before(conf.setup);
20+
21+
after(conf.restore);
22+
23+
beforeEach(function () {
24+
return tmp.setup('./tmp')
25+
.then(function () {
26+
process.chdir('./tmp');
27+
})
28+
.then(function () {
29+
return ng([
30+
'init',
31+
'--skip-npm',
32+
'--skip-bower'
33+
]);
34+
});
35+
});
36+
37+
afterEach(function () {
38+
return tmp.teardown('./tmp');
39+
});
40+
41+
it('ng test should excecute a test', function () {
42+
console.log('starting test');
43+
return ng([
44+
'test',
45+
'--single-run'
46+
])
47+
.then(function () {
48+
console.log('test completed');
49+
});
50+
});
51+
});

0 commit comments

Comments
 (0)