【问题标题】:TS2304: Cannot find name 'describe' in Webpack / Angular 2TS2304:在 Webpack / Angular 2 中找不到名称“描述”
【发布时间】: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


【解决方案1】:

呃……我找到了。正如我在帖子中所说,我使用的是 typescript 2.0.9,但最新版本的 @types/jasmine 使用的是 keyof,这是 typescript 2.1 的一个特性...:/

我将@types/jasmine 恢复到 2.5.41,一切都很好。

【讨论】:

  • Angular 是纸牌屋
【解决方案2】:

我使用以下命令安装了旧版本的 jasmine:

npm install --save-dev @types/jasmine@2.5.41

问题解决了。

【讨论】:

    猜你喜欢
    • 2019-06-11
    • 2016-12-25
    • 2019-04-21
    • 2017-06-27
    • 2017-01-17
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多