【问题标题】:Uncaught ReferenceError: require is not defined on karma start karma.conf.js未捕获的 ReferenceError:在 karma start karma.conf.js 上未定义要求
【发布时间】:2016-07-25 17:07:15
【问题描述】:

使用 Karma 和 Jasmine 在 rails 应用的 Angular 前端进行单元测试。看来我已经做了人类已知的所有事情来解决这个错误,并且我的 package.json 中有一百万个依赖项。这是我的 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

// list of files / patterns to load in the browser
files: [
   //angular mocks
  'bower_components/angular/angular.js',
  'bower_components/angular-mocs/angular-mocks.js',
  'bower_components/angular-resource/angular-resource.js',

  //load modules
  'public/app/app.js',

  //test file locations
  'app/**/*.js',
  'spec/**/*.js',
  'public/**/*.js'

],

// list of files 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', 'Firefox'],


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

plugins : [
  'karma-requirejs',
  'karma-jasmine',
  'karma-chrome-launcher',
  'karma-firefox-launcher',
  'karma-browserify'
],

frameworks: ['jasmine', 'browserify']

  })
}

这里有什么明显的我做错了吗?我希望有,看起来我已经在这里实现了几个解决方案,但并不完全知道发生了什么。谢谢!

我的 app.js 文件的第一行出现错误,带有“require”

【问题讨论】:

  • 你在 app.js 中使用了 require,你有像 webpack 这样的模块加载器吗?
  • 我已尝试回答您的问题。看看吧。

标签: angularjs browserify karma-jasmine


【解决方案1】:

浏览器不理解要求,因此您需要在将文件提供给浏览器之前对其进行预处理。您可以将 webpack 配置到 karma.config 中,以便 karma 可以在测试之前使用 webpack 预处理您的文件。你还需要安装 karma webpack,

npm i --save-dev karma-webpack

有很多方法可以做到这一点,我就是这样做的。

var path = require('path');
var webpackConfig = require('./webpack.config');
var entry = path.resolve(webpackConfig.context, webpackConfig.entry);
var preprocessors = {};
preprocessors[entry] = ['webpack'];
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: ['chai','mocha'],


    // list of files / patterns to load in the browser
    files: [entry],
    webpack:webpackConfig,

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


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: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 simultanous
    concurrency: Infinity,
    plugins:[
      require('karma-webpack'),
      ('karma-chai'),
      ('karma-mocha'),
      ('karma-chrome-launcher')
    ]
  })
}

here is a seed i worked on with karma, webpack, angularjs.

看看,祝你好运。

【讨论】:

  • 谢谢!我必须进行一些配置,但这让我走上了正确的道路
  • 使用此解决方案我收到以下错误。 [14:30:48] Using gulpfile ~/myProject/app/gulpfile.js [14:30:48] Starting 'test'... 19 06 2017 14:30:48.935:ERROR [config]: Invalid config file! TypeError: Path must be a string. 在这条线上var entry = path.resolve(webpackConfig.context, webpackConfig.entry);。有什么想法吗?
【解决方案2】:

如果您使用 require 加载模块,请按照本指南设置 Karma:

https://karma-runner.github.io/0.8/plus/RequireJS.html

或者如果你使用 browserify 进行捆绑,试试这个:

https://github.com/nikku/karma-browserify

你可能还想看看这个问题:How to test browserify project using karma/jasmine

【讨论】:

  • 我尝试了这两个,但无法解决错误,webpack 最终成为了解决方案
猜你喜欢
  • 1970-01-01
  • 2015-09-03
  • 2017-06-01
  • 2020-05-16
  • 2012-12-16
  • 2017-02-28
  • 2018-01-30
  • 2019-09-28
相关资源
最近更新 更多