From b81c064624883e58e2ee817e109a741f9087f9fc Mon Sep 17 00:00:00 2001 From: Mario Romano Date: Fri, 13 May 2016 00:36:17 +0100 Subject: [PATCH] #62 add coverage and tests --- .../ng2-alfresco-login/karma.conf.js | 128 ++++++++++++++++++ .../ng2-alfresco-login/package.json | 19 ++- .../assets/authentication.service.mock.ts | 0 .../assets/translation.service.mock.ts | 0 .../components/alfresco-login.spec.ts | 0 .../services/alfresco-authentication.spec.ts | 0 .../ng2-alfresco-login/test-main.js | 78 +++++++++++ .../ng2-alfresco-login/test/.gitignore | 3 - .../ng2-alfresco-login/test/index.html | 65 --------- .../ng2-alfresco-login/test/tsconfig.json | 16 --- .../ng2-alfresco-login/tsconfig.json | 51 ++++--- 11 files changed, 246 insertions(+), 114 deletions(-) create mode 100644 ng2-components/ng2-alfresco-login/karma.conf.js rename ng2-components/ng2-alfresco-login/{test => src}/assets/authentication.service.mock.ts (100%) rename ng2-components/ng2-alfresco-login/{test => src}/assets/translation.service.mock.ts (100%) rename ng2-components/ng2-alfresco-login/{test => src}/components/alfresco-login.spec.ts (100%) rename ng2-components/ng2-alfresco-login/{test => src}/services/alfresco-authentication.spec.ts (100%) create mode 100644 ng2-components/ng2-alfresco-login/test-main.js delete mode 100644 ng2-components/ng2-alfresco-login/test/.gitignore delete mode 100644 ng2-components/ng2-alfresco-login/test/index.html delete mode 100644 ng2-components/ng2-alfresco-login/test/tsconfig.json diff --git a/ng2-components/ng2-alfresco-login/karma.conf.js b/ng2-components/ng2-alfresco-login/karma.conf.js new file mode 100644 index 0000000000..4fc94d241f --- /dev/null +++ b/ng2-components/ng2-alfresco-login/karma.conf.js @@ -0,0 +1,128 @@ +// Karma configuration +// Generated on Wed Jul 15 2015 09:44:02 GMT+0200 (Romance Daylight Time) +'use strict'; + +var argv = require('yargs').argv; + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: './', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['jasmine'], + + + // list of files / patterns to load in the browser + files: [ + 'node_modules/zone.js/dist/zone.js', + 'node_modules/zone.js/dist/long-stack-trace-zone.js', + 'node_modules/zone.js/dist/jasmine-patch.js', + 'node_modules/zone.js/dist/async-test.js', + 'node_modules/zone.js/dist/fake-async-test.js', + 'node_modules/es6-module-loader/dist/es6-module-loader.js', + 'node_modules/traceur/bin/traceur-runtime.js', // Required by PhantomJS2, otherwise it shouts ReferenceError: Can't find variable: require + 'node_modules/traceur/bin/traceur.js', + 'node_modules/systemjs/dist/system.src.js', + 'node_modules/reflect-metadata/Reflect.js', + 'node_modules/angular2/es6/dev/src/testing/shims_for_IE.js', + { pattern: 'node_modules/ng2-translate/**/*.js', included: false, watched: false }, // source files + { pattern: 'node_modules/angular2/**/*.js', included: false, watched: false }, + { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false }, + { pattern: 'dist/**/*.js', included: false, watched: true }, + { pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: false, watched: false }, // PhantomJS2 (and possibly others) might require it + + // suppress annoying 404 warnings for resources, images, etc. + //{ pattern: 'dist/src/assets/**/*', watched: false, included: false, served: true }, + + 'test-main.js' + ], + + // must go along with above, suppress annoying 404 warnings. + proxies: { + '/assets/': '/base/dist/dev/assets/' + }, + + // list of files to exclude + exclude: [ + 'node_modules/angular2/**/*spec.js' + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + 'dist/**/!(*spec).js': ['coverage'] + }, + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['mocha', 'coverage'], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: [ + 'PhantomJS', + 'Chrome' + ], + + + customLaunchers: { + Chrome_travis_ci: { + base: 'Chrome', + flags: ['--no-sandbox'] + } + }, + + coverageReporter: { + dir: 'coverage/', + reporters: [ + { type: 'text-summary' }, + { type: 'json', subdir: '.', file: 'coverage-final.json' }, + { type: 'html' } + ] + }, + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: false, + + // Passing command line arguments to tests + client: { + files: argv.files + } + }); + + if (process.env.APPVEYOR) { + config.browsers = ['IE']; + config.singleRun = true; + config.browserNoActivityTimeout = 90000; // Note: default value (10000) is not enough + } + + if (process.env.TRAVIS || process.env.CIRCLECI) { + config.browsers = ['Chrome_travis_ci']; + config.singleRun = true; + } +}; diff --git a/ng2-components/ng2-alfresco-login/package.json b/ng2-components/ng2-alfresco-login/package.json index c6919e2994..00c7cfb032 100644 --- a/ng2-components/ng2-alfresco-login/package.json +++ b/ng2-components/ng2-alfresco-login/package.json @@ -12,8 +12,7 @@ "tsc": "tsc", "tsc:w": "tsc -w", "typings": "typings", - "test": "live-server --open=index.html --entry-file=test/ --mount=/ng2-alfresco-core:../ng2-alfresco-core", - "test:w": "concurrently \"npm run tsc:w\" \"npm run test\" " + "test": "karma start karma.conf.js" }, "repository": { "type": "git", @@ -43,9 +42,10 @@ "angular2": "2.0.0-beta.15", "systemjs": "0.19.26", "es6-shim": "^0.35.0", + "es6-module-loader": "^0.17.8", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.2", - "zone.js": "0.6.10", + "zone.js": "^0.6.12", "ng2-translate": "^1.11.2" }, "peerDependencies": { @@ -59,7 +59,18 @@ "typings": "^0.7.12", "license-check": "^1.0.4", "tslint": "^3.8.1", - "concurrently": "^2.0.0" + "concurrently": "^2.0.0", + "jasmine-spec-reporter": "^2.4.0", + "karma": "~0.13.22", + "karma-chrome-launcher": "~1.0.1", + "karma-coverage": "^1.0.0", + "karma-ie-launcher": "^1.0.0", + "karma-jasmine": "~1.0.2", + "karma-mocha-reporter": "^2.0.3", + "karma-phantomjs-launcher": "^1.0.0", + "traceur": "^0.0.91", + "remap-istanbul": "^0.6.3", + "phantomjs-prebuilt": "^2.1.7" }, "license-check-config": { "src": [ diff --git a/ng2-components/ng2-alfresco-login/test/assets/authentication.service.mock.ts b/ng2-components/ng2-alfresco-login/src/assets/authentication.service.mock.ts similarity index 100% rename from ng2-components/ng2-alfresco-login/test/assets/authentication.service.mock.ts rename to ng2-components/ng2-alfresco-login/src/assets/authentication.service.mock.ts diff --git a/ng2-components/ng2-alfresco-login/test/assets/translation.service.mock.ts b/ng2-components/ng2-alfresco-login/src/assets/translation.service.mock.ts similarity index 100% rename from ng2-components/ng2-alfresco-login/test/assets/translation.service.mock.ts rename to ng2-components/ng2-alfresco-login/src/assets/translation.service.mock.ts diff --git a/ng2-components/ng2-alfresco-login/test/components/alfresco-login.spec.ts b/ng2-components/ng2-alfresco-login/src/components/alfresco-login.spec.ts similarity index 100% rename from ng2-components/ng2-alfresco-login/test/components/alfresco-login.spec.ts rename to ng2-components/ng2-alfresco-login/src/components/alfresco-login.spec.ts diff --git a/ng2-components/ng2-alfresco-login/test/services/alfresco-authentication.spec.ts b/ng2-components/ng2-alfresco-login/src/services/alfresco-authentication.spec.ts similarity index 100% rename from ng2-components/ng2-alfresco-login/test/services/alfresco-authentication.spec.ts rename to ng2-components/ng2-alfresco-login/src/services/alfresco-authentication.spec.ts diff --git a/ng2-components/ng2-alfresco-login/test-main.js b/ng2-components/ng2-alfresco-login/test-main.js new file mode 100644 index 0000000000..9b86a87b52 --- /dev/null +++ b/ng2-components/ng2-alfresco-login/test-main.js @@ -0,0 +1,78 @@ +if (!Object.hasOwnProperty('name')) { + Object.defineProperty(Function.prototype, 'name', { + get: function() { + var matches = this.toString().match(/^\s*function\s*(\S*)\s*\(/); + var name = matches && matches.length > 1 ? matches[1] : ""; + Object.defineProperty(this, 'name', {value: name}); + return name; + } + }); +} + +// Turn on full stack traces in errors to help debugging +Error.stackTraceLimit = Infinity; + +jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; + +// Cancel Karma's synchronous start, +// we will call `__karma__.start()` later, once all the specs are loaded. +__karma__.loaded = function() {}; + +System.config({ + baseURL: '/base/', + defaultJSExtensions: true, + paths: { + 'angular2/*': 'node_modules/angular2/*.js', + 'rxjs/*': 'node_modules/rxjs/*.js', + 'ng2-translate/*':'node_modules/ng2-translate/*.js' + } +}); + +Promise.all([ + System.import('angular2/src/platform/browser/browser_adapter'), + System.import('angular2/platform/testing/browser'), + System.import('angular2/testing') +]).then(function (modules) { + var browser_adapter = modules[0]; + var providers = modules[1]; + var testing = modules[2]; + testing.setBaseTestProviders(providers.TEST_BROWSER_PLATFORM_PROVIDERS, + providers.TEST_BROWSER_APPLICATION_PROVIDERS); + + browser_adapter.BrowserDomAdapter.makeCurrent(); +}).then(function() { + return Promise.all( + Object.keys(window.__karma__.files) // All files served by Karma. + .filter(onlySpecFiles) + .map(file2moduleName) + .map(function(path) { + return System.import(path).then(function(module) { + if (module.hasOwnProperty('main')) { + module.main(); + } else { + throw new Error('Module ' + path + ' does not implement main() method.'); + } + }); + })); + }) + .then(function() { + __karma__.start(); + }, function(error) { + // console.error(error.stack || error); + __karma__.start(); + }); + +function onlySpecFiles(path) { + // check for individual files, if not given, always matches to all + var patternMatched = __karma__.config.files ? + path.match(new RegExp(__karma__.config.files)) : true; + + return patternMatched && /[\.|_]spec\.js$/.test(path); +} + +// Normalize paths to module names. +function file2moduleName(filePath) { + return filePath.replace(/\\/g, '/') + .replace(/^\/base\//, '') + .replace(/\.js$/, ''); +} diff --git a/ng2-components/ng2-alfresco-login/test/.gitignore b/ng2-components/ng2-alfresco-login/test/.gitignore deleted file mode 100644 index 75871e6dc2..0000000000 --- a/ng2-components/ng2-alfresco-login/test/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.js -*.js.map -*.d.ts diff --git a/ng2-components/ng2-alfresco-login/test/index.html b/ng2-components/ng2-alfresco-login/test/index.html deleted file mode 100644 index 6960ea6e65..0000000000 --- a/ng2-components/ng2-alfresco-login/test/index.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - Alfresco Login component Tests - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/ng2-components/ng2-alfresco-login/test/tsconfig.json b/ng2-components/ng2-alfresco-login/test/tsconfig.json deleted file mode 100644 index d24362499a..0000000000 --- a/ng2-components/ng2-alfresco-login/test/tsconfig.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compilerOptions": { - "target": "es5", - "module": "system", - "moduleResolution": "node", - "sourceMap": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "removeComments": false, - "noImplicitAny": false, - "declaration": false, - "outDir": "../dist" - }, - "exclude": [ - ] -} diff --git a/ng2-components/ng2-alfresco-login/tsconfig.json b/ng2-components/ng2-alfresco-login/tsconfig.json index f2ce3750e5..05edabdb2d 100644 --- a/ng2-components/ng2-alfresco-login/tsconfig.json +++ b/ng2-components/ng2-alfresco-login/tsconfig.json @@ -1,28 +1,27 @@ { - "compilerOptions": { - "target": "es5", - "module": "commonjs", - "declaration": false, - "removeComments": true, - "noLib": false, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "sourceMap": true, - "allowUnreachableCode": false, - "allowUnusedLabels": false, - "noImplicitAny": false, - "noImplicitReturns": false, - "noImplicitUseStrict": false, - "noFallthroughCasesInSwitch": true, - "outDir": "dist" - }, - "exclude": [ - "node_modules", - "dist", - "typings/browser.d.ts", - "typings/browser", - "demo", - "src" - ], - "compileOnSave": false + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "declaration": false, + "removeComments": true, + "noLib": false, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "sourceMap": true, + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "noImplicitAny": false, + "noImplicitReturns": false, + "noImplicitUseStrict": false, + "noFallthroughCasesInSwitch": true, + "outDir": "dist" + }, + "exclude": [ + "node_modules", + "dist", + "typings/browser.d.ts", + "typings/browser", + "demo" + ], + "compileOnSave": false }