茉莉花测试返回错误“未捕获的引用错误:未定义要求”

Jasmine Test returns error "Uncaught ReferenceError: require is not defined"

本文关键字:错误 引用 未定义 返回 测试 茉莉花      更新时间:2023-09-26

首先,我知道这个问题在其他时候被问过,但它似乎有所不同。

这是我的业力配置文件:

// Karma configuration
// Generated on Thu Jan 21 2016 09:58:23 GMT-0300 (Argentina Standard Time)
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: [
      'src/*.js',
      'test/*.js',
      'src/*.js',
      'tests/*.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'],

    // 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
  })
}

这是我到目前为止的测试,以使其工作:

require('../src/person.js');
describe('Person ', function(){
    it('Should say hi with proposed first name and last name', function() {
        var person = new person();
        expect(true).toEqual(true);
    });
});

当我运行业力时,我收到错误说require is not defined。无论如何,karma-requirejs都是由npm全局安装的。

我正在阅读这个问题,似乎是同样的问题:茉莉花测试给出错误"未捕获的引用错误:未定义要求"

但是添加后

plugins : [
    'karma-requirejs',
],

到业力康普文件,我还有很多其他错误,似乎它不认识这个属性?

如何修复该错误?

我建议在项目的根目录中安装karma-requirejs,或者将所有其他npm模块安装在项目本地的位置。