【问题标题】:How do I fix my generator-angular project so that grunt test works?如何修复我的生成器角度项目,以便 grunt 测试工作?
【发布时间】:2023-03-06 00:20:01
【问题描述】:

我正在学习本教程:http://www.sitepoint.com/kickstart-your-angularjs-development-with-yeoman-grunt-and-bower/ 作为了解使用 yo generator-angular 创建的文件的一种方式。

我有使用 AngularJS 的经验,但正在寻找一种方法来设置最佳实践目录;我不确定如何设置依赖项并让 karma 自己运行,因此使用 yeoman 生成器。

但是,直接开箱即用,无需编辑任何其他内容,当我运行 grunt test 时,我得到以下信息:

running "clean:server" (clean) task
Cleaning .tmp...OK

Running "concurrent:test" (concurrent) task

Running "copy:styles" (copy) task
Copied 1 files

Done, without errors

Running "autoprefixer:dist" (autoprefixer) task
Prefixed file ".tmp/styles/main.css" created.

Running "connect:test" (connect) task
Started connect web server on 127.0..0.1:9001.

Running "karma:unit" (karma) task
Warning: No provider for "framework:jasmine"! (resolving: framework:jasmine) Use --force to continue.

Aborted due to warnings.

我不明白为什么 jasmine 没有提供程序,也不知道如何解决这个问题。是修复我的 package.json 文件和更新节点的问题吗?

编辑:这是配置文件:

// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html

module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks:['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'app/bower_components/angular/angular.js',
      'app/bower_components/angular-mocks/angular-mocks.js',
      'app/bower_components/angular-resource/angular-resource.js',
      'app/scripts/*.js',
      'app/scripts/**/*.js',
      'test/mock/**/*.js',
      'test/spec/**/*.js'
    ],

    // list of files / patterns to exclude
    exclude: [],

    // web server port
    port: 8080,

    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });

};

【问题讨论】:

  • 您能发布一个配置示例吗? Here 是我的 express.js/angular.js/bootstrap 启动项目的配置,如果你想比较配置的话。
  • 我相信我已经解决了这个问题,见下文

标签: angularjs gruntjs jasmine yeoman karma-runner


【解决方案1】:

对于任何有类似问题的人,我似乎已经解决了我的问题:

在我的 karma.conf.js 中,我添加了以下内容:

plugins: [
    'karma-chrome-launcher',
    'karma-jasmine'
    ],

一开始我添加了“karma-jasmine”,但后来遇到“无法加载“Chrome”,它没有注册!”通过添加“karma-chrome-launcher”作为插件解决了这个问题

不确定是我的错还是 generator-angular 是否过时,但它现在可以工作了。

【讨论】:

  • 这是非常有用的信息。我在 generator-angular 上找到了 this issue,在 generator-karma 上找到了 this issue,它们似乎与您所看到的有关。
  • @JimSchubert 今天也遇到了同样的问题
  • 大量问题需要解决,但由于社区对 SOF 的支持,解决起来很容易。谢谢!非常小的一点——默认名称是“karma.conf.js”,而不是“karma.config.js”。干杯。
【解决方案2】:

如果您在yo angular 命令上使用了--coffee 标志,您需要:

安装一些浏览器启动器(在本例中为 chrome)和以下 npm 包:

npm install --save-dev karma-chrome-launcher karma-jasmine karma-coffee-preprocessor

并将以下内容添加到您的 karma.conf.js 文件中:

plugins:['karma-chrome-launcher', 'karma-jasmine', 'karma-coffee-preprocessor'],
preprocessors:{'**/*.coffee':['coffee']}

之后运行grunt test 应该可以工作了。

【讨论】:

    【解决方案3】:

    @Jim Schubert 指出的 github issue 有这样的评论:

    我需要安装的业力依赖项的完整列表 测试工作(咖啡脚本)。

    npm install --save-dev karma-chrome-launcher karma-firefox-launcher karma-safari-launcher karma-opera-launcher karma-ie-launcher karma-jasmine karma-coffee-preprocessor
    

    【讨论】:

    • 更糟糕的是,我预计我在引用的业力 ~0.10.2 和所有其他业力部门的波浪号版本...版本 0.10.10 及其所有依赖项不知何故不同步并导致很多我的团队的错误。
    猜你喜欢
    • 2014-04-26
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多