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 #70
  • Loading branch information
Brocco committed Feb 6, 2016
1 parent f751830 commit 9cf843d
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
node_modules/
.idea
npm-debug.log
typings/
typings/
tmp/
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -2,13 +2,18 @@ sudo: false
env:
- NODE_VERSION=4
- NODE_VERSION=5

os:
- linux
- osx
script: npm run-script test
addons:
firefox: "latest"
before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then source ~/.nvm/nvm-exec; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi
- nvm install $NODE_VERSION
- npm link npm
- npm config set spin false
Expand Down
11 changes: 2 additions & 9 deletions README.md
Expand Up @@ -137,15 +137,11 @@ ng install ng2-cli-test-lib

### Running unit 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)


### Running end-to-end tests
Expand All @@ -161,9 +157,6 @@ $(npm bin)/tsc -p e2e/
Afterwards you only need to run `$(npm bin)/protractor` while serving via
`ng serve`.

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


### Deploying the app via GitHub Pages

Expand Down
7 changes: 5 additions & 2 deletions addon/ng2/blueprints/ng2/files/karma.conf.js
Expand Up @@ -4,7 +4,8 @@ module.exports = function(config) {
frameworks: ['jasmine'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher')
require('karma-chrome-launcher'),
require('karma-firefox-launcher')
],
files: [
{pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', included: true, watched: true},
Expand All @@ -14,6 +15,8 @@ module.exports = function(config) {
{pattern: 'node_modules/angular2/bundles/http.dev.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/router.dev.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true},
{pattern: 'node_modules/es6-shim/es6-shim.js', included: true, watched: true},
{pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: true, watched: true},

{pattern: 'karma-test-shim.js', included: true, watched: true},

Expand All @@ -40,7 +43,7 @@ module.exports = function(config) {
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
browsers: ['Chrome', 'Firefox'],
singleRun: false
});
};
1 change: 1 addition & 0 deletions addon/ng2/blueprints/ng2/files/package.json
Expand Up @@ -25,6 +25,7 @@
"jasmine-core": "^2.3.4",
"karma": "^0.13.15",
"karma-chrome-launcher": "^0.2.1",
"karma-firefox-launcher": "^0.1.7",
"karma-jasmine": "^0.3.6",
"protractor": "^3.0.0",
"typescript": "^1.7.3"
Expand Down
@@ -1,13 +1,15 @@
<div *ngIf="<%= camelizedModuleName %>">
<h3>"{{editName}}"</h3>
<div>
<label>Id:</label>
{{<%= camelizedModuleName %>.id}}
<div>
<div *ngIf="<%= camelizedModuleName %>">
<h3>"{{editName}}"</h3>
<div>
<label>Id:</label>
{{<%= camelizedModuleName %>.id}}
</div>
<div>
<label>Name:</label>
<input [(ngModel)]="editName" placeholder="name"/>
</div>
<button (click)="save()">Save</button>
<button (click)="cancel()">Cancel</button>
</div>
<div>
<label>Name:</label>
<input [(ngModel)]="editName" placeholder="name"/>
</div>
<button (click)="save()">Save</button>
<button (click)="cancel()">Cancel</button>
</div>
71 changes: 71 additions & 0 deletions addon/ng2/commands/test.js
@@ -0,0 +1,71 @@
'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');

// Convert browsers from a string to an array
if (commandOptions.browsers){
commandOptions.browsers = commandOptions.browsers.split(',');
}
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
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
Expand Up @@ -32,14 +32,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
78 changes: 37 additions & 41 deletions tests/e2e/e2e_workflow.spec.js
Expand Up @@ -15,6 +15,17 @@ describe('Basic end-to-end Workflow', function () {

after(conf.restore);

var testArgs = [
'test',
'--single-run'
];

// In travis CI only run tests in Firefox
if (process.env.TRAVIS) {
testArgs.push('--browsers');
testArgs.push('Firefox');
}

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

Expand Down Expand Up @@ -55,19 +66,17 @@ 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();
return ng(testArgs)
.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 +90,12 @@ 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();
return ng(testArgs)
.then(function(result) {
expect(result.exitCode).to.be.equal(0);
});
});

Expand All @@ -106,15 +112,12 @@ 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();
return ng(testArgs)
.then(function(result) {
expect(result.exitCode).to.be.equal(0);
});
});

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

it('Perform `ng test`', function(done) {
this.timeout(30000);
it('Perform `ng test` after 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);
done();
return ng(testArgs)
.then(function(result) {
expect(result.exitCode).to.be.equal(0);
});
});

Expand All @@ -165,20 +165,16 @@ describe('Basic end-to-end Workflow', function () {
});
});

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

return ng([
'test'
]).then(function(err) {
// TODO when `ng test` will be implemented
//expect(err).to.be.equal(1);
// Clean `tmp` folder
return ng(testArgs)
.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 9cf843d

Please sign in to comment.