Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Cannot read property 'start' of undefined trying to generate lcov or text-lcov report using nyc #2

Open
FrederikP opened this issue May 19, 2020 · 2 comments

Comments

@FrederikP
Copy link

FrederikP commented May 19, 2020

When using nyc report --reporter=lcov or nyc report --reporter=lcov I am getting Cannot read property 'start' of undefined errors.

It works using e.g. --reporter=text or --reporter=html.

Is this a bug or am I using it wrong? I don't generate the reports directly from code but want to use nyc to keep code more flexible.

My interaction with this tool looks like this:

var Ajv = require("ajv");
var ajvIstanbul = require("ajv-istanbul");

var ajv = new Ajv();
ajvIstanbul(ajv);

Edit: ah I just realized this might be using quite an old version of istanbul? I'll try to port it locally to see if that fixes it.

@FrederikP
Copy link
Author

I guess one of the issues with newer versions of istanbul in relation to this package is the removal of the embedSource option: https://github.com/istanbuljs/istanbuljs/blob/master/packages/istanbul-lib-instrument/v0-changes.md#api-changes

In this comment #1 (comment) there is a link to a project that keeps the code in memory for reporting (at least I think so). That won't work with the approach I want to be able to take (several nyc commands), because memory is lost inbetween calls.

@FrederikP
Copy link
Author

For now I ended up doing something like this:

// modified to use up to date istanbul intrumenter by Frederik Petersen, fp@abusix.com, 2020

"use strict";

const beautify = require("js-beautify").js_beautify;
const { createInstrumenter } = require("istanbul-lib-instrument");
const instrumenter = createInstrumenter();
const fs = require("fs");
const path = require("path");
const { v4: uuidv4 } = require("uuid");

const codeDir = "./.ajv_istanbul";

module.exports = function (ajv) {
  fs.mkdirSync(codeDir, { recursive: true });
  compileAddedSchemas(ajv, "_refs");
  compileAddedSchemas(ajv, "_schemas");
  ajv._opts.processCode = instrument;
  return ajv;
};

function compileAddedSchemas(ajv, schemasKey) {
  for (var key in ajv[schemasKey]) ajv.getSchema(key);
}

function instrument(code) {
  var filePath = path.join(codeDir, "schema_gen" + uuidv4() + ".js");
  code = beautify(code, { indent_size: 2 });
  fs.writeFileSync(filePath, code);
  return instrumenter.instrumentSync(code, filePath);
}

This creates a new subfolder and puts generated code there. That allows generating html reports containing source code without needing to do instrumentation and reporting in the same step.

If someone knows a way to use CodeCov, coveralls or something similar with generated code that is not part of the repo please let me know. CodeCov doesn't even show the percentages of files that don't exist in the repo. Coveralls does at least show percentage, but no code.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant