所以我尝试使用webpack和selenium-webdriver在JavaScript项目上设置测试。但是,当我需要来自test / index.js文件的测试文件以便在浏览器中运行时,npm start会因一系列“无法解决模块”错误而崩溃。我所需测试的标题如下所示:
//=> test/meal_test.js
const assert = require('chai').assert;
const webdriver = require('selenium-webdriver');
const Meal = require("../lib/meal.js")
const until = webdriver.until;
const frontEndLocation = "http://localhost:8080"
describe('test meal object', function() {
it('should meal info and food info', function() {
const meal = new Meal({
"id": 1,
"name": "Breakfast",
"foods": [
{ "id": 4, "name": "Grapes", "calories": 180 },
{ "id": 6, "name": "Yogurt", "calories": 550 },
{ "id": 7, "name": "Macaroni and Cheese", "calories": 950 }
]
})
assert.equal(meal.id, 1)
assert.equal(meal.name, 'Breakfast')
assert(Array.isArray(meal.foods));
assert.equal(meal.foods.length, 3)
assert.equal(meal.total, 1680)
})
})
我的test / index.js看起来像这样:
//=> test/index.js
require('./meal_test.js')
我的webpack.config.js看起来像这样:
const path = require('path');
module.exports = {
entry: {
main: "./lib/index.js",
test: "mocha!./test/index.js"
},
output: {
path: __dirname,
filename: "[name].bundle.js"
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' },
{ test: /\.css$/, loader: "style!css" },
{ test: /\.scss$/, loaders: ["style-loader", "css-loader", "sass-loader"] },
]
},
resolve: {
extensions: ['', '.js', '.json', '.css']
},
};
我的package.json看起来像这样:
{
"name": "quantified-self-starter-kit",
"version": "1.0.0",
"description": "Starter kit for Turing School's BEE M4 Quantified Self Project",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --hot --inline",
"build": "webpack",
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/turingschool-examples/game-time-starter-kit-FEm1.git"
},
"author": "Turing School of Software and Design - Nate Allen",
"license": "MIT",
"bugs": {
"url": "https://github.com/turingschool-examples/quantified-self-starter-kit/issues"
},
"homepage": "https://github.com/turingschool-examples/quantified-self-starter-kit#readme",
"devDependencies": {
"babel": "^6.5.2",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"chai": "^3.5.0",
"chromedriver": "^2.27.2",
"css-loader": "^0.26.0",
"jquery": "^3.1.1",
"mocha": "^3.1.2",
"mocha-loader": "^1.0.0",
"node-libs-browser": "^1.0.0",
"node-sass": "^4.3.0",
"sass-loader": "^4.1.1",
"selenium-webdriver": "^3.0.1",
"sinon": "^3.2.1",
"style-loader": "^0.13.1",
"webdriverjs": "^1.7.5",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.0"
},
"dependencies": {
"fs": "0.0.1-security"
}
}
运行npm start
时出现的疯狂错误如下所示:
ERROR in ./~/selenium-webdriver/chrome.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver
@ ./~/selenium-webdriver/chrome.js 131:11-24
ERROR in ./~/selenium-webdriver/io/index.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/io
@ ./~/selenium-webdriver/io/index.js 20:9-22
ERROR in ./~/rimraf/rimraf.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/rimraf
@ ./~/rimraf/rimraf.js 6:9-22
ERROR in ./~/glob/glob.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/glob
@ ./~/glob/glob.js 43:9-22
ERROR in ./~/fs.realpath/index.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/fs.realpath
@ ./~/fs.realpath/index.js 8:9-22
ERROR in ./~/fs.realpath/old.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/fs.realpath
@ ./~/fs.realpath/old.js 24:9-22
ERROR in ./~/glob/sync.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/glob
@ ./~/glob/sync.js 4:9-22
ERROR in ./~/tmp/lib/tmp.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/tmp/lib
@ ./~/tmp/lib/tmp.js 13:11-24
ERROR in ./~/selenium-webdriver/net/portprober.js
Module not found: Error: Cannot resolve module 'child_process' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/net
@ ./~/selenium-webdriver/net/portprober.js 20:11-35
ERROR in ./~/selenium-webdriver/net/portprober.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/net
@ ./~/selenium-webdriver/net/portprober.js 21:9-22
ERROR in ./~/selenium-webdriver/net/portprober.js
Module not found: Error: Cannot resolve module 'net' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/net
@ ./~/selenium-webdriver/net/portprober.js 22:10-24
ERROR in ./~/selenium-webdriver/remote/index.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/remote
@ ./~/selenium-webdriver/remote/index.js 20:11-24
ERROR in ./~/selenium-webdriver/io/exec.js
Module not found: Error: Cannot resolve module 'child_process' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/io
@ ./~/selenium-webdriver/io/exec.js 20:21-45
ERROR in ./~/selenium-webdriver/edge.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver
@ ./~/selenium-webdriver/edge.js 73:11-24
ERROR in ./~/selenium-webdriver/firefox/binary.js
Module not found: Error: Cannot resolve module 'child_process' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/firefox
@ ./~/selenium-webdriver/firefox/binary.js 25:14-38
ERROR in ./~/selenium-webdriver/firefox/binary.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/firefox
@ ./~/selenium-webdriver/firefox/binary.js 26:9-22
ERROR in ./~/selenium-webdriver/lib/devmode.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/lib
@ ./~/selenium-webdriver/lib/devmode.js 25:11-24
ERROR in ./~/selenium-webdriver/firefox/profile.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/firefox
@ ./~/selenium-webdriver/firefox/profile.js 25:11-24
ERROR in ./~/selenium-webdriver/firefox/extension.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver/firefox
@ ./~/selenium-webdriver/firefox/extension.js 22:11-24
ERROR in ./~/selenium-webdriver/ie.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver
@ ./~/selenium-webdriver/ie.js 30:11-24
ERROR in ./~/selenium-webdriver/opera.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver
@ ./~/selenium-webdriver/opera.js 75:11-24
ERROR in ./~/selenium-webdriver/phantomjs.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Home/turing/4module/quantified-self-client/node_modules/selenium-webdriver
@ ./~/selenium-webdriver/phantomjs.js 47:11-24
当我从index.js中删除require语句时,构建和测试运行正常,但是我无法访问localhost:3000 / test.html上的测试。
我还尝试设置"target": "node"
和npm install fs --save
无效。
我已经尝试逐一安装一些模块依赖项,但这也没有帮助。
它似乎与该行直接相关:const webdriver = require('selenium-webdriver');
删除该行时npm start
工作正常。
我不确定这是否真的是一个问题,或者这只是一起使用这些工具时的预期行为。
有没有办法在我的index.js中要求我的端到端集成测试,然后在我的test.html中运行?
非常感谢任何帮助,见解或建议!