Karma Webpack - Error: Cannot find module "./test/utilities.js"

Karma Webpack - Error: Cannot find module "./test/utilities.js"

我正在使用 Karma Webpack 进行项目的单元测试。

当我运行karma start时,我有这个错误:

Error: Cannot find module "./test/utilities.js" at /myproject/test/campaign.test.js:49 <- webpack:///test/campaign.test.js:3:0

这是我的文件内容 campaign_test.js:

"use strict";

var utilities = require('./test/utilities.js');

describe(" campaign unit tests", function () {
  var error = null;
  var user = null;

  utilities.initParse();  
});

这是 karma.conf.js 文件:

var webpack = require("webpack"),
path = require("path");

// Karma configuration
// Generated on Mon May 11 2015 14:13:57 GMT-0600 (MDT)

module.exports = function(config) {
  config.set({
    basePath: "test",
    frameworks: ["jasmine"],
    files: [
        "**/*.js"
    ],
    preprocessors: {
        "**/*.js": ["webpack",  "sourcemap"]
    },
    webpack: {
        plugins: [
            new webpack.ResolverPlugin([
                new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
            ])
        ],
        resolve: {
            root: [path.join(__dirname, "./bower_components"), path.join(__dirname, "./src")]
        },
        devtool: "inline-source-map",
        resolve: {
            alias: {
                app: path.join(__dirname, 'test')
            }
        }
    },
    webpackMiddleware: {
        progress: false,
        stats: false,
        debug: true,
        noInfo: true,
        silent: true 
    },
    plugins: [
        require("karma-webpack"),
        require("karma-jasmine"),
        require("karma-phantomjs-launcher"),
        require("karma-sourcemap-loader"),
    ],
    reporters: ["dots"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ["PhantomJS"],
    singleRun: false
  });
};

这是我的项目结构:

package.json
karma.conf.js
test/
    campaign.test.js
    game.test.js
    user.test.js
    utilities.test.js
    utilities.js

有什么建议吗?

依赖路径是相对于当前脚本的。所以只需重写您的要求声明:

var utilities = require('./utilities.js');