我已经广泛搜索了这个问题,但没有发现任何可以解决的问题。我正在尝试使用 webpack 运行单个 Jasmine 测试。我试图按照 Angular 网站上的教程进行操作,但它...不是真正正确或完整。
这是我到目前为止尝试过的:
- 已安装 Jasmine 类型,它们存在于我的 node_modules/@types/jasmine 中
- 将
"types": ["jasmine"]
添加到我的 tsconfig.json,即使您不必在 Typescript 2.0.X 中这样做 - 将
"typeRoots": ["./node_modules/@types"]
添加到我的 tsconfig.json,即使您不必在 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
最佳答案
呃...我找到了。正如我在帖子中所说,我使用的是 typescript 2.0.9,但最新版本的@types/jasmine 使用 keyof
,这是 typescript 2.1 的一个特性...:/
我将@types/jasmine 恢复到 2.5.41,一切正常。
关于angular - TS2304 : Cannot find name 'describe' in Webpack/Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42660699/