s1n*_*7ax 4 unit-testing 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
Run Code Online (Sandbox Code Playgroud)
如果在没有 import 语句的情况下编写测试,则不会出现编译错误,但在运行时会抛出错误,因为describe未定义方法。
测试是打字稿文件很重要,因为必须导入打字稿类。有没有可以与 typescript + webpack 一起使用并在浏览器上运行的库?
import 'mocha'
describe('sample', () => {
it('should return something', () => {
})
});
Run Code Online (Sandbox Code Playgroud)
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
}
};
Run Code Online (Sandbox Code Playgroud)
"scripts": {
"build": "webpack",
"watch": "webpack --watch",
"test:browser": "npm run build && start http://localhost:9000 && webpack-dev-server"
}
Run Code Online (Sandbox Code Playgroud)
找到了。
在 的顶部webpack.config.js添加
const nodeExternals = require('webpack-node-externals');
Run Code Online (Sandbox Code Playgroud)
并添加到配置中:
externals: [nodeExternals()]
Run Code Online (Sandbox Code Playgroud)
作为旁注,在package.json我有 ao
"mocha": "6.2.1",
"webpack": "4.41.0",
"webpack-node-externals": "1.7.2",
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1758 次 |
| 最近记录: |