- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Phaser3 开发一款游戏,并决定为游戏的某些组件添加一些测试。
我使用 Typescript 3.3 和 webpack 4 作为我的主要工具。我想在浏览器上运行测试,其中一些测试可能依赖于 Phaser 游戏库。过去我曾使用 Karma+Jasmine 来测试 Angular.js 应用程序,所以我想我会使用它。
经过一些设置后,我运行了测试,但由于某种原因,测试文件无法导入其他模块,而是出现错误
Compiled with warnings.
ℹ 「wdm」: Compiling...
✖ 「wdm」:
ERROR in ./src/test/core/EntityFactory.spec.ts
Module not found: Error: Can't resolve './foo' in '<path to the project>/src/test/core'
@ ./src/test/core/EntityFactory.spec.ts 1:0-26
ℹ 「wdm」: Failed to compile.
Module not found: Error: Can't resolve './foo' in '/<path to the project>/src/test/core'
提到的 /test/core
目录包含文件 EntityFactory.spec.ts
和 foo.ts
。导入在套件中写为:
import thing from './foo';
下面是我的 karma 配置文件和它使用的 webpack 配置:
// Karma configuration
// Generated on Sun Mar 17 2019 17:03:14 GMT+0200 (EET)
const webpackConfig = require('./webpack.config').test;
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: [
'**/*.spec.ts'
// each file acts as entry point for the webpack configuration
],
mime: {
"text/x-typescript": ["ts", "tsx"],
},
// list of files / patterns to exclude
exclude: [
// '**/*.spec.ts'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
// add webpack as preprocessor
'**/*.spec.ts': [ 'webpack' ],
},
webpack: webpackConfig,
webpackMiddleware: {
// webpack-dev-middleware configuration
// i. e.
stats: 'errors-only'
},
// 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: false,
// 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: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
和 Webpack 配置:
const baseConfig = {
entry: './src/index.ts',
mode: 'development',
output: {
path: path.resolve(__dirname, 'build'),
publicPath: '/',
chunkFilename: '[name].js',
filename: '[name].js'
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
// Source maps support ('inline-source-map' also works)
devtool: 'source-map',
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader'
},
{
test: /\.js$/,
use: ['source-map-loader'],
enforce: 'pre'
}
]
},
optimization: {
runtimeChunk: {
name: 'manifest'
},
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'initial'
}
}
}
},
plugins: [
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, 'assets'),
to: path.resolve(__dirname, 'build', 'assets')
}
]),
new webpack.DefinePlugin({
CANVAS_RENDERER: JSON.stringify(true),
WEBGL_RENDERER: JSON.stringify(true)
}),
new MiniHtmlWebpackPlugin({
context: {
title: 'Phaser game'
},
// FIXME: CSS-loader and default minithmlwebpack template would probably handle this better
template: ({js, title, publicPath } ) =>
`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${title}</title>
<style type = "text/css">
body {
padding: 0;
margin: 0;
}
canvas {
display:block;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
${generateJSReferences(js, publicPath)}
</body>
</html>
`
})
]
};
function getTestConfig() {
const config = _.cloneDeep(baseConfig);
delete config.entry;
return config;
}
getTestConfig()
函数稍微更改了 baseConfig 并将结果传递给 Karma 配置。
最后是开发依赖,以防万一可能有帮助:
"devDependencies": {
"@types/jasmine": "^3.3.10",
"browser-sync": "^2.26.3",
"browser-sync-webpack-plugin": "^2.2.2",
"copy-webpack-plugin": "^4.6.0",
"jasmine": "^3.3.1",
"karma": "^4.0.1",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^2.0.1",
"karma-webpack": "^3.0.5",
"mini-html-webpack-plugin": "^0.2.3",
"prettier": "^1.16.4",
"source-map-loader": "^0.2.4",
"ts-loader": "^5.3.3",
"tslint": "^5.12.1",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.3",
"webpack": "^4.29.3",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.1.14"
},
所以我的问题是,为什么测试似乎无法导入我要测试的模块?问题出在 Webpack、Karma 还是 Typescript?
最佳答案
这听起来像是 Typescript 没有编译您要包含的模块 (foo
)。将重现错误的小测试用例放在一起会有所帮助,但您应该检查 tsconfig.json
以确保它包含您希望能够测试的所有代码。
我有一个单独的 tsconfig.test.json
覆盖应用程序的主要入口点(files: ["src/index.ts"]
)以及所有测试(包括:["src/**/*.spec.ts"]
)。如果你走这条路,也许可以制作一个带有常用选项的 tsconfig.base.json
,然后将其包含在你的生产 tsconfig.json
以及测试中。然后你需要将你的 Webpack 配置指向正确的文件,比如
{
test: /\.tsx?$/,
use: {
loader: 'ts-loader',
options: {
configFile: 'tsconfig.test.json',
}
}
},
您可以将 configFile
默认设置为 tsconfig.json
,然后您的 getTestConfig()
函数可以更改它以供 使用 karma 网络包
。
关于typescript - 如何配置 Typescript、Karma、Webpack 以在浏览器中运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55209332/
我的“登录”是在 IE 浏览器中进行的,登录后,如果我单击链接,它们就会在 Chrome 浏览器中打开。如何在同一个测试用例中将我当前的浏览器从 IE 切换到 Chrome。如果我创建一个 chrom
您好,我不明白在 Firefox 的内容属性中使用的特殊字符的不同显示行为。我已经剥离了一切并创造了一支笔: http://codepen.io/rpkoller/pen/Fbgav 在其“基本形式”
我正在研究 Spring Data REST,特别是 HAL 浏览器。我一直在关注 http://docs.spring.io/spring-data/rest/docs/current/refere
我正在使用工具提示,在 ie 上出现定位错误。我放了jquery浏览器代码 我的工具提示 $('.tooltip').tooltip({ position: "bottom center"
我应该如何处理蓝鸟协程中的错误? 我使用co in节点已有一段时间,它具有出色的捕获功能。 co(function*() { return new Promise(function(resol
package webviewbrowser; import java.util.List; import javafx.application.Application; import javafx.
我有一些 JavaScript 在同一域上的两个独立服务器之间共享请求。 .com 是 JavaScript 中域的要求吗? 在这种情况下,两台服务器都位于 .abc.tyy 域上,tyy 通常是 .
package webviewbrowser; import java.util.List; import javafx.application.Application; import javafx.
我正在尝试构建仍支持 NPAPI 的先前版本的 Chromium 浏览器。我已经获得了代码,并且可以使用 stand build 命令在我的 mac 上构建最新版本的 Chromium gclient
我环顾四周,找不到 browscap 的 Python 等效项(我在 PHP 中使用它来检测给定的用户代理字符串是什么浏览器。 我希望我不必自己写......:P 最佳答案 看看这个,它应该做你想要的
是否有任何 chrome 或 firefox 扩展允许 javascript 在客户端 PC 中创建写入文件? 最佳答案 你想做什么? HTML5 有一个 File API .这是最好的解决方案,因为
当我点击链接或刷新或关闭标签页时,我有这段代码会发出警报。 但我需要在关闭 窗口(选项卡)上仅 发出警报。怎么做? 我的网站上有很多外部和内部链接。
我目前正在尝试使用 Browserify + Angular,但我遇到了一个奇怪的问题。我在我的 Controller 的子目录中创建了一个名为 controllers/start-controlle
我正在为客户(项目已被接受,但现在是解释不同功能的问题)写一份详细的估算,以开发一个响应式布局的网站。 这不是我第一次进行此类开发,但这是一个关键客户,必须铺平道路。 布局将从 300px 宽度调整到
我在时事通讯上设计了一些黑底白字。由于时事通讯在打印时看起来不错且可读。我需要使布局和文本与浏览器中的内容相似。 通常情况下,黑色文本和无背景颜色是浏览器/网络邮件客户端的默认打印样式吗? 最佳答案
我有一个使用 GWT/mGWT 构建的移动友好网络应用程序。该应用程序有白色输入文本框和深灰色输入文本。但是,在 Android 浏览器上,文本显示为白色,因此是不可见的。我尝试的所有 CSS 都无法
我创建了一个带有选择输入的页面来更改正在使用的 jQuery UI 主题。当主题更改时,它会存储在 cookie 中。页面加载时,如果 cookie 存在,则恢复主题,否则加载默认主题。 当我使用 F
在我的 CSS 中,我使用了以下代码片段: word-break: break-word; -webkit-hyphens: auto; hyphens: auto; 渲染引擎如何知道在所有不同语言中
我的网络浏览器 Safari 有问题,我在 Chrome、FireFox 中测试了我的网站。 Safari 版本也是正确的,但是,当需要在 1920x1080 或更高分辨率下对其进行测试时,它无法正常
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我是一名优秀的程序员,十分优秀!