How to use the istanbul-lib-instrument.createInstrumenter function in istanbul-lib-instrument

To help you get started, we’ve selected a few istanbul-lib-instrument examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / istanbul-lib-instrument / istanbul-lib-instrument-tests.ts View on Github external
readInitialCoverage,
	programVisitor
} from 'istanbul-lib-instrument';

import * as babelTypes from 'babel-types';
import { RawSourceMap } from 'source-map';

const code = 'foo';
const filename = 'bar.txt';

createInstrumenter();
createInstrumenter({});
createInstrumenter({
	coverageVariable: 'coverage'
});
const instrumenter = createInstrumenter({
	preserveComments: true,
	compact: false,
	esModules: true,
	autoWrap: false,
	produceSourceMap: true,
	sourceMapUrlCallback: (filename: string, url: string) => {},
	debug: false
});

const sourceMap: RawSourceMap = {
	version: 1 as any as string, // Fixed by https://github.com/mozilla/source-map/pull/293 but the fix is not yet published
	sources: ['foo', 'bar'],
	names: ['foo', 'bar'],
	mappings: 'foo',
	file: 'foo'
};
github istanbuljs / istanbuljs / packages / istanbul-api / lib / run-instrument.js View on Github external
let instrumenter;
    const origCallback = callback;
    const needBaseline = iOpts.saveBaseline();
    const baselineFile = path.resolve(iOpts.baselineFile());

    if (iOpts.completeCopy()) {
        includes = ['**/*'];
    } else {
        includes = iOpts.extensions().map(ext => '**/*' + ext);
    }

    if (!input) {
        return callback(new Error('No input specified'));
    }

    instrumenter = libInstrument.createInstrumenter(
        iOpts.getInstrumenterOpts()
    );

    if (needBaseline) {
        mkdirp.sync(path.dirname(baselineFile));
        instrumenter = new BaselineCollector(instrumenter);
        callback = function(err) {
            /* istanbul ignore else */
            if (!err) {
                console.error('Saving baseline coverage at ' + baselineFile);
                fs.writeFileSync(
                    baselineFile,
                    JSON.stringify(instrumenter.getCoverage()),
                    'utf8'
                );
            }
github splunk / splunk-sdk-javascript / node_modules / nyc / lib / instrumenters / istanbul.js View on Github external
function InstrumenterIstanbul (options) {
  const { plugins } = options
  const configPlugins = plugins ? { plugins } : {}

  const instrumenter = createInstrumenter(Object.assign({
    autoWrap: true,
    coverageVariable: '__coverage__',
    embedSource: true,
    compact: options.compact,
    preserveComments: options.preserveComments,
    produceSourceMap: options.produceSourceMap,
    ignoreClassMethods: options.ignoreClassMethods,
    esModules: options.esModules
  }, configPlugins))

  return {
    instrumentSync (code, filename, sourceMap) {
      var instrumented = instrumenter.instrumentSync(code, filename)
      // the instrumenter can optionally produce source maps,
      // this is useful for features like remapping stack-traces.
      // TODO: test source-map merging logic.
github yahoo / jsx-test / lib / helper.js View on Github external
function instrumentCode(code, filename) {
    var createInstrumenter = require('istanbul-lib-instrument').createInstrumenter;
    return createInstrumenter().instrumentSync(code, filename);
}
/**
github computestdev / Openrunner / building / CoverageInstrumentationStream.js View on Github external
'use strict';
const {Transform} = require('stream');
const {createInstrumenter} = require('istanbul-lib-instrument');

const instrumenter = createInstrumenter({
    coverageVariable: '__runner_coverage__',
    preserveComments: true,
    compact: false,
    esModules: false,
    autoWrap: false,
    produceSourceMap: false,
    sourceMapUrlCallback: null,
    debug: false,
});

const scriptContent = Symbol('scriptContent');
const scriptFileName = Symbol('scriptFileName');

class CoverageInstrumentationStream extends Transform {
    constructor(options, fileName) {
        super(options);
github imodeljs / imodeljs / test-apps / testbed / coverage.js View on Github external
}

function report() {
  //Add map files to transform .js into .ts in coverage report
  for (let file in cov)
    if (fs.existsSync(file + '.map'))
      cov[file].inputSourceMap = JSON.parse(fs.readFileSync(file + '.map', 'utf-8'));

  fs.writeFileSync(join(tmpd, `${process.type}.json`), JSON.stringify(cov), 'utf-8')
}

const customOpts = {
  includeAllSources: true
};

const instrumenter = createInstrumenter(customOpts)
const transformer = instrumenter.instrumentSync.bind(instrumenter)
const cov = global.__coverage__ = {}

const tmpd = resolve(__dirname, '.nyc_output')
if (!fs.existsSync(tmpd)) {
  fs.mkdirSync(tmpd)
}

const matched = match()

hookRequire(matched, transformer, {})

if (process.type === 'browser') {
  process.on('exit', report)
} else {
  window.addEventListener('unload', report)
github stryker-mutator / stryker / packages / core / src / transpiler / CoverageInstrumenterTranspiler.ts View on Github external
constructor(private readonly settings: StrykerOptions, private readonly filesToInstrument: readonly string[]) {
    this.instrumenter = createInstrumenter({ coverageVariable: this.coverageVariable, preserveComments: true });
  }
github stryker-mutator / stryker / packages / stryker / src / transpiler / CoverageInstrumenterTranspiler.ts View on Github external
constructor(private readonly settings: StrykerOptions, private readonly filesToInstrument: ReadonlyArray) {
    this.instrumenter = createInstrumenter({ coverageVariable: this.coverageVariable, preserveComments: true });
  }
github laggingreflex / mochista / istanbul / instrument / transformer.js View on Github external
module.exports = function createTransformerFn ({
  root,
  coverageVariable,
  transformerCacheVariable,
  sourceMapCacheVariable,
  cacheDir,
  disableCache = false,
  ext = '.js',
  verbose = false
}) {
  const transformerCache = global[transformerCacheVariable] = global[transformerCacheVariable] || {};
  const sourceMapCache = global[sourceMapCacheVariable] = global[sourceMapCacheVariable] || createSourceMapStore();

  const instrumenter = createInstrumenter({
    autoWrap: true,
    coverageVariable,
    embedSource: true,
    noCompact: true,
    preserveComments: true
  });
  const instrument = instrumenter.instrumentSync.bind(instrumenter);

  return function transform (code, file) {
    let instrumentedCode, hasChanged, cacheFile, codeHash;

    cacheFile = resolve(cacheDir, relative(root, file)) + '.json';
    codeHash = md5Hex(code);

    const sourceMap = convertSourceMap.fromSource(code);
    if (sourceMap) {
github istanbuljs / nyc / build-self-coverage.js View on Github external
'use strict'

const path = require('path')
const fs = require('fs')
const istanbul = require('istanbul-lib-instrument')
const makeDir = require('make-dir')
const glob = require('glob')

const instrumenter = istanbul.createInstrumenter({
  coverageVariable: '___NYC_SELF_COVERAGE___',
  esModules: true
})

function instrumentFile (name) {
  const indexPath = path.join(__dirname, name)
  const outputPath = path.join(__dirname, 'self-coverage', name)

  const source = fs.readFileSync(indexPath, 'utf8')
  const instrumentedSource = name === 'package.json' ? source : instrumenter.instrumentSync(source, indexPath)

  makeDir.sync(path.dirname(outputPath))
  fs.writeFileSync(outputPath, instrumentedSource)
}

function instrumentGlob (pattern) {

istanbul-lib-instrument

Core istanbul API for JS code coverage

BSD-3-Clause
Latest version published 2 months ago

Package Health Score

87 / 100
Full package analysis