From fbb94189e76c5315e0cb64b34ca0764e79b6d7b4 Mon Sep 17 00:00:00 2001 From: Mario Romano Date: Mon, 16 May 2016 19:04:06 +0100 Subject: [PATCH] #60 coverage and tets for upload component --- .../ng2-alfresco-documentlist/package.json | 4 +- .../ng2-alfresco-login/tsconfig.json | 7 + .../ng2-alfresco-upload/karma-test-shim.js | 74 ++++++++++ .../ng2-alfresco-upload/karma.conf.js | 134 ++++++------------ .../ng2-alfresco-upload/package.json | 35 ++--- .../ng2-alfresco-upload/test-main.js | 78 ---------- .../ng2-alfresco-upload/tsconfig.json | 49 ++++--- 7 files changed, 167 insertions(+), 214 deletions(-) create mode 100644 ng2-components/ng2-alfresco-upload/karma-test-shim.js delete mode 100644 ng2-components/ng2-alfresco-upload/test-main.js diff --git a/ng2-components/ng2-alfresco-documentlist/package.json b/ng2-components/ng2-alfresco-documentlist/package.json index 6a7f3fb1af..69244d5562 100644 --- a/ng2-components/ng2-alfresco-documentlist/package.json +++ b/ng2-components/ng2-alfresco-documentlist/package.json @@ -73,9 +73,7 @@ "traceur": "^0.0.91", "tslint": "^3.8.1", "typescript": "^1.8.10", - "typings": "^0.7.12", - "xo": "^0.14.0", - "yargs": "^4.7.0" + "typings": "^0.7.12" }, "license-check-config": { "src": [ diff --git a/ng2-components/ng2-alfresco-login/tsconfig.json b/ng2-components/ng2-alfresco-login/tsconfig.json index 45d37de047..ad8bb61c75 100644 --- a/ng2-components/ng2-alfresco-login/tsconfig.json +++ b/ng2-components/ng2-alfresco-login/tsconfig.json @@ -8,6 +8,13 @@ "sourceMap": true, "removeComments": true, "declaration": true, + "noLib": false, + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "noImplicitAny": false, + "noImplicitReturns": false, + "noImplicitUseStrict": false, + "noFallthroughCasesInSwitch": true, "outDir": "dist" }, "exclude": [ diff --git a/ng2-components/ng2-alfresco-upload/karma-test-shim.js b/ng2-components/ng2-alfresco-upload/karma-test-shim.js new file mode 100644 index 0000000000..a9ff64301c --- /dev/null +++ b/ng2-components/ng2-alfresco-upload/karma-test-shim.js @@ -0,0 +1,74 @@ +// Tun 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({ + packages: { + 'base/dist': { + defaultExtension: 'js', + format: 'register', + map: Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {}) + }, + 'ng2-translate': { + defaultExtension: 'js', + format: 'register' + } + }, + map: { + 'ng2-translate': '/base/node_modules/ng2-translate/bundles' + } +}); + +System.import('angular2/src/platform/browser/browser_adapter') + .then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); }) + .then(function() { return Promise.all(resolveTestFiles()); }) + .then( + function() { + __karma__.start(); + }, + function(error) { + __karma__.error(error.stack || error); + } + ); + +function createPathRecords(pathsMapping, appPath) { + // creates local module name mapping to global path with karma's fingerprint in path, e.g.: + // './vg-player/vg-player': + // '/base/dist/vg-player/vg-player.js?f4523daf879cfb7310ef6242682ccf10b2041b3e' + var moduleName = './' + resolveKeyPathForMapping('base/dist/', appPath); + moduleName = moduleName.replace(/\.js$/, ''); + pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]; + return pathsMapping; +} + +function onlyAppFiles(filePath) { + return /\/base\/dist\/(?!.*\.spec\.js$).*\.js$/.test(filePath); +} + +function onlySpecFiles(path) { + return /\.spec\.js$/.test(path); +} + +function resolveTestFiles() { + return Object.keys(window.__karma__.files) // All files served by Karma. + .filter(onlySpecFiles) + .map(function(moduleName) { + // loads all spec files via their global module names (e.g. + // 'base/dist/vg-player/vg-player.spec') + return System.import(moduleName); + }); +} + +function resolveKeyPathForMapping(basePathWhereToStart, appPath) { + var location = appPath.indexOf(basePathWhereToStart); + if (location > -1) { + return appPath.substring(basePathWhereToStart.length + 1); + } else { + return appPath; + } +} diff --git a/ng2-components/ng2-alfresco-upload/karma.conf.js b/ng2-components/ng2-alfresco-upload/karma.conf.js index bb8970f2fb..7b9f3b6e46 100644 --- a/ng2-components/ng2-alfresco-upload/karma.conf.js +++ b/ng2-components/ng2-alfresco-upload/karma.conf.js @@ -1,128 +1,78 @@ -// 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) { +module.exports = function (config) { config.set({ - // base path that will be used to resolve all patterns (eg. files, exclude) - basePath: './', + basePath: '.', + frameworks: ['jasmine'], - // frameworks to use - // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['jasmine-ajax', '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 + // paths loaded by Karma + {pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', included: true, watched: false}, + {pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: false}, + {pattern: 'node_modules/rxjs/bundles/Rx.js', included: true, watched: false}, + {pattern: 'node_modules/angular2/bundles/angular2.dev.js', included: true, watched: false}, + {pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: false}, + {pattern: 'node_modules/angular2/bundles/http.dev.js', included: true, watched: false}, + {pattern: 'node_modules/angular2/bundles/router.dev.js', included: true, watched: false}, + {pattern: 'node_modules/alfresco-core-rest-api/bundle.js', included: true, watched: false}, + {pattern: 'node_modules/ng2-translate/bundles/ng2-translate.js', included: true, watched: false}, - // suppress annoying 404 warnings for resources, images, etc. - //{ pattern: 'dist/src/assets/**/*', watched: false, included: false, served: true }, + {pattern: 'karma-test-shim.js', included: true, watched: true}, - 'test-main.js' + // paths loaded via module imports + {pattern: 'dist/**/*.js', included: false, watched: true}, + + // paths to support debugging with source maps in dev tools + {pattern: 'src/**/*.ts', included: false, watched: false}, + {pattern: 'dist/**/*.js.map', included: false, watched: false} ], - // must go along with above, suppress annoying 404 warnings. + // proxied base paths proxies: { - '/assets/': '/base/dist/dev/assets/' + // required for component assets fetched by Angular's compiler + '/src/': '/base/src/' }, - // 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, + colors: true, - // enable / disable watching file and executing tests whenever any file changes autoWatch: true, + browsers: ['Chrome'], - // start these browsers - // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher - browsers: [ - 'PhantomJS', - 'Chrome' + // Karma plugins loaded + plugins: [ + 'karma-jasmine', + 'karma-coverage', + 'karma-chrome-launcher', + 'karma-mocha-reporter' ], + // Coverage reporter generates the coverage + reporters: ['mocha', 'coverage'], - customLaunchers: { - Chrome_travis_ci: { - base: 'Chrome', - flags: ['--no-sandbox'] - } + // Source files that you wanna generate coverage for. + // Do not include tests or libraries (these files will be instrumented by Istanbul) + preprocessors: { + 'dist/**/!(*spec).js': ['coverage'] }, coverageReporter: { dir: 'coverage/', reporters: [ - { type: 'text-summary' }, - { type: 'json', subdir: '.', file: 'coverage-final.json' }, - { type: 'html' } + {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; - } + singleRun: true + }) }; diff --git a/ng2-components/ng2-alfresco-upload/package.json b/ng2-components/ng2-alfresco-upload/package.json index 8f71e45148..b9e746aae8 100644 --- a/ng2-components/ng2-alfresco-upload/package.json +++ b/ng2-components/ng2-alfresco-upload/package.json @@ -4,15 +4,23 @@ "version": "0.1.0", "author": "Alfresco Software, Ltd.", "scripts": { - "build": "npm run tslint-test && npm run tslint-src && npm run tslint-root && typings install && rm -rf dist && tsc && license-check", + "postinstall": "npm run typings", + "typings": "typings install", + "start": "npm run test && http-server -c-1 -o -p 8875 .", + "build": "npm run tslint && typings install && rm -rf dist && tsc && npm run copytemplates && license-check", + "tslint": "npm run tslint-test && npm run tslint-src && npm run tslint-root", "tslint-test": "tslint -c tslint.json test/**/*.ts", "tslint-src": "tslint -c tslint.json src/**/*.ts", "tslint-root": "tslint -c tslint.json *.ts", + "copytemplates": "npm run copy-html && npm run copy-css", + "copy-html": "copyfiles './src/**/*.html' dist", + "copy-css": "copyfiles './src/**/*.css' dist", "licensecheck": "license-check", "tsc": "tsc", - "tsc:w": "tsc -w", - "typings": "typings", - "test": "karma start karma.conf.js" + "pretest": "npm run build", + "test": "karma start karma.conf.js", + "posttest": "node_modules/.bin/remap-istanbul -i coverage/coverage-final.json -o coverage -t html", + "coverage": "http-server -c-1 -o -p 9875 ./coverage" }, "repository": { "type": "git", @@ -48,27 +56,22 @@ "angular2": "2.0.0-beta.15" }, "devDependencies": { + "copyfiles": "^0.2.1", "coveralls": "^2.11.9", + "http-server": "0.8.5", "jasmine-ajax": "^3.2.0", "jasmine-core": "2.4.1", - "license-check": "^1.0.4", - "live-server": "^0.9.2", - "tslint": "^3.8.1", - "typescript": "^1.8.10", - "typings": "^0.7.12", - "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-jasmine-ajax": "^0.1.13", "karma-mocha-reporter": "^2.0.3", - "karma-phantomjs-launcher": "^1.0.0", - "traceur": "^0.0.91", + "license-check": "^1.0.4", "remap-istanbul": "^0.6.3", - "phantomjs-prebuilt": "^2.1.7" + "traceur": "^0.0.91", + "tslint": "^3.8.1", + "typescript": "^1.8.10", + "typings": "^0.7.12" }, "license-check-config": { "src": [ diff --git a/ng2-components/ng2-alfresco-upload/test-main.js b/ng2-components/ng2-alfresco-upload/test-main.js deleted file mode 100644 index 65ecaefbc0..0000000000 --- a/ng2-components/ng2-alfresco-upload/test-main.js +++ /dev/null @@ -1,78 +0,0 @@ -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-upload/tsconfig.json b/ng2-components/ng2-alfresco-upload/tsconfig.json index 98b6cf1ce8..ad8bb61c75 100644 --- a/ng2-components/ng2-alfresco-upload/tsconfig.json +++ b/ng2-components/ng2-alfresco-upload/tsconfig.json @@ -1,27 +1,26 @@ { - "compilerOptions": { - "target": "es5", - "module": "commonjs", - "declaration": true, - "removeComments": true, - "noLib": false, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "sourceMap": true, - "allowUnreachableCode": false, - "allowUnusedLabels": false, - "noImplicitAny": false, - "noImplicitReturns": true, - "noImplicitUseStrict": false, - "noFallthroughCasesInSwitch": true, - "outDir": "dist" - }, - "exclude": [ - "node_modules", - "dist", - "typings/browser.d.ts", - "typings/browser", - "demo" - ], - "compileOnSave": false + "compilerOptions": { + "target": "es5", + "module": "system", + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "sourceMap": true, + "removeComments": true, + "declaration": true, + "noLib": false, + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "noImplicitAny": false, + "noImplicitReturns": false, + "noImplicitUseStrict": false, + "noFallthroughCasesInSwitch": true, + "outDir": "dist" + }, + "exclude": [ + "demo", + "node_modules", + "typings/main", + "typings/main.d.ts" + ] }