1

I’d like a sanity check. I’m running tests on a bunch of web components using karma + webpack (so karma-webpack). The app itself compiles and all those things perfectly fine. But when I try to do the same things via karma-webpack (using the same config even), it doesn’t seem to work. It keeps erroring out on compile when I import anything which isn’t js, which webpack should’ve processed. So for simplicity sake (these are headless unit tests), if its any of those things, attempting to just null-loader them:

test.webpack.config.js

module.exports = {
  module: {
    devtool: "inline-source-map",
    module: {
      rules: [
        {
          test: /\.(png|svg|jpg|gif|scss)$/,
          loader: "null-loader"
        },
        {
          test: /\.js$/,
          loader: "babel-loader",
          options: {
            cacheDirectory: true
          }
        }
      ]
    }
  }
};

karma.conf.js:

const path = require("path");

module.exports = function(config) {
  config.set({
    basePath: "",
    singleRun: true,
    browsers: ["ChromeHeadlessNoSandbox", "FirefoxHeadless"],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: "ChromeHeadless",
        flags: ["--disable-gpu", "--no-sandbox"]
      },
      FirefoxHeadless: {
        base: "Firefox",
        flags: ["-headless"]
      }
    },
    frameworks: ["mocha", "sinon-chai"],
    files: [
      {
        pattern:
          "../../node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js",
        watched: false
      },
      {
        pattern:
          "../../node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js",
        watched: false
      },
      "test/unit/index.js"
    ],
    preprocessors: {
      "test/unit/index.js": ["webpack", "sourcemap"]
    },
    reporters: ["dots"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    client: {
      mocha: {
        reporter: "html",
        ui: "bdd"
      },
      chai: {
        includeStack: true
      }
    },

    webpack: require("./test.webpack.config"),

    webpackMiddleware: {
      stats: "errors-only"
    },

    webpackServer: {
      noInfo: true
    },

    webpack: {
      output: {
        filename: "[name]"
      }
    }
  });
};

The resulting error will be something like:

ERROR in ../my/project/src/assets/images/_proto/dashboard.png 1:0 Module parse failed: Unexpected character '�' (1:0) You may need an appropriate loader to handle this file type. (Source code omitted for this binary file)

1 Answers1

0

So, we declared webpack twice. The second declaration overrode the first. :'(