在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):SBoudrias/gulp-istanbul开源软件地址(OpenSource Url):https://github.com/SBoudrias/gulp-istanbul开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):gulp-istanbulIstanbul unit test coverage plugin for gulp. Works on top of any Node.js unit test framework. Installationnpm install --save-dev gulp-istanbul ExampleIn your Node.js testingvar istanbul = require('gulp-istanbul');
// We'll use mocha in this example, but any test framework will work
var mocha = require('gulp-mocha');
gulp.task('pre-test', function () {
return gulp.src(['lib/**/*.js'])
// Covering files
.pipe(istanbul())
// Force `require` to return covered files
.pipe(istanbul.hookRequire());
});
gulp.task('test', ['pre-test'], function () {
return gulp.src(['test/*.js'])
.pipe(mocha())
// Creating the reports after tests ran
.pipe(istanbul.writeReports())
// Enforce a coverage of at least 90%
.pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } }));
}); Note: Version 4.x.x of Browser testingFor browser testing, you'll need to write the files covered by istanbul in a directory from where you'll serve these files to the browser running the test. You'll also need a way to extract the value of the coverage variable after the test have runned in the browser. Browser testing is hard. If you're not sure what to do, then I suggest you take a look at Karma test runner - it has built-in coverage using Istanbul. var istanbul = require('gulp-istanbul');
gulp.task('pre-test', function () {
return gulp.src(['lib/**/*.js'])
// Covering files
.pipe(istanbul())
// Write the covered files to a temporary directory
.pipe(gulp.dest('test-tmp/'));
});
gulp.task('test', ['pre-test'], function () {
// Make sure your tests files are requiring files from the
// test-tmp/ directory
return gulp.src(['test/*.js'])
.pipe(testFramework())
// Creating the reports after tests ran
.pipe(istanbul.writeReports());
}); Source Mapsgulp-istanbul supports gulp-sourcemaps when instrumenting: gulp.task('pre-test', function () {
return gulp.src(['lib/**/*.js'])
// optionally load existing source maps
.pipe(sourcemaps.init())
// Covering files
.pipe(istanbul())
.pipe(sourcemaps.write('.'))
// Write the covered files to a temporary directory
.pipe(gulp.dest('test-tmp/'));
}); APIistanbul(opt)Instrument files passed in the stream. optType: {
coverageVariable: 'someVariable',
...other Instrumeter options...
} coverageVariableType: The global variable istanbul uses to store coverage See also: includeUntestedType: Flag to include test coverage of files that aren't See also: instrumenterType: Custom Instrumenter to be used instead of the default istanbul one. var isparta = require('isparta');
var istanbul = require('gulp-istanbul');
gulp.src('lib/**.js')
.pipe(istanbul({
// supports es6
instrumenter: isparta.Instrumenter
})); See also: Other Istanbul Instrumenter optionsSee: istanbul.hookRequire()Overwrite Always use this option if you're running tests in Node.js istanbul.summarizeCoverage(opt)get coverage summary details optType: {
coverageVariable: 'someVariable'
} coverageVariableType: The global variable istanbul uses to store coverage See also: returnsType: {
lines: { total: 4, covered: 2, skipped: 0, pct: 50 },
statements: { total: 4, covered: 2, skipped: 0, pct: 50 },
functions: { total: 2, covered: 0, skipped: 0, pct: 0 },
branches: { total: 0, covered: 0, skipped: 0, pct: 100 }
} See also: istanbul.writeReports(opt)Create the reports on stream end. optType: {
dir: './coverage',
reporters: [ 'lcov', 'json', 'text', 'text-summary', CustomReport ],
reportOpts: { dir: './coverage' },
coverageVariable: 'someVariable'
} You can pass individual configuration to a reporter. {
dir: './coverage',
reporters: [ 'lcovonly', 'json', 'text', 'text-summary', CustomReport ],
reportOpts: {
lcov: {dir: 'lcovonly', file: 'lcov.info'},
json: {dir: 'json', file: 'converage.json'}
},
coverageVariable: 'someVariable'
} dirType: The folder in which the reports are to be outputted. reportersType: The list of available reporters:
You can also specify one or more custom reporter objects as items in the array. These will be automatically registered with istanbul. See also reportOptsType: {
dir: './coverage'
} You can also configure separate directory for each report. {
html: {
dir: './coverage/html',
watermarks: {
statements: [ 50, 80 ],
lines: [ 50, 80 ],
functions: [ 50, 80],
branches: [ 50, 80 ]
}
},
lcov: {dir: './coverage/lcov'},
lcovonly: {dir: './coverage/lcovonly'},
json: {dir: './coverage/json'},
}
coverageVariableType: The global variable istanbul uses to store coverage See also: istanbul.enforceThresholds(opt)Checks coverage against minimum acceptable thresholds. Fails the build if any of the thresholds are not met. optType: {
coverageVariable: 'someVariable',
thresholds: {
global: 60,
each: -10
}
} coverageVariableType: The global variable istanbul uses to store coverage thresholdsType: Minimum acceptable coverage thresholds. Any coverage values lower than the specified threshold will fail the build. Each threshold value can be:
Thresholds can be specified across all files (
You can also specify a value for each metric:
emitsA plugin error in the stream if the coverage fails LicenseMIT License (c) Simon Boudrias - 2013 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论