如何使karma-webpack尊重声明的加载程序?

时间:2018-09-13 18:23:22

标签: webpack karma-webpack

我要进行健全性检查。我正在使用karma + webpack(所以karma-webpack)对一堆Web组件进行测试。该应用程序本身可以编译,所有这些东西都很好。但是,当我尝试通过karma-webpack(甚至使用相同的配置)执行相同的操作时,它似乎不起作用。当我导入非js(应该对webpack进行处理)的任何内容时,编译时总是会出错。因此,为了简单起见(这些都是无头单元测试),如果有任何这些事情,请尝试null-loader

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]"
      }
    }
  });
};

产生的错误将类似于:

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 个答案:

答案 0 :(得分:0)

因此,我们两次声明了public RawImage image; public void OpenExplorer() { SimpleFileBrowser.FileBrowser.SetFilters(true, ".txt", ".jpg", ".png", ".mp4"); SimpleFileBrowser.FileBrowser.ShowLoadDialog((path) => { StartCoroutine(UpdateImage(path)); }, null, false, null, "Select File", "Select"); } IEnumerator UpdateImage(string pfad) { if (pfad != null) { WWW www = new WWW(pfad); //WAIT UNTIL REQUEST IS DONE! yield return www; //Check for error if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); } else { image.texture = www.texture; var texWidth = www.texture.width; var texHeight = www.texture.height; if (texWidth > texHeight) { GetComponent<RectTransform>().sizeDelta = new Vector2(1920 / 2, 1080 / 2); } if (texWidth < texHeight) { GetComponent<RectTransform>().sizeDelta = new Vector2(1080 / 2, 1920 / 2); } } } } 。第二条宣言取代了第一条宣言。 :'(