【发布时间】:2019-03-02 21:21:29
【问题描述】:
我想将单元测试添加到this 项目。这是一个基本的 webpack + typescript 项目,将被另一个 web 应用程序使用。单元测试应该在浏览器上运行。
我尝试了mocha,但只是import 'mocha' 引发编译错误。
(我在project_folder/test/test.ts 中有测试文件,这是 webpack 的入口。)
WARNING in ./node_modules/mocha/lib/mocha.js 219:20-37
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
WARNING in ./node_modules/mocha/lib/mocha.js 227:24-70
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
WARNING in ./node_modules/mocha/lib/mocha.js 277:24-35
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
WARNING in ./node_modules/mocha/lib/mocha.js 327:35-48
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
WARNING in ./node_modules/mocha/lib/mocha.js 342:23-44
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
ERROR in ./node_modules/mocha/lib/browser/growl.js
Module not found: Error: Can't resolve '../../package' in 'C:\Users\s1n7ax\Documents\GitHub\open-unicode-converter\node_modules\mocha\lib\browser'
@ ./node_modules/mocha/lib/browser/growl.js 127:13-37
@ ./node_modules/mocha/lib/mocha.js
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
ERROR in ./node_modules/mkdirp/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\s1n7ax\Documents\GitHub\open-unicode-converter\node_modules\mkdirp'
@ ./node_modules/mkdirp/index.js 2:9-22
@ ./node_modules/mocha/lib/reporters/xunit.js
@ ./node_modules/mocha/lib/reporters/index.js
@ ./node_modules/mocha/lib/mocha.js
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! open-unicode-converter@1.0.0 build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the open-unicode-converter@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\s1n7ax\AppData\Roaming\npm-cache\_logs\2019-03-02T20_59_10_221Z-debug.log
如果在没有 import 语句的情况下编写测试,则不会出现编译错误,但在运行时会抛出错误,因为方法 describe 未定义。
测试是一个打字稿文件很重要,因为必须导入打字稿类。是否有可以与 typescript + webpack 一起使用并在浏览器上运行的库?
测试/test.ts
import 'mocha'
describe('sample', () => {
it('should return something', () => {
})
});
webpack.config.js
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
let distPath = path.resolve(__dirname, 'dist');
let srcPath = path.resolve(__dirname, 'src');
let testPath = path.resolve(__dirname, 'test');
module.exports = {
mode: 'development',
devtool: 'inline-source-map',
entry: "./test/index.test.ts",
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
plugins: [
// this copy index.html from test/index.html to dist
new CopyWebpackPlugin([
{from: path.resolve(__dirname, 'test'), to: distPath, ignore: ['*.ts', '*.tsx', '*.js']}
]),
],
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: '[name].js',
path: distPath
},
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
}
};
package.json
"scripts": {
"build": "webpack",
"watch": "webpack --watch",
"test:browser": "npm run build && start http://localhost:9000 && webpack-dev-server"
}
【问题讨论】:
-
您是如何准确执行测试的?
-
将html文件添加到链接已编译js文件的测试文件夹中。使用复制插件将html移动到dist。在 dist 文件夹上启动 webpack-dev-server。
-
如果它是一个 js 文件,这与 mocha web ui 报告完美配合,但找不到与 TS 和 wepack 类似的东西。
-
好的,我的理解是你的生产代码在 import 语句中工作得很好,但测试代码却不行。这意味着您处理它们的方式有所不同。你能告诉我你的与测试代码编译相关的配置文件(webpack config、ts config 等)吗?
-
@DanMacák 更新了问题中的详细信息。
标签: typescript unit-testing webpack