【发布时间】:2023-04-04 16:47:01
【问题描述】:
我已经广泛搜索了这个问题,但没有找到任何解决方法。我正在尝试使用 webpack 运行单个 jasmine 测试。我尝试按照 Angular 网站上的教程进行操作,但它……并不正确或不完整。
这是我迄今为止尝试过的:
- 已安装 Jasmine 类型,它们存在于我的 node_modules/@types/jasmine 中
- 在我的 tsconfig.json 中添加了
"types": ["jasmine"],即使您不必在 Typescript 2.0.X 中这样做 - 在我的 tsconfig.json 中添加了
"typeRoots": ["./node_modules/@types"],即使您不必在 Typescript 2.0.X 中这样做 - 在我的单元测试中添加了
import {} from 'jasmine';,如下所述:Angular 2 Unit Tests: Cannot find name 'describe'
那时,我感觉自己离基地很远,这里肯定发生了其他事情。我正在使用 awesome-typescript-loader,也许它无法识别类型?
我使用的是2.0.9版本的typescript,我的配置文件如下:
webpack.test.ts
var webpack = require('webpack');
var helpers = require('./helpers');
module.exports = {
devtool: 'inline-source-map',
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
loaders: [
{
loader: 'awesome-typescript-loader',
options: { configFileName: helpers.root('tsconfig.json') }
} , 'angular2-template-loader'
]
},
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'null-loader'
},
{
test: /\.css$/,
exclude: helpers.root('src', 'app'),
loader: 'null-loader'
},
{
test: /\.css$/,
include: helpers.root('src', 'app'),
loader: 'raw-loader'
}
]
},
plugins: [
new webpack.ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
helpers.root('./src'), // location of your src
{} // a map of your routes
)
]
}
tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2015",
"dom"
],
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"target": "es5"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
编辑:我用来运行测试的命令:karma start ./config/karma.conf.js --single-run
【问题讨论】:
-
您使用哪个命令来运行测试?
-
好问题,更新了问题,它是:karma start ./config/karma.conf.js --single-run
标签: angular typescript webpack