[英]Using Webpack with Karma causes node modules loading error
I am trying to just run my Jasmine Test with Karma and I believe some of the modules that should be loaded are not loaded, it could be a dependency issue but I am running out of ideas. 我试图用Karma运行我的Jasmine测试,我相信一些应该加载的模块没有加载,它可能是一个依赖问题,但我的想法已经用完了。
karma.conf.js karma.conf.js
var webpackConfig = require('./webpack.config.js');
webpackConfig.entry = {};
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
{ pattern: 'test-context.js'}
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test-context.js': ['webpack']
},
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
debug: true
})
}
webpack.config.js webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'babel-polyfill',
'./index.js'
],
output: {
publicPath: '/',
filename: 'main.js'
},
devtool: 'source-map',
resolve: {
extensions: ['', '.js', '.jsx']
},
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader'
}
],
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
node: {
net: 'empty',
tls: 'empty',
//dns: 'empty',
fs: 'empty'
},
externals: {
//'crypto': 'crypto'
},
debug: true
};
package.json 的package.json
{
"name": "Web-Application",
"version": "0.0.1",
"description": "A web application",
"engines": {
"node": "5.9.1"
},
"main": "index.js",
"scripts": {
"start": "node index.js",
"tests": "karma start"
},
"dependencies": {
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.23.0",
"babel-register": "^6.23.0",
"box-node-sdk": "^1.3.0",
"ejs": "2.4.1",
"express": "4.13.3",
"node-libs-browser": "^2.0.0",
"crypto-browserify": "^3.11.0"
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-getting-started"
},
"keywords": [
"node",
"heroku",
"express"
],
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-loader": "^v6.4.0",
"babel-preset-env": "^1.2.0",
"babel-preset-es2015": "^6.22.0",
"express": "4.13.3",
"jasmine-core": "^2.5.2",
"json-loader": "^0.5.4",
"karma": "^1.5.0",
"karma-babel-preprocessor": "^6.0.1",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.1.0",
"karma-phantomjs2-launcher": "^0.5.0",
"karma-webpack": "^2.0.2",
"webpack": "^1.3.0",
"webpack-dev-server": "1.10.1"
}
}
I added 我补充道
node: {
net: 'empty',
tls: 'empty',
//dns: 'empty',
fs: 'empty'
},
Into the code to remove a previous dependency problem, but that could have just fixed the service layer and maybe there are deeper dependency issues. 在代码中删除以前的依赖问题,但这可能只是修复了服务层,可能还有更深层次的依赖问题。
Stack Trace: 堆栈跟踪:
Git Source Git Source
https://github.com/noobiehacker/revaBoxWeb https://github.com/noobiehacker/revaBoxWeb
Any help or hints is appreciated >< 任何帮助或提示表示赞赏> <
This example is a Node.js app that uses express , which requires Node.js built-in modules like fs
or net
. 这个例子是一个使用express的Node.js应用程序,它需要Node.js内置模块,如
fs
或net
。 But you try to build a web app with webpack (the default target is web
) and in the browser those modules are not available. 但是您尝试使用webpack构建Web应用程序(默认目标是
web
),并且在浏览器中这些模块不可用。 Webpack told you that the modules couldn't be resolved, but you decided to empty them out, which leaves you with a run-time error. Webpack告诉你,模块无法解析,但你决定将它们清空,这会给你带来运行时错误。
In order to use express
with webpack you need to configure target
to node
, so webpack won't touch built-in modules. 为了使用带有webpack的
express
,你需要配置target
to node
,因此webpack不会触及内置模块。
target: 'node'
You also need to remove the node
config you added before. 您还需要删除之前添加的
node
配置。
I am trying to just run my Jasmine Test with Karma
我正试图用Karma进行我的Jasmine测试
As mentioned above, you're building a Node.js app, but Karma is a browser testing tool. 如上所述,您正在构建Node.js应用程序,但Karma是一个浏览器测试工具。 It won't be able to run your code.
它将无法运行您的代码。 Your code is meant to be run by node and also tested by node, not the browser.
您的代码应由节点运行,并且还由节点而不是浏览器进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.