如何让 Karma + Webpack 找到模块?
How can I get Karma + Webpack to find the module?
在 webpack 通过 Karma 测试 运行ner 将它们组合在一起后,我想 运行 我对一堆模块进行测试,但是每当我 运行 我的测试 Karma 说,
"Error: Cannot find module "hello.js" at
http://localhost:9877/base/src/hello.spec.js?d301966ffc1330826574d9d8fff5a644c3390c68:47"
我有一个规范文件:
var a = require('hello.js');
describe("a test test", function() {
it("humperdink test", function() {
expect(a).toEqual('humperdink');
}); //end it
}); //end describe
hello.js是这样的:
var a = 'humperdink';
module.exports = a;
这两个文件都在同一个文件夹中。
我的karma.conf.js是:
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
files: [
'src/**/*.js',
'tests/**/*.spec.js'
],
preprocessors: {
'tests/**/*.spec.js': ['webpack'],
'src/**/*.js' : ['webpack']
},
browsers: ['PhantomJS'],
webpack: {
entry: './src/hello.spec.js',
output: {
filename: 'bundle.js'
}
},
webpackMiddleware: {
noInfo: true
}
})
};
目前我安装的 devDependencies 是
"devDependencies": {
"jasmine-core": "^2.3.4",
"jshint": "^2.8.0",
"karma": "^0.13.15",
"karma-jasmine": "^0.3.6",
"karma-jshint-preprocessor": "0.0.6",
"karma-phantomjs-launcher": "^0.2.1",
"karma-webpack": "^1.7.0",
"phantomjs": "^1.9.19",
"sinon": "^1.17.2",
"webpack": "^1.12.9"
如何让 Karma 找到 hello.js 模块?
我尝试将规范文件的第一行更改为
require('hello.js');
或
require('./hello.js');
或
require('hello');
根据
的建议
我不认为这里有什么太复杂的事情,比如 。
我已经检查以确保 Karma 测试 运行ner 正常工作。如果我 运行 在它自己的文件中进行一个非常简单的测试,它就可以正常工作。
如何解决这个问题?
我已经复制了你的项目并修复了它。继 https://github.com/webpack/karma-webpack
在规范中:
var a = require('../src/hello.js');
karma.conf.js:
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
files: [
//'src/**/*.js', <-------- Remove or comment this
'tests/**/*.spec.js'
],
preprocessors: {
'tests/**/*.spec.js': ['webpack'],
'src/**/*.js' : ['webpack']
},
browsers: ['PhantomJS'],
webpack: {
entry: './tests/hello.spec.js',
output: {
filename: 'bundle.js'
}
},
webpackMiddleware: {
noInfo: true
}
})
};
此外 npm test 命令:
在 package.json 中:
"scripts": {
"test": "./node_modules/karma/bin/karma start"
}
在 webpack 通过 Karma 测试 运行ner 将它们组合在一起后,我想 运行 我对一堆模块进行测试,但是每当我 运行 我的测试 Karma 说,
"Error: Cannot find module "hello.js" at http://localhost:9877/base/src/hello.spec.js?d301966ffc1330826574d9d8fff5a644c3390c68:47"
我有一个规范文件:
var a = require('hello.js');
describe("a test test", function() {
it("humperdink test", function() {
expect(a).toEqual('humperdink');
}); //end it
}); //end describe
hello.js是这样的:
var a = 'humperdink';
module.exports = a;
这两个文件都在同一个文件夹中。
我的karma.conf.js是:
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
files: [
'src/**/*.js',
'tests/**/*.spec.js'
],
preprocessors: {
'tests/**/*.spec.js': ['webpack'],
'src/**/*.js' : ['webpack']
},
browsers: ['PhantomJS'],
webpack: {
entry: './src/hello.spec.js',
output: {
filename: 'bundle.js'
}
},
webpackMiddleware: {
noInfo: true
}
})
};
目前我安装的 devDependencies 是
"devDependencies": {
"jasmine-core": "^2.3.4",
"jshint": "^2.8.0",
"karma": "^0.13.15",
"karma-jasmine": "^0.3.6",
"karma-jshint-preprocessor": "0.0.6",
"karma-phantomjs-launcher": "^0.2.1",
"karma-webpack": "^1.7.0",
"phantomjs": "^1.9.19",
"sinon": "^1.17.2",
"webpack": "^1.12.9"
如何让 Karma 找到 hello.js 模块?
我尝试将规范文件的第一行更改为
require('hello.js');
或
require('./hello.js');
或
require('hello');
根据
我不认为这里有什么太复杂的事情,比如
我已经检查以确保 Karma 测试 运行ner 正常工作。如果我 运行 在它自己的文件中进行一个非常简单的测试,它就可以正常工作。
如何解决这个问题?
我已经复制了你的项目并修复了它。继 https://github.com/webpack/karma-webpack
在规范中:
var a = require('../src/hello.js');
karma.conf.js:
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
files: [
//'src/**/*.js', <-------- Remove or comment this
'tests/**/*.spec.js'
],
preprocessors: {
'tests/**/*.spec.js': ['webpack'],
'src/**/*.js' : ['webpack']
},
browsers: ['PhantomJS'],
webpack: {
entry: './tests/hello.spec.js',
output: {
filename: 'bundle.js'
}
},
webpackMiddleware: {
noInfo: true
}
})
};
此外 npm test 命令: 在 package.json 中:
"scripts": {
"test": "./node_modules/karma/bin/karma start"
}