Skip to content

Commit

Permalink
feat(command): ng test command runs karma
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Brocco committed Jan 30, 2016
1 parent e78adef commit 43a6ea4
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
.idea
npm-debug.log
typings/
typings/
tmp/
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,13 @@ The build artifacts will be stored in the `dist/` directory.

### Running tests

Before running the tests make sure that the project is built. To build the
project once you can use:

```bash
ng build
ng test
```

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

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


### Deploying the app via GitHub Pages
Expand Down
66 changes: 66 additions & 0 deletions addon/ng2/commands/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

var chalk = require('chalk');
var Command = require('ember-cli/lib/models/command');
var Promise = require('ember-cli/lib/ext/promise');
var Project = require('ember-cli/lib/models/project');
var SilentError = require('silent-error');
var validProjectName = require('ember-cli/lib/utilities/valid-project-name');
var normalizeBlueprint = require('ember-cli/lib/utilities/normalize-blueprint-option');

var TestCommand = require('ember-cli/lib/commands/test');
var win = require('ember-cli/lib/utilities/windows-admin');
var path = require('path');

// require dependencies within the target project
function requireDependency (root, moduleName) {
var packageJson = require(path.join(root, 'node_modules', moduleName, 'package.json'));
var main = path.normalize(packageJson.main);
return require(path.join(root, 'node_modules', moduleName, main));
}

module.exports = TestCommand.extend({
availableOptions: [
{ name: 'single-run', type: Boolean, default: true },
{ name: 'auto-watch', type: Boolean },
{ name: 'browsers', type: String },
{ name: 'colors', type: Boolean },
{ name: 'log-level', type: String },
{ name: 'port', type: Number },
{ name: 'reporters', type: String },
],

run: function(commandOptions, rawArgs) {
var BuildTask = this.tasks.Build;
var buildTask = new BuildTask({
ui: this.ui,
analytics: this.analytics,
project: this.project
});

var buildCommandOptions = {
environment: 'development',
outputPath: 'dist/',
watch: false
};

var projectRoot = this.project.root;

return win.checkWindowsElevation(this.ui)
.then(function() {
return buildTask.run(buildCommandOptions);
})
.then(function(){
return new Promise(function(resolve, reject){
var karma = requireDependency(projectRoot, 'karma');
var karmaConfig = path.join(projectRoot, 'karma.conf');
commandOptions.configFile = karmaConfig;
var karmaServer = new karma.Server(commandOptions, resolve);

karmaServer.start();
});
});
}
});

module.exports.overrideCore = true;
3 changes: 2 additions & 1 deletion addon/ng2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
'new' : require('./commands/new'),
'init' : require('./commands/init'),
'install' : require('./commands/install'),
'uninstall' : require('./commands/uninstall')
'uninstall' : require('./commands/uninstall'),
'test' : require('./commands/test')
};
}
};
2 changes: 0 additions & 2 deletions tests/acceptance/install.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ describe('Acceptance: ng install', function() {

function parsePackage(packageName) {
var packagePath = path.resolve(process.cwd(), 'node_modules', packageName, packageName + '.ts');

if (!existsSync(packagePath)) {
return false;
}

var contents = fs.readFileSync(packagePath, 'utf8');
var data = {};

data.Directive = [];
data.Pipe = [];
data.Provider = [];
Expand Down
68 changes: 68 additions & 0 deletions tests/acceptance/test.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

var fs = require('fs-extra');
var ng = require('../helpers/ng');
var existsSync = require('exists-sync');
var expect = require('chai').expect;
var forEach = require('lodash/collection/forEach');
var walkSync = require('walk-sync');
var Blueprint = require('ember-cli/lib/models/blueprint');
var path = require('path');
var tmp = require('../helpers/tmp');
var root = process.cwd();
var util = require('util');
var conf = require('ember-cli/tests/helpers/conf');
var EOL = require('os').EOL;
var rewire = require('rewire');
var sh = require('shelljs');

describe('Acceptance: ng test', function () {
before(conf.setup);

after(conf.restore);

var testProjectName = 'ng-test-test-proj';

it('Installs angular-cli correctly', function() {
this.timeout(300000);

return tmp.setup('./tmp')
.then(function () {
process.chdir('./tmp');

sh.exec('npm i angular-cli -g -C ' + process.cwd(), { silent: true });
expect(fs.existsSync(path.join(process.cwd(), 'bin', 'ng')));
});
});

it('Can create new project using `ng new [project name]`', function() {
this.timeout(300000);

return ng([
'new',
testProjectName,
'--silent'
]).then(function() {
expect(fs.existsSync(path.join(root, testProjectName)));
});
});

it('Can change current working directory', function() {
process.chdir(path.join(root, testProjectName));
expect(path.basename(process.cwd())).to.equal(testProjectName);
});

it('Can execute a test run', function() {
this.timeout(300000);
return ng([
'test',
'--single-run'
]).then(function(result) {
expect(result.exitCode).to.be.equal(0);

// Clean `tmp` folder
process.chdir(path.resolve(root, '..'));
sh.rm('-rf', './tmp'); // tmp.teardown takes too long
});
});
});
55 changes: 25 additions & 30 deletions tests/e2e/e2e_workflow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,18 @@ describe('Basic end-to-end Workflow', function () {
});
});

it('Perform `ng test`', function(done) {
this.timeout(30000);

it('Perform `ng test` after initial build', function() {
this.timeout(300000);
return ng([
'test'
]).then(function(err) {
// TODO when `ng test` will be implemented
//expect(err).to.be.equal(1);
done();
'test',
'--single-run'
]).then(function(result) {
expect(result.exitCode).to.be.equal(0);
});
});

it('Can create a test component using `ng generate component test-component`', function() {
this.timeout(10000);
return ng([
'generate',
'component',
Expand All @@ -81,15 +80,14 @@ describe('Basic end-to-end Workflow', function () {
});
});

it('Perform `ng test`', function(done) {
this.timeout(30000);
it('Perform `ng test` after adding a component', function() {
this.timeout(300000);

return ng([
'test'
]).then(function(err) {
// TODO when `ng test` will be implemented
//expect(err).to.be.equal(1);
done();
'test',
'--single-run'
]).then(function(result) {
expect(result.exitCode).to.be.equal(0);
});
});

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

it('Perform `ng test`', function(done) {
this.timeout(30000);
it('Perform `ng test` after adding a service', function() {
this.timeout(300000);

return ng([
'test'
]).then(function(err) {
// TODO when `ng test` will be implemented
//expect(err).to.be.equal(1);
done();
'test',
'--single-run'
]).then(function(result) {
expect(result.exitCode).to.be.equal(0);
});
});

Expand All @@ -131,20 +128,18 @@ describe('Basic end-to-end Workflow', function () {
});
});

it('Perform `ng test`', function(done) {
it('Perform `ng test` adding a pipe', function() {
this.timeout(300000);

return ng([
'test'
]).then(function(err) {
// TODO when `ng test` will be implemented
//expect(err).to.be.equal(1);
'test',
'--single-run'
]).then(function(result) {
expect(result.exitCode).to.be.equal(0);

// Clean `tmp` folder

process.chdir(path.resolve(root, '..'));
sh.rm('-rf', './tmp'); // tmp.teardown takes too long

done();
});
});

Expand Down

0 comments on commit 43a6ea4

Please sign in to comment.