【问题标题】:Angular not loading when running Karma tests (following a tutorial)运行 Karma 测试时未加载 Angular(按照教程)
【发布时间】:2021-04-12 17:07:52
【问题描述】:

我正在尝试了解如何在编写 Angular 应用程序时采用测试驱动开发方法;我一直在关注Introduction to Angular Test Driven Development 教程。

在我编写任何测试之前,我在第一关就失败了。

当我从tutorial/ 目录运行karma start 时,我收到错误:

Karma v 6.3.2 - 已连接; test: karma_error 在 afterAll Uncaught ReferenceError: angular is not defined ReferenceError: angular is not defined at http://localhost:9876/base/app/app.js

如何让 Angular 加载?

我目前的理解是angular 应该自动加载,因为karma.conf.jsapp.js 中的files: [] 行应该可以访问它。目前我的app.js 正在创建一个基本控制器,仅此而已,据我所知,我的代码与本阶段教程中的代码相匹配。教程有Git Hub repository

根据教程,我的目录结构如下:

tutorial/
|-app/
|---app.js
|-karma.conf.js
|-node_modules/ [Only listing relevant files]
|---angular/
|------angular.js
|---angular-mocks/
|------angular-mocks.js
|-package.json
|-tests/
|--[empty dir]

这是我的全部app/app.js

angular.module('ItemsApp', [])
    .controller('MainCtrl', function($scope) {
        $scope.title = 'Items App Title'
    });

这是我的karma.conf.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: [
      'app/**.js',
      'tests/**.js',
      'node_modules/angular/angular.js',
      'node_modules/angular-mocks/angular-mocks.js'
    ],


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


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // 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: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

【问题讨论】:

    标签: angular karma-runner


    【解决方案1】:

    本教程建议您在运行 karma init 时以错误的顺序提供应用程序的路径。

    由于app/app.js 依赖于node_modules/angular/angular.js,因此应先声明后者。要解决此问题,请更新 karma.conf.js 以声明:

        files: [
          'node_modules/angular/angular.js',
          'node_modules/angular-mocks/angular-mocks.js',
          'app/**.js',
          'tests/**.js'
        ],
    

    这是教程示例文件中显示的顺序,但不是我按照教程中的说明运行 karma init 时(在我的系统上)生成的顺序。

    【讨论】:

      猜你喜欢
      • 2017-08-23
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      相关资源
      最近更新 更多