How to use istanbul-lib-instrument - 10 common examples

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
// instrument without a filename
instrumenter.instrument(code, (error, code) => {
	if (error) {
		error.stack;
	} else {
		code.trim();
	}
});

const cov = instrumenter.lastFileCoverage();
Object.create(cov);

const map = instrumenter.lastSourceMap();
Object.create(map);

const initialCov = readInitialCoverage(code);
initialCov.gcv;

programVisitor(babelTypes);
programVisitor(babelTypes, filename);
programVisitor(babelTypes, filename, { coverageVariable: 'coverage' });
const visitor = programVisitor(babelTypes, filename, { inputSourceMap: sourceMap });

visitor.enter(filename);
const data = visitor.exit(filename);
Object.create(data.fileCoverage);
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 DefinitelyTyped / DefinitelyTyped / types / istanbul-lib-instrument / istanbul-lib-instrument-tests.ts View on Github external
error.stack;
	} else {
		code.trim();
	}
});

const cov = instrumenter.lastFileCoverage();
Object.create(cov);

const map = instrumenter.lastSourceMap();
Object.create(map);

const initialCov = readInitialCoverage(code);
initialCov.gcv;

programVisitor(babelTypes);
programVisitor(babelTypes, filename);
programVisitor(babelTypes, filename, { coverageVariable: 'coverage' });
const visitor = programVisitor(babelTypes, filename, { inputSourceMap: sourceMap });

visitor.enter(filename);
const data = visitor.exit(filename);
Object.create(data.fileCoverage);
github DefinitelyTyped / DefinitelyTyped / types / istanbul-lib-instrument / istanbul-lib-instrument-tests.ts View on Github external
}
});

const cov = instrumenter.lastFileCoverage();
Object.create(cov);

const map = instrumenter.lastSourceMap();
Object.create(map);

const initialCov = readInitialCoverage(code);
initialCov.gcv;

programVisitor(babelTypes);
programVisitor(babelTypes, filename);
programVisitor(babelTypes, filename, { coverageVariable: 'coverage' });
const visitor = programVisitor(babelTypes, filename, { inputSourceMap: sourceMap });

visitor.enter(filename);
const data = visitor.exit(filename);
Object.create(data.fileCoverage);
github DefinitelyTyped / DefinitelyTyped / types / istanbul-lib-instrument / istanbul-lib-instrument-tests.ts View on Github external
code.trim();
	}
});

const cov = instrumenter.lastFileCoverage();
Object.create(cov);

const map = instrumenter.lastSourceMap();
Object.create(map);

const initialCov = readInitialCoverage(code);
initialCov.gcv;

programVisitor(babelTypes);
programVisitor(babelTypes, filename);
programVisitor(babelTypes, filename, { coverageVariable: 'coverage' });
const visitor = programVisitor(babelTypes, filename, { inputSourceMap: sourceMap });

visitor.enter(filename);
const data = visitor.exit(filename);
Object.create(data.fileCoverage);
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)

istanbul-lib-instrument

Core istanbul API for JS code coverage

BSD-3-Clause
Latest version published 3 months ago

Package Health Score

87 / 100
Full package analysis