Commit 6f744e765280af1c0ad3ed2b6e102bf32d9c57b3
1 parent
d316e303
Exists in
master
and in
35 other branches
added coverage
Showing
6 changed files
with
182 additions
and
61 deletions
Show diff stats
.vscode/tasks.json
dev-scripts/node_modules
| ... | ... | @@ -0,0 +1,66 @@ |
| 1 | +/** | |
| 2 | + * @script remap-coverage.ts | |
| 3 | + * | |
| 4 | + * Esse script serve para transformar as informações de cobertura geradas pelo karma-coverage | |
| 5 | + * e que originalmente é construída apontando para os arquivos javascript (já que os testes são executados em javascript) | |
| 6 | + * para a informação de cobertura apontando para os arquivos Typescript, utilizando os source maps gerados pelo compilador | |
| 7 | + * typescript | |
| 8 | + * @author: Abner Oliveira | |
| 9 | + * | |
| 10 | + * Examplo de uso: | |
| 11 | + * | |
| 12 | + * Na linha de comando, na pasta raiz do projeto, digite: | |
| 13 | + * | |
| 14 | + * ts-node dev-scripts/remap-coverage.ts | |
| 15 | + * | |
| 16 | + * Observação: O karma já deve ter sido executado antes, e a pasta de coverage deve ser "./coverage" | |
| 17 | + */ | |
| 18 | + | |
| 19 | +import * as path from "path"; | |
| 20 | +import * as fs from "fs"; | |
| 21 | + | |
| 22 | +let remapIstanbul = require("remap-istanbul"); | |
| 23 | + | |
| 24 | +// pasta onde os arquivos do coverage são gerados | |
| 25 | +let coveragePath = path.join(__dirname, "..", "coverage"); | |
| 26 | + | |
| 27 | +// o pré-processador "coverage" do runner de tests "karma" gera uma pasta | |
| 28 | +// de coverage para cada browser em que os testes foram executados | |
| 29 | +// iteraremos arqui então entre essas pastas para realizar o remap de cada uma | |
| 30 | + | |
| 31 | +console.log("COVERAGE PATH:", coveragePath); | |
| 32 | +// lendo o diretório coveragePath | |
| 33 | +fs.readdir(coveragePath, (err, directories) => { | |
| 34 | + if (err) { | |
| 35 | + console.error(err.message); | |
| 36 | + throw err; | |
| 37 | + } | |
| 38 | + // para cada diretório na pasta coveragePath faz map transformando o path para o caminho absoluto | |
| 39 | + directories.map((file) => { | |
| 40 | + return path.join(coveragePath, file); | |
| 41 | + }).forEach((coverageFolder) => { | |
| 42 | + | |
| 43 | + let coverageFile = path.join(coverageFolder, "coverage-final.json"); | |
| 44 | + | |
| 45 | + let replace = require("replace"); | |
| 46 | + | |
| 47 | + let absoluteProjectPath = path.join(__dirname, "../"); | |
| 48 | + | |
| 49 | + replace({ | |
| 50 | + regex: absoluteProjectPath, | |
| 51 | + replacement: "", | |
| 52 | + paths: [coverageFile], | |
| 53 | + sillent: true | |
| 54 | + }); | |
| 55 | + // para cada pasta executa o remap do coverage que está apontando para os arquivos js | |
| 56 | + // para apontar para os arquivos Typecript | |
| 57 | + // gerando dois reports: JSON e HTML | |
| 58 | + remapIstanbul(coverageFile, | |
| 59 | + { | |
| 60 | + "basePath": "./src/", | |
| 61 | + "json": path.join(coverageFolder, "coverage-final-remaped.json"), | |
| 62 | + "html": path.join(coverageFolder) | |
| 63 | + }); | |
| 64 | + }); | |
| 65 | + | |
| 66 | +}); | |
| 0 | 67 | \ No newline at end of file | ... | ... |
karma.conf.js
| ... | ... | @@ -4,6 +4,44 @@ |
| 4 | 4 | var path = require('path'); |
| 5 | 5 | var conf = require('./gulp/conf'); |
| 6 | 6 | |
| 7 | +var argv = require("yargs").argv; | |
| 8 | + | |
| 9 | +var singleRun = false; | |
| 10 | + | |
| 11 | +if (argv.singleRun) { | |
| 12 | + singleRun = true; | |
| 13 | +} | |
| 14 | + | |
| 15 | +var projectFiles = [ | |
| 16 | + './src/commons.js', | |
| 17 | + './src/noosfero.js', | |
| 18 | + './src/noosfero-specs.js' | |
| 19 | +]; | |
| 20 | + | |
| 21 | +var karmaPlugins = [ | |
| 22 | + 'karma-chrome-launcher', | |
| 23 | + 'karma-phantomjs-launcher', | |
| 24 | + 'karma-angular-filesort', | |
| 25 | + 'karma-phantomjs-shim', | |
| 26 | + 'karma-jasmine', | |
| 27 | + 'karma-spec-reporter', | |
| 28 | + 'karma-ng-html2js-preprocessor', | |
| 29 | + 'karma-sourcemap-loader' | |
| 30 | +]; | |
| 31 | + | |
| 32 | + | |
| 33 | +var karmaReporters = ['spec']; | |
| 34 | + | |
| 35 | +if (argv.coverage) { | |
| 36 | + //projectFiles = ['./src/shim.ts', './src/app/index.ts', './src/**/*.spec.ts']; | |
| 37 | + singleRun = true; | |
| 38 | + | |
| 39 | + karmaPlugins.push('karma-coverage'); | |
| 40 | + //karmaPlugins.push('karma-webpack'); | |
| 41 | + | |
| 42 | + karmaReporters.push('coverage'); | |
| 43 | +} | |
| 44 | + | |
| 7 | 45 | |
| 8 | 46 | var _ = require('lodash'); |
| 9 | 47 | var wiredep = require('wiredep'); |
| ... | ... | @@ -13,7 +51,7 @@ var pathSrcHtml = [ |
| 13 | 51 | ]; |
| 14 | 52 | |
| 15 | 53 | var glob = require("glob"); |
| 16 | -var testFiles = glob.sync("./src/**/*.[sS]pec.ts"); | |
| 54 | +//var testFiles = glob.sync("./src/**/*.[sS]pec.ts"); | |
| 17 | 55 | |
| 18 | 56 | function listFiles() { |
| 19 | 57 | var wiredepOptions = _.extend({}, conf.wiredep, { |
| ... | ... | @@ -22,22 +60,7 @@ function listFiles() { |
| 22 | 60 | }); |
| 23 | 61 | |
| 24 | 62 | var patterns = [].concat(wiredep(wiredepOptions).js) |
| 25 | - .concat([ | |
| 26 | - './src/commons.js', | |
| 27 | - './src/noosfero.js', | |
| 28 | - './src/noosfero-specs.js' | |
| 29 | - ] | |
| 30 | - //[ | |
| 31 | - //path.join(conf.paths.src, 'common.js'), | |
| 32 | - //, path.join(conf.paths.src, 'index.ts') | |
| 33 | - //path.join(conf.paths.src, 'test.js') | |
| 34 | - // path.join(conf.paths.src, '/app/**/*.module.js'), | |
| 35 | - // path.join(conf.paths.src, '/app/**/*.js'), | |
| 36 | - // path.join(conf.paths.src, '/**/*.spec.js'), | |
| 37 | - // path.join(conf.paths.src, '/**/*.mock.js') | |
| 38 | - | |
| 39 | - //] | |
| 40 | - ) | |
| 63 | + .concat(projectFiles) | |
| 41 | 64 | .concat(pathSrcHtml); |
| 42 | 65 | |
| 43 | 66 | var files = patterns.map(function (pattern) { |
| ... | ... | @@ -51,11 +74,11 @@ function listFiles() { |
| 51 | 74 | served: true, |
| 52 | 75 | watched: false |
| 53 | 76 | }); |
| 54 | - files.push({ | |
| 55 | - pattern: path.join(conf.paths.src, '/test.js.map'), | |
| 56 | - included: false, | |
| 57 | - served: true | |
| 58 | - }); | |
| 77 | + // files.push({ | |
| 78 | + // pattern: path.join(conf.paths.src, '/test.js.map'), | |
| 79 | + // included: false, | |
| 80 | + // served: true | |
| 81 | + // }); | |
| 59 | 82 | return files; |
| 60 | 83 | } |
| 61 | 84 | |
| ... | ... | @@ -64,9 +87,11 @@ var webpackConfig = require("./webpack.config.js"); |
| 64 | 87 | module.exports = function (config) { |
| 65 | 88 | |
| 66 | 89 | var configuration = { |
| 90 | + basePath: '../angular-theme', | |
| 91 | + | |
| 67 | 92 | files: listFiles(), |
| 68 | 93 | |
| 69 | - singleRun: false, | |
| 94 | + singleRun: singleRun, | |
| 70 | 95 | |
| 71 | 96 | autoWatch: true, |
| 72 | 97 | colors: true, |
| ... | ... | @@ -87,48 +112,71 @@ module.exports = function (config) { |
| 87 | 112 | |
| 88 | 113 | browsers: ['PhantomJS'], |
| 89 | 114 | |
| 90 | - /* webpack: _.merge({ | |
| 91 | - | |
| 92 | - }, webpackConfig, { | |
| 93 | - devtool: 'inline-source-map' | |
| 94 | - }), | |
| 95 | - webpackServer: { | |
| 96 | - quite: true | |
| 97 | - },*/ | |
| 98 | - plugins: [ | |
| 99 | -// require('karma-webpack'), | |
| 100 | - 'karma-chrome-launcher', | |
| 101 | - 'karma-phantomjs-launcher', | |
| 102 | - 'karma-angular-filesort', | |
| 103 | - 'karma-webpack', | |
| 104 | - 'karma-phantomjs-shim', | |
| 105 | - 'karma-coverage', | |
| 106 | - 'karma-jasmine', | |
| 107 | - 'karma-spec-reporter', | |
| 108 | - 'karma-ng-html2js-preprocessor', | |
| 109 | - 'karma-sourcemap-loader' | |
| 110 | - ], | |
| 111 | - | |
| 112 | - coverageReporter: { | |
| 113 | - type: 'html', | |
| 114 | - dir: 'coverage/' | |
| 115 | - }, | |
| 116 | 115 | |
| 117 | - reporters: ['spec', "coverage"], | |
| 116 | + plugins: karmaPlugins, | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + reporters: karmaReporters, | |
| 118 | 121 | |
| 119 | 122 | proxies: { |
| 120 | 123 | '/assets/': path.join('/base/', conf.paths.src, '/assets/') |
| 121 | 124 | } |
| 122 | 125 | }; |
| 123 | 126 | |
| 124 | - // This is the default preprocessors configuration for a usage with Karma cli | |
| 125 | - // The coverage preprocessor is added in gulp/unit-test.js only for single tests | |
| 126 | - // It was not possible to do it there because karma doesn't let us now if we are | |
| 127 | - // running a single test or not | |
| 128 | - configuration.preprocessors = { | |
| 129 | - 'src/**/*.js': ['sourcemap'], | |
| 130 | - 'src/**/*.ts': ['sourcemap'] | |
| 131 | - }; | |
| 127 | + if (argv.coverage) { | |
| 128 | + | |
| 129 | + /*configuration.webpack = { | |
| 130 | + module: { | |
| 131 | + loaders: [ | |
| 132 | + { | |
| 133 | + test: /\.tsx?$/, | |
| 134 | + loader: 'ts-loader' | |
| 135 | + } | |
| 136 | + ] | |
| 137 | + }, | |
| 138 | + resolve: { | |
| 139 | + extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js'], | |
| 140 | + modulesDirectories: ['node_modules'], | |
| 141 | + root: path.resolve(__dirname) | |
| 142 | + } | |
| 143 | + };*/ | |
| 144 | + /*configuration.webpack = _.merge({ | |
| 145 | + | |
| 146 | + }, webpackConfig, { | |
| 147 | + devtool: 'source-map' | |
| 148 | + }), | |
| 149 | + configuration.webpackServer = { | |
| 150 | + quite: true | |
| 151 | + };*/ | |
| 152 | + | |
| 153 | + // This is the default preprocessors configuration for a usage with Karma cli | |
| 154 | + // The coverage preprocessor is added in gulp/unit-test.js only for single tests | |
| 155 | + // It was not possible to do it there because karma doesn't let us now if we are | |
| 156 | + // running a single test or not | |
| 157 | + configuration.preprocessors = { | |
| 158 | + 'src/noosfero.js': ['sourcemap', 'coverage'] | |
| 159 | + }; | |
| 160 | + | |
| 161 | + configuration.coverageReporter = { | |
| 162 | + dir: 'coverage/', | |
| 163 | + reporters: [ | |
| 164 | + /*{ type: 'html' },*/ | |
| 165 | + { type: 'json', file: 'coverage-final.json' }, | |
| 166 | + { type: 'text-summary' } | |
| 167 | + ] | |
| 168 | + }; | |
| 169 | + } else { | |
| 170 | + // This is the default preprocessors configuration for a usage with Karma cli | |
| 171 | + // The coverage preprocessor is added in gulp/unit-test.js only for single tests | |
| 172 | + // It was not possible to do it there because karma doesn't let us now if we are | |
| 173 | + // running a single test or not | |
| 174 | + configuration.preprocessors = { | |
| 175 | + 'src/noosfero.js': ['sourcemap'], | |
| 176 | + 'src/**/*.ts': ['sourcemap'] | |
| 177 | + }; | |
| 178 | + } | |
| 179 | + | |
| 132 | 180 | // 'src/**/*.js': ['sourcemap'], |
| 133 | 181 | // 'src/**/*.[sS]pec.ts': ['sourcemap'] |
| 134 | 182 | // }; | ... | ... |
package.json
| ... | ... | @@ -7,6 +7,9 @@ |
| 7 | 7 | "moment": "^2.11.2" |
| 8 | 8 | }, |
| 9 | 9 | "scripts": { |
| 10 | + "coverage": "karma start --coverage & npm run remap-coverage", | |
| 11 | + "remap-coverage": "ts-node --project ./dev-scripts ./dev-scripts/remapCoverage.ts", | |
| 12 | + "test-single": "webpack && karma start --single-run", | |
| 10 | 13 | "test": "concurrently \"webpack -w\" \"karma start\"", |
| 11 | 14 | "postinstall": "npm install -g bower && bower install", |
| 12 | 15 | "start": "concurrently \"webpack -w\" \"gulp serve\"", |
| ... | ... | @@ -48,6 +51,7 @@ |
| 48 | 51 | "gulp-useref": "~1.3.0", |
| 49 | 52 | "gulp-util": "~3.0.6", |
| 50 | 53 | "http-proxy-middleware": "~0.9.0", |
| 54 | + "istanbul": "^1.0.0-alpha.2", | |
| 51 | 55 | "karma": "~0.13.10", |
| 52 | 56 | "karma-angular-filesort": "~1.0.0", |
| 53 | 57 | "karma-chrome-launcher": "^0.2.2", |
| ... | ... | @@ -65,6 +69,8 @@ |
| 65 | 69 | "phantomjs": "~1.9.18", |
| 66 | 70 | "phantomjs-polyfill": "0.0.2", |
| 67 | 71 | "reflect-metadata": "^0.1.3", |
| 72 | + "remap-istanbul": "^0.5.1", | |
| 73 | + "replace": "^0.3.0", | |
| 68 | 74 | "ts-loader": "^0.8.1", |
| 69 | 75 | "ts-node": "^0.5.5", |
| 70 | 76 | "typescript": "^1.8.2", | ... | ... |
webpack.config.js
| ... | ... | @@ -42,7 +42,7 @@ var webpackConfig = { |
| 42 | 42 | extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js'] |
| 43 | 43 | }, |
| 44 | 44 | // Source maps support (or 'inline-source-map' also works) |
| 45 | - devtool: 'inline-source-map', | |
| 45 | + devtool: 'source-map', | |
| 46 | 46 | |
| 47 | 47 | module: { |
| 48 | 48 | loaders: [{ | ... | ... |