Karma Webpack - 错误:找不到模块“./test/utilities.js”

时间:2015-10-15 15:45:26

标签: javascript karma-runner webpack

我正在使用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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

依赖关系路径与当前脚本相关。所以只需重写你的require语句:

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