typescript - 浏览器单元测试(Typescript + Webpack)
问题描述
我想向这个项目添加单元测试。这是一个基本的 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
如果 test 是在没有 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
}
};
包.json
"scripts": {
"build": "webpack",
"watch": "webpack --watch",
"test:browser": "npm run build && start http://localhost:9000 && webpack-dev-server"
}
解决方案
找到了。
在 的顶部webpack.config.js
,添加
const nodeExternals = require('webpack-node-externals');
并添加到配置中:
externals: [nodeExternals()]
作为旁注,在package.json
我有一个
"mocha": "6.2.1",
"webpack": "4.41.0",
"webpack-node-externals": "1.7.2",
推荐阅读
- jenkins - 如何从 Jenkins 管道启用并发构建
- android - 当我在 Android Studio 中使用导航第二次启动操作时,为什么导航目的地会丢失?
- javascript - 向左或向右看时,如何使子弹不随角色移动?
- node.js - 使用 Nodejs 在 Mongodb 中导入文件
- reactjs - 将变量传递给 React useCallback 钩子
- angular - 无法使用 angular-6-social-login 在 ionic 中使用 google 登录
- java - 使用任何 JavaFX 指令时 Gradle 项目中的 IllegalAccessError
- java - Spring Batch:处理块后发送通知
- matlab - 如何在MATLAB中为矩阵的列向量生成所有可能的组合?
- .net-core - 错误消息“在更新模式下无法多次打开条目。” 在电子表格精简版另存为功能