karma-coverage icon indicating copy to clipboard operation
karma-coverage copied to clipboard

ERROR [preprocessor.coverage]: Unexpected token

Open CodHeK opened this issue 6 years ago • 9 comments

I am trying to add coverage to my project, I keep getting this error every time I run npm run karma

screenshot 2019-02-20 at 3 52 03 am

snippet of algorithms.js

/**
Currently used only for adding 'assert' class to algorithm lists  
*/

import css from "text!../../assets/algorithms.css";

export const name = "core/algorithms";

export async function run() {
  const elements = Array.from(document.querySelectorAll("ol.algorithm li"));
  elements
    .filter(li => li.innerText.trim().startsWith("Assert: "))
    .forEach(li => li.classList.add("assert"));
  if (document.querySelector(".assert")) {
    const style = document.createElement("style");
    style.textContent = css;
    document.head.appendChild(style);
  }
}

karma.conf.js

// Karma configuration
// Generated on Fri Feb 26 2016 13:09:51 GMT+1100 (AEDT)
/*globals module, require, process*/
"use strict";
module.exports = function(config) {
  const options = {
    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: "./",

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ["jasmine", "requirejs", "detectBrowsers"],

    // configuration
    detectBrowsers: {
      enabled: !config.browsers.length,
      usePhantomJS: false,
      postDetection(browsers) {
        return (
          browsers
            // Remove IE
            .filter(browser => browser !== "IE")
        );
      },
    },

    // list of files / patterns to load in the browser
    files: [
      "js/deps/jquery.js",
      {
        pattern: "assets/**/*.*",
        included: false,
        served: true,
      },
      {
        pattern: "tests/support-files/**/*",
        included: false,
        served: true,
      },
      {
        pattern: "builds/**/*.*",
        included: false,
        served: true,
      },
      {
        pattern: "js/deps/marked.js",
        included: false,
        served: true,
      },
      {
        pattern: "js/**/*.*",
        included: false,
        served: true,
      },
      {
        pattern: "tests/**/*-spec.js",
        included: false,
        served: true,
      },
      {
        pattern: "tests/data/**/*",
        included: false,
        served: true,
      },
      {
        pattern: "tests/*.html",
        included: false,
        served: true,
      },
      {
        pattern: "tests/**/*.html",
        included: false,
        served: true,
      },
      {
        pattern: "worker/*.js",
        included: false,
        served: true,
      },
      {
        pattern: "src/**/*.js",
        included: false,
      },
      "tests/spec/SpecHelper.js",
      "tests/test-main.js",
    ],

    // list of files to exclude
    exclude: ["**/*.swp", "*.swp", ".DS_Store"],

    proxies: {
      "/about-blank.html": "/base/tests/about-blank.html",
      "/assets/": "/base/assets/",
      "/js/": "/base/js/",
      "/builds/": "/base/builds/",
      "/tests/": "/base/tests/",
      "/spec/": "/base/tests/spec/",
      "/deps/": "/base/js/deps/",
      "/js/deps/": "/base/js/deps/",
      "/base/deps/": "/base/js/deps/",
      "/worker/respec-worker.js": "/base/worker/respec-worker.js",
      "/support-files/hljs-testlang.js":
        "/base/tests/support-files/hljs-testlang.js",
    },

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      "src/**/*.js": ["coverage"],
    },

    // test results reporter to use
    // possible values: "dots", "progress"
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ["mocha", "progress", "coverage", "coveralls"],

    // web server port
    port: config.port || 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // See "detectBrowsers"
    //browsers: ["Chrome", "Safari", "Firefox"],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: 1,

    browserNoActivityTimeout: 100000,

    client: {
      args: ["--grep", config.grep || ""],
    },

    coverageReporter: {
      includeAllSources: true,
      type: "lcov", // lcov or lcovonly are required for generating lcov.info files needed for coveralls.
      dir: "coverage/",
    },
  };
  if (process.env.TRAVIS) {
    process.env.CHROME_BIN = require("puppeteer").executablePath();
    options.detectBrowsers.enabled = false;
    options.autoWatch = false;
    options.singleRun = true;
    options.concurrency = 1;
    options.reporters = ["mocha", "progress", "coverage", "coveralls"];
    options.browsers = ["ChromeHeadless"];
  }
  config.set(options);
};

CodHeK avatar Feb 19 '19 22:02 CodHeK

Looks like this project uses istanbul 0.4 and async support is v1.1

johnjbarton avatar Feb 20 '19 00:02 johnjbarton

@johnjbarton any work around for this or which packages should I update ? It'd be great if you could help me out here, Ive been stuck here for days now 😕

CodHeK avatar Feb 20 '19 04:02 CodHeK

Here is my package.json

  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.3.3",
    "@babel/preset-env": "^7.3.1",
    "chai": "^4.2.0",
    "clipboard": "^2.0.4",
    "domReady": "github:requirejs/domReady#d85bdc38a6df868ead52ab3e80715aaf60e68765",
    "eslint": "^5.14.0",
    "eslint-config-prettier": "^4.0.0",
    "eslint-plugin-jasmine": "^2.10.1",
    "eslint-plugin-prettier": "^3.0.1",
    "exports-loader": "^0.7.0",
    "handlebars": "^4.1.0",
    "idb": "^3.0.2",
    "jasmine-core": "^3.3.0",
    "jasmine-reporters": "^2.3.2",
    "jquery": "^3.3.1",
    "karma": "^4.0.0",
    "karma-babel-preprocessor": "^8.0.0-beta.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-coverage": "^1.1.2",
    "karma-coveralls": "^2.1.0",
    "karma-detect-browsers": "^2.3.3",
    "karma-edge-launcher": "^0.4.2",
    "karma-firefox-launcher": "^1.1.0",
    "karma-jasmine": "^2.0.1",
    "karma-mocha": "^1.3.0",
    "karma-mocha-reporter": "^2.2.5",
    "karma-opera-launcher": "^1.0.0",
    "karma-requirejs": "^1.1.0",
    "karma-safari-launcher": "^1.0.0",
    "karma-safaritechpreview-launcher": "2.0.2",
    "karma-verbose-summary-reporter": "0.0.1",
    "mocha": "^6.0.0",
    "moment": "^2.24.0",
    "pluralize": "^7.0.0",
    "prettier": "^1.16.4",
    "raw-loader": "^1.0.0",
    "requirejs": "^2.3.6",
    "serve": "^10.1.2",
    "text": "github:requirejs/text#d04de4ffd7bf5ba6cb80cdca2d40d4f6f52a1b1f",
    "typescript": "^3.3.3",
    "webidl2": "^17.0.2",
    "webpack": "^4.29.4",
    "webpack-cli": "^3.2.3"
  },

CodHeK avatar Feb 20 '19 04:02 CodHeK

I think this project will require work to fix tests and upgrade it.

Did you look at https://www.npmjs.com/package/karma-coverage-istanbul-reporter?

johnjbarton avatar Feb 20 '19 15:02 johnjbarton

Will try this out!

CodHeK avatar Feb 22 '19 04:02 CodHeK

Will try this out!

@johnjbarton tried this package, it generated an lcov.info file but it's always empty not sure why...

This is my karma.conf.js

// Karma configuration
// Generated on Fri Feb 26 2016 13:09:51 GMT+1100 (AEDT)
/*globals module, require, process*/
"use strict";
const path = require('path');
module.exports = function(config) {
  const options = {
    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: "./",

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ["jasmine", "requirejs", "detectBrowsers"],

    // configuration
    detectBrowsers: {
      enabled: !config.browsers.length,
      usePhantomJS: false,
      postDetection(browsers) {
        return (
          browsers
            // Remove IE
            .filter(browser => browser !== "IE")
        );
      },
    },

    // list of files / patterns to load in the browser
    files: [
      "js/deps/jquery.js",
      {
        pattern: "assets/**/*.*",
        included: false,
        served: true,
      },
      {
        pattern: "tests/support-files/**/*",
        included: false,
        served: true,
      },
      {
        pattern: "builds/**/*.*",
        included: false,
        served: true,
      },
      {
        pattern: "js/deps/marked.js",
        included: false,
        served: true,
      },
      {
        pattern: "js/**/*.*",
        included: false,
        served: true,
      },
      {
        pattern: "tests/**/*-spec.js",
        included: false,
        served: true,
      },
      {
        pattern: "tests/data/**/*",
        included: false,
        served: true,
      },
      {
        pattern: "tests/*.html",
        included: false,
        served: true,
      },
      {
        pattern: "tests/**/*.html",
        included: false,
        served: true,
      },
      {
        pattern: "worker/*.js",
        included: false,
        served: true,
      },
      {
        pattern: "src/**/*.js",
        included: false,
      },
      "tests/spec/SpecHelper.js",
      "tests/test-main.js",
    ],

    // list of files to exclude
    exclude: ["**/*.swp", "*.swp", ".DS_Store"],

    proxies: {
      "/about-blank.html": "/base/tests/about-blank.html",
      "/assets/": "/base/assets/",
      "/js/": "/base/js/",
      "/builds/": "/base/builds/",
      "/tests/": "/base/tests/",
      "/spec/": "/base/tests/spec/",
      "/deps/": "/base/js/deps/",
      "/js/deps/": "/base/js/deps/",
      "/base/deps/": "/base/js/deps/",
      "/worker/respec-worker.js": "/base/worker/respec-worker.js",
      "/support-files/hljs-testlang.js":
        "/base/tests/support-files/hljs-testlang.js",
    },

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {},

    // test results reporter to use
    // possible values: "dots", "progress"
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ["mocha", "coverage-istanbul", "coveralls"],

    // web server port
    port: config.port || 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // See "detectBrowsers"
    //browsers: ["Chrome", "Safari", "Firefox"],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: 1,

    browserNoActivityTimeout: 100000,

    client: {
      args: ["--grep", config.grep || ""],
    },

    // any of these options are valid: https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-api/lib/config.js#L33-L39
    coverageIstanbulReporter: {
      // reports can be any that are listed here: https://github.com/istanbuljs/istanbuljs/tree/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib
      reports: ['lcovonly'],

      // base output directory. If you include %browser% in the path it will be replaced with the karma browser name
      dir: path.join(__dirname, 'coverage'),

      // Combines coverage information from multiple browsers into one report rather than outputting a report
      // for each browser.
      combineBrowserReports: true,

      // if using webpack and pre-loaders, work around webpack breaking the source path
      fixWebpackSourcePaths: true,

      // Omit files with no statements, no functions and no branches from the report
      skipFilesWithNoCoverage: true,

      // Most reporters accept additional config options. You can pass these through the `report-config` option
      'report-config': {},

      // enforce percentage thresholds
      // anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode
      thresholds: {
        emitWarning: false, // set to `true` to not fail the test command when thresholds are not met
        // thresholds for all files
        global: {
          statements: 100,
          lines: 100,
          branches: 100,
          functions: 100
        },
        // thresholds per file
        each: {
          statements: 100,
          lines: 100,
          branches: 100,
          functions: 100,
        }
      },

      verbose: true // output config used by istanbul for debugging
    }
  };
  if (process.env.TRAVIS) {
    process.env.CHROME_BIN = require("puppeteer").executablePath();
    options.detectBrowsers.enabled = false;
    options.autoWatch = false;
    options.singleRun = true;
    options.concurrency = 1;
    options.reporters = ["mocha", "progress", "coverage", "coveralls"];
    options.browsers = ["ChromeHeadless"];
  }
  config.set(options);
};

CodHeK avatar Feb 22 '19 05:02 CodHeK

The error I get when I run npm run karma - it ends abruptly after displaying the verbose output from the istanbul reporter.

Error :

screenshot 2019-02-22 at 10 51 35 am

CodHeK avatar Feb 22 '19 05:02 CodHeK

Are there any updates in this? I want to run karma with jasmine in Chrome, but I don't want to transpile / bundle my ES6 modules. I can run the specs right now, but when I add coverage I get the Unexpected token error. How can I get karma-coverage to work with ES6 modules? Thanks!

micronaut avatar Apr 11 '19 23:04 micronaut

Getting ERROR : [preprocessor.coverage]: Line 236: Unexpected token ... preprocessor is not able read spread operator

Can u please help ?

AN321657 avatar Jul 29 '20 12:07 AN321657