使用karma-webpack时找不到模块错误

joshuahornby10

我有以下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'
]
});
};
JuhoVepsäläinen

如果要使用这样的导入,则需要设置resolve.alias演示:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用modprobe的“致命:找不到模块错误”

错误:使用Ionic时找不到模块'../lib/utils/unsupported.js'

使用Webpack时,节点找不到模块“ fs”

错误:使用Mocha测试时找不到模块

使用npm时找不到模块'npmconf'

使用Webpack时找不到导入模块的字体文件

找不到模块:错误:使用webpack时无法解析模块“反应”

在Karma,Webpack,PhantomJS中运行单元测试时如何解决错误“ ReferenceError:找不到变量:require”

使用Webpack时,Typescript编译器“找不到模块”要求CSS /图像资产

在Cordova中使用Webpack时出现“找不到文件”错误

在.Net Core 2.0和Angular中使用'dotnet publish'时找不到模块'aspnet-webpack'

未捕获的错误:使用Webpack时找不到模块“ fs”

找不到模块:错误:使用@ babel / polyfill或@ babel / runtime时无法解析“导入”

使用TypeScript 3项目参考时出现“找不到模块”错误

使用pytest时找不到模块

错误:使用StencilJS克隆Ionic 4组件时出现“找不到模块”

将Webpack worker-loader与Typescript一起使用导致找不到模块错误

使用库时,在生产模式下出现“错误:找不到模块'@ app / foo'”

找不到模块:错误:使用外部JS库时无法解析...

使用WebPack时找不到功能

使用Docker时找不到Python模块

使用tsconfig-paths-webpack-plugin会为“ fs”和其他标准模块提供“找不到模块”错误

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

导入错误:DLL加载失败:使用cxfreeze时找不到指定的模块

错误 TS2304:使用 karma-typescript 时找不到名称“Map”

错误:找不到模块“@angular-cli/plugins/karma”

Webpack:使用 webpack.ignorePlugin 时出现“找不到模块”问题

TypeScript:错误:使用路径时找不到模块

使用 NPM 在 Ubuntu 上安装 git-stats 时找不到模块错误