From 665fbd075b2a18b77dc5abef17b9475b5d6fd928 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Sat, 28 Nov 2015 12:25:36 -0500 Subject: [PATCH] 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 --- .gitignore | 3 +- .travis.yml | 8 ++ README.md | 11 +-- .../blueprints/ng2/files/karma-test-shim.js | 8 +- addon/ng2/blueprints/ng2/files/karma.conf.js | 7 +- addon/ng2/blueprints/ng2/files/package.json | 3 +- addon/ng2/commands/test.js | 71 +++++++++++++++++ addon/ng2/index.js | 3 +- tests/e2e/e2e_workflow.spec.js | 78 +++++++++---------- 9 files changed, 134 insertions(+), 58 deletions(-) create mode 100644 addon/ng2/commands/test.js diff --git a/.gitignore b/.gitignore index 2676228edb0a..a180e51682de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ .idea npm-debug.log -typings/ \ No newline at end of file +typings/ +tmp/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 5aafb9687986..a6ac4d55abfc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,15 +2,23 @@ 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" == "osx" ]]; then brew update; fi + - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install Caskroom/cask/firefox; 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 config set spin false + - npm config set progress false install: - node --version - npm --version diff --git a/README.md b/README.md index 15ffb77e43c6..fd2e719808d2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/addon/ng2/blueprints/ng2/files/karma-test-shim.js b/addon/ng2/blueprints/ng2/files/karma-test-shim.js index 286323376d55..a62ac95bc9b7 100644 --- a/addon/ng2/blueprints/ng2/files/karma-test-shim.js +++ b/addon/ng2/blueprints/ng2/files/karma-test-shim.js @@ -20,9 +20,11 @@ System.config({ } }); -System.import('angular2/platform/browser').then(function(browser_adapter) { - // TODO: once beta is out we should change this code to use a "test platform" - browser_adapter.BrowserDomAdapter.makeCurrent(); +System.import('angular2/testing').then(function(testing) { + return System.import('angular2/platform/testing/browser').then(function(providers) { + testing.setBaseTestProviders(providers.TEST_BROWSER_PLATFORM_PROVIDERS, + providers.TEST_BROWSER_APPLICATION_PROVIDERS); + }); }).then(function() { return Promise.all( Object.keys(window.__karma__.files) diff --git a/addon/ng2/blueprints/ng2/files/karma.conf.js b/addon/ng2/blueprints/ng2/files/karma.conf.js index d226b6e03261..b82e305364ef 100644 --- a/addon/ng2/blueprints/ng2/files/karma.conf.js +++ b/addon/ng2/blueprints/ng2/files/karma.conf.js @@ -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}, @@ -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}, @@ -40,7 +43,7 @@ module.exports = function(config) { colors: true, logLevel: config.LOG_INFO, autoWatch: true, - browsers: ['Chrome'], + browsers: ['Chrome', 'Firefox'], singleRun: false }); }; diff --git a/addon/ng2/blueprints/ng2/files/package.json b/addon/ng2/blueprints/ng2/files/package.json index 702e0019b1c2..2a9140ae7c2a 100644 --- a/addon/ng2/blueprints/ng2/files/package.json +++ b/addon/ng2/blueprints/ng2/files/package.json @@ -19,7 +19,7 @@ "es6-shim": "^0.33.3", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.0", - "systemjs": "0.19.4" + "systemjs": "0.19.20" }, "devDependencies": { "angular-cli": "0.0.*", @@ -29,6 +29,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", diff --git a/addon/ng2/commands/test.js b/addon/ng2/commands/test.js new file mode 100644 index 000000000000..bf77181e8c37 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/addon/ng2/index.js b/addon/ng2/index.js index 9cd064680ba8..d05efd8e9458 100644 --- a/addon/ng2/index.js +++ b/addon/ng2/index.js @@ -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') }; } }; diff --git a/tests/e2e/e2e_workflow.spec.js b/tests/e2e/e2e_workflow.spec.js index 658ff68224bc..0cecfbac19c7 100644 --- a/tests/e2e/e2e_workflow.spec.js +++ b/tests/e2e/e2e_workflow.spec.js @@ -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); @@ -56,19 +67,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', @@ -82,15 +91,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); }); }); @@ -107,15 +113,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); }); }); @@ -132,15 +135,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); }); }); @@ -166,20 +166,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(); }); });