2017-08-01 91 views
1

請安置,我已經將我的代碼從es6轉換爲es5,並使用gulp作爲任務執行者。我已經完成了伊斯坦布爾的報道報道。它沒有顯示設置後的測試覆蓋率。下面是我的代碼如何使用Gulp顯示使用gulp-mocha的測試覆蓋範圍

import gulp from 'gulp'; 
import loadPlugins from 'gulp-load-plugins'; 
import path from 'path'; 
import mocha from 'gulp-mocha'; 
import exit from 'gulp-exit'; 
import coveralls from 'gulp-coveralls'; 
import cover from 'gulp-coverage'; 

裝入大口插件到plugins變量

const plugins = loadPlugins(); 

gulp.task('tests',() => { 
    gulp.src('./server/tests/*.js') 
    .pipe(plugins.babel()) 
    .pipe(mocha()) 
    .pipe(exit()); 
}); 

編譯所有通天的Javascript到ES5,地點在蒸餾水文件夾

const paths = { 
    js: ['./**/*.js', '!dist/**', '!node_modules/**'] 
}; 

編譯所有通天的Javascript到ES5並將其放入分機目錄

gulp.task('babel',() => 
    gulp.src(paths.js, { base: '.' }) 
    .pipe(plugins.babel()) 
    .pipe(gulp.dest('dist')) 
); 

gulp.task('coverage',() => { 
    gulp.src('server/test/**/*.js', { read: false }) 
    .pipe(cover.instrument({ 
    pattern: ['server/controllers/**/*.js'], 
     debugDirectory: 'debug' 
    })) 
    .pipe(mocha()) 
    .pipe(cover.gather()) 
    .pipe(cover.format()) 
    .pipe(gulp.dest('reports')); 
}); 

gulp.task('coveralls',() => gulp.src('./coverage/lcov') 
    .pipe(coveralls())); 

重新啓動服務器上進行提交

gulp.task('nodemon', ['babel'],() => 
    plugins.nodemon({ 
    script: path.join('dist', 'index.js'), 
    ignore: ['README.md', 'node_modules/**/*.js', 'dist/**/*.js'], 
    ext: 'js', 
    tasks: ['babel'] 
    }) 
); 

gulp.task('test', ['tests']); 
gulp.task('default', ['nodemon']); 
gulp.task('production', ['babel']); 
+0

什麼沒有顯示設置後覆蓋報告它呢?它是什麼'? – user2347763

回答

1

下面的代碼片段走過我是如何解決這個問題有一點點修改每一個變化。

把下面的代碼在你gulpfile

import gulp from 'gulp'; 
import loadPlugins from 'gulp-load-plugins'; 
import path from 'path'; 
import shell from 'gulp-shell'; 

// Load the gulp plugins into the `plugins` variable 
const plugins = loadPlugins(); 

// Compile all Babel Javascript into ES5 and place in dist folder 
const paths = { 
    js: ['./**/*.js', '!dist/**', '!node_modules/**', 
    '!./server/tests/**'] 
}; 

// Compile all Babel Javascript into ES5 and put it into the dist dir 
gulp.task('babel',() => 
    gulp.src(paths.js, { base: '.' }) 
    .pipe(plugins.babel()) 
    .pipe(gulp.dest('dist')) 
); 

gulp.task('migrate', shell.task([ 
    'cross-env NODE_ENV=test sequelize db:migrate', 
])); 

gulp.task('coverage', shell.task([ 
    'cross-env NODE_ENV=test nyc mocha ./server/test/**/*.js', 
])); 

// Restart server with on every changes made to file 
gulp.task('nodemon', ['babel'],() => 
    plugins.nodemon({ 
    script: path.join('dist', 'index.js'), 
    ignore: ['README.md', 'node_modules/**/*.js', 'dist/**/*.js'], 
    ext: 'js', 
    tasks: ['babel'] 
    }) 
); 

gulp.task('test', ['migrate', 'coverage']); 
gulp.task('default', ['nodemon']); 
gulp.task('production', ['babel']); 

然後去到你的package.json然後添加以下

"nyc": { 
    "require": [ 
     "babel-register" 
    ], 
    "reporter": [ 
     "lcov", 
     "text", 
     "html" 
    ], 
    "sourceMap": false, 
    "instrument": false, 
    "exclude": [ 
     "the test file you want to exclude from coverage" 
    ] 
    } 

絕對做

相關問題