Koe*_*eef 9 unit-testing jasmine typescript webpack
关于如何使用Webpack和Jasmine进行Angular项目单元测试有大量信息.
但我有一个项目使用'普通'打字稿,而不是AngularJs.所以我有ts类,但我不使用组件.我无法弄清楚如何将我找到的信息应用于非AngularJs项目; 一切似乎都适合使用组件.
如何在Typescript Webpack项目中集成Jasmine(ts spec文件)?
我更喜欢使用单独的webpack配置进行测试的解决方案.
package.json 启动脚本使用dev-build启动节点
{
"name": "webpack.typescript",
"version": "1.0.0",
"description": "Webpack + TypeScript",
"main": "dev-build.js",
"author": "Shane Osbourne and John Lindquist",
"license": "MIT",
"scripts": {
"start": "node dev-build"
},
"dependencies": {
"bootstrap": "^3.3.7",
"lodash": "^4.17.4"
},
"devDependencies": {
"@types/lodash": "^4.14.53",
"browser-sync": "^2.18.8",
"bs-pretty-message": "^1.0.8",
"css-loader": "^0.26.2",
"node-sass": "^4.5.0",
"sass-loader": "^6.0.2",
"style-loader": "^0.13.2",
"ts-loader": "^2.0.1",
"typescript": "^2.2.1",
"url-loader": "^0.5.8",
"webpack": "^2.2.1",
"webpack-dev-middleware": "^1.10.1"
}
}
Run Code Online (Sandbox Code Playgroud)
dev-build.js 加载配置,捆绑并启动BrowserSync.
/**
* Require Browsersync along with webpack and middleware for it
*/
var browserSync = require('browser-sync').create();
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
/**
* Require ./dev-webpack.config.js and make a bundler from it
*/
var webpackConfig = require('./dev-webpack.config');
var bundler = webpack(webpackConfig);
/**
* Reload all devices when bundle is complete
* or send a fullscreen error message to the browser instead
*/
bundler.plugin('done', function (stats) {
if (stats.hasErrors() || stats.hasWarnings()) {
return browserSync.sockets.emit('fullscreen:message', {
title: "Error",
timeout: 100000
});
}
browserSync.reload();
});
/**
* Run Browsersync and use middleware for Hot Module Replacement
*/
browserSync.init({
server: 'app',
open: false,
logFileChanges: false,
middleware: [
webpackDevMiddleware(bundler, {
publicPath: webpackConfig.output.publicPath,
stats: {colors: true}
})
],
plugins: ['bs-pretty-message'],
files: [
'app/css/*.css',
'app/*.html'
]
});
Run Code Online (Sandbox Code Playgroud)
dev-webpack.config.js Handle Typescript和scss.(我以为我应该有一个test-webpack.config.js)
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool: '#inline-source-map',
entry: [
'./src/main.ts',
'./src/main.scss'
],
output: {
path: path.join(__dirname, 'app'),
publicPath: '/',
filename: 'dist/bundle.js'
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.LoaderOptionsPlugin({
debug: true
})
],
resolve: {
extensions: ['.ts', '.js', '.scss']
},
module: {
rules: [
{
test: /\.ts$/, use: [{
loader: 'ts-loader'
}]
},
{
test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
use: [{
loader: 'url-loader'
}]
},
{
test: /\.scss$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader", options: {
sourceMap: true
}
}, {
loader: "sass-loader", options: {
sourceMap: true
}
}]
}
],
}
};
Run Code Online (Sandbox Code Playgroud)
我发现了什么:
https://dzone.com/articles/unit-testing-with-webpack-amp-mocha
Ngz的答案仍然包括棱角分明的东西和很多其他东西。因此,我将尽一切必要的答案。
test-webpack-config.js将测试作为入口点,因为webpack从那里寻找依赖项,而ts-loader进行了转译:
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool: '#inline-source-map',
entry: [
'./test/index.spec.ts',
],
output: {
filename: 'dist/bundle.js'
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.LoaderOptionsPlugin({
debug: true
})
],
resolve: {
extensions: ['.ts', '.js', '.tsx']
},
module: {
rules: [
{
test: /\.ts$/, use: [{
loader: 'ts-loader'
}]
}]
}
};
Run Code Online (Sandbox Code Playgroud)
我使用karma作为测试运行程序,带有karma-webpack插件,这是配置:
var webpackConfig = require('./test-webpack.config.js');
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: [
'test/*.spec.ts',
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/**/*.spec.ts': ['webpack'],
'src/**/*.ts': ['webpack'],
},
// 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: true,
// 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,
mime: {
'text/x-typescript': ['ts','tsx']
},
// Set Webpack configuration, but set the entry to spec files
webpack: {
module: webpackConfig.module,
resolve: webpackConfig.resolve
}
})
}
Run Code Online (Sandbox Code Playgroud)
MIME部分是使打字稿中的测试正常工作的关键。
对于完整的示例:https : //github.com/SierraNL/webpack-typescript-jasmine
| 归档时间: |
|
| 查看次数: |
5163 次 |
| 最近记录: |