簡體   English   中英

使用karma-webpack時找不到模塊錯誤

[英]Cannot find module error when using karma-webpack

我有以下karma.conf.js文件,我還有一個名為example.spec.js的測試文件,該文件位於app/compontents/example/example.spec.js我正在嘗試導入Example.js file

import Example from 'app/components/Example/Example.jsx';

describe('Example Component', function() {

});

但我得到以下錯誤Error: Cannot find module "app/components/Example/Example.jsx"我已經嘗試了許多不同的想法,比如./Example.jsx./Examplecomponents/Example/Example.jsx但每時間我得到同樣的錯誤。

var webpack = require('webpack');

module.exports = function(config) {
 config.set({

// milliseconds
browserNoActivityTimeout: 40000,

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'],


// list of files / patterns to load in the browser
files: [
  'app/tests/setup.js',
  'app/**/*.spec.js'
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
  'app/tests/setup.js': ['webpack', 'sourcemap'],
  'app/**/*.spec.js': ['webpack', 'sourcemap']
},


webpack: {
  devtool: 'inline-source-map',
  cache: false,
  resolve: {
    extensions: ['', '.js', '.jsx'],
    modulesDirectories: ['node_modules', 'app'],
    fallback: __dirname
  },
  module: {
    preLoaders: [],
    loaders: [
      { test: /\.(js|jsx)$/, loaders: ['babel-loader'], exclude: /node_modules/ },
    ],
    plugins: [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify('test')
      })
    ]
  }
},


webpackMiddleware: {
  progress: false,
  stats: false,
  debug: false,
  noInfo: true,
  silent: true
},


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


// web server port
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: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],


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

plugins: [
  'karma-mocha',
  'karma-chai',
  'karma-webpack',
  'karma-sourcemap-loader',
  'karma-phantomjs-launcher'
]
});
};

如果要使用這樣的導入,則需要設置resolve.alias 演示:

resolve: {
    alias: {
        app: path.join(__dirname, 'app')
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM