未捕获的参考错误:需要没有定义。

时间:2022-06-09 08:00:52

For a project that I am working on I have been using a hodgepodge of JavaScript libraries. The main logic of my code is broken down into multiple commonjs modules. I use google closure to combine the modules into one output js file which I use within my AngularJS application.

对于我正在开发的一个项目,我使用了大量的JavaScript库。我的代码的主要逻辑被分解为多个commonjs模块。我使用谷歌闭包将模块合并到一个输出js文件中,我在AngularJS应用程序中使用这个文件。

The problem I am having is trying to perform tests with testacular. There error I receive is Uncaught ReferenceError: require is not defined. It is happening because, unlike google closure, testacular doesn't understand commonjs modules. There are a couple work arounds I can do, but I was hoping to make it work without having to restructure my code.

我遇到的问题是尝试用testacular进行测试。我收到的错误是未捕获的ReferenceError: require没有定义。之所以会发生这种情况,是因为testacular与谷歌闭包不同,它并不理解commonjs模块。我可以做一些工作,但我希望它可以工作,而不必重构代码。

  1. I can restucture the modules so that I'm no longer using commonjs. I don't like this because it feels like a step backwards. I want my code to be modular.
  2. 我可以重新构建模块,这样我就不再使用commonjs了。我不喜欢这个,因为感觉像是倒退了一步。我希望代码是模块化的。
  3. I can run testacular on the compiled js from google closure. I don't mind doing it this way, but I have not been able to trigger everything to work on file changes. Testacular can re-run itself on file changes, but I haven't seen anyway to make google closure re-compile on changes.
  4. 我可以在谷歌闭包编译的js上运行testacular。我不介意这样做,但是我还没能触发所有文件修改。Testacular可以在文件修改后重新运行,但是我还没有看到谷歌闭包在修改后重新编译。
  5. Finally, I can enable commonjs module in testacular. Ideally this is the way I want to go, but it may not be the easiest.
  6. 最后,我可以在testacular中启用commonjs模块。理想情况下,这是我想要的方式,但它可能不是最容易的。

Has anyone else run into a similar problem? I'm open for trying different things; I just don't want anything hacky.

还有人遇到过类似的问题吗?我愿意尝试不同的东西;我只是不想要任何陈腐的东西。

javaclassstreamreader.spec.js:

javaclassstreamreader.spec.js:

"use strict"

var JavaClassStreamReader = require('../javaclassstreamreader.js').JavaClassStreamReader;

describe('javaclassstreamreader', function() {

  it('reader can be constructed', function() {
    var dataView = {
      byteLength : 0
    };
    //FIXME load dataView

    var reader = new JavaClassStreamReader(dataView);
    expect(reader.dataView).toBe(dataView);
    expect(reader.offset).toBe(0);
    expect(reader.maxOffset).toBe(0);
  });

});

javaclassstreamreader.js:

javaclassstreamreader.js:

function JavaClassStreamReader(dataView, initialOffset, maxBytesToRead) {
  this.dataView = dataView;
  this.offset = initialOffset || 0;
  this.maxOffset = this.offset + (maxBytesToRead || this.dataView.byteLength);
}
//... code trucated ...

2 个解决方案

#1


2  

It seems there is/was an issue with Testacular.

这似乎是一个与Testacular有关的问题。

Could you try the following:

你能试试下面的方法吗?

  • clear npm cache: npm cache clean
  • 清除npm缓存:npm缓存清理
  • install another version of testacular: npm install -g testacular@0.5.6
  • 安装testacular的另一个版本:npm安装-g testacular@0.5.6

#2


1  

I was not able to make it work with require, but I do have a partial solution.

我无法使它与require一起工作,但是我有一个部分的解决方案。

grunt.js:

grunt.js:

/*global module:false*/
module.exports = function(grunt) {"use strict";

  // Project configuration.
  grunt.initConfig({
    pkg : '<json:package.json>',
    meta : {
      banner : '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd") %>\n' + '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' + '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' + ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
    },
    lint : {
      files : ['grunt.js', 'src/*.js', 'src/public/js/**/*.js', 'src/specs/**/*.js']
    },
    watch : {
      files : '<config:lint.files>',
      tasks : 'default'
    },
    exec : {
      ensure_generated_directory : {
        command : 'mkdir -p generated/js/'
      }
    },
    clean : {
      all : ['generated']
    },
    jshint : {
      files : '<config:lint.files>',
      options : {
        curly : true,
        eqeqeq : true,
        forin : true,
        immed : true,
        latedef : true,
        newcap : true,
        noarg : true,
        sub : true,
        undef : true,
        unused : true,
        strict : true,
        boss : true,
        eqnull : true,
        es5 : true,
        browser : true,
        jquery : true,
        devel : true
      },
      globals : {
        //jasmine
        describe : false,
        it : false,
        expect : false,
        //commonjs
        require : false,
        exports : true,
        //angular
        angular : false
      }
    },
    'closure-compiler' : {
      frontend : {
        closurePath : 'closure-compiler',
        js : ['src/*.js', 'src/public/js/**/*.js'],
        jsOutputFile : 'generated/js/complete-app.js',
        options : {
          externs : 'externs.js',
          compilation_level : 'SIMPLE_OPTIMIZATIONS',
          language_in : 'ECMASCRIPT5_STRICT',
          logging_level : 'ALL',
          debug : null,
          warning_level : 'verbose',
          summary_detail_level : 3,
          formatting : ['PRETTY_PRINT', 'PRINT_INPUT_DELIMITER'],
          common_js_entry_module : 'src/public/js/app.js',
          process_common_js_modules : null,
          process_jquery_primitives : null,
          common_js_module_path_prefix : 'src'
        }
      }
    },
    testacularServer : {
      integration : {
        options : {
          keepalive : true
        },
        configFile : 'testacular.conf.js',
        autoWatch : false,
        singleRun : true
      }
    }
  }); 

  // Default task.
  grunt.registerTask('default', 'lint exec:ensure_generated_directory closure-compiler testacularServer:integration');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-closure-compiler');
  grunt.loadNpmTasks('grunt-exec');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-testacular');
};

I can run grunt watch and I get a similar result. grunt lints, then compiles, then runs testacular. This isn't as fast as I was hoping. testacular starts and stops the server each time.

我可以运行咕噜手表,我得到了类似的结果。咕噜lints,然后编译,然后运行testacular。这没有我希望的那么快。testacular每次启动和停止服务器。

#1


2  

It seems there is/was an issue with Testacular.

这似乎是一个与Testacular有关的问题。

Could you try the following:

你能试试下面的方法吗?

  • clear npm cache: npm cache clean
  • 清除npm缓存:npm缓存清理
  • install another version of testacular: npm install -g testacular@0.5.6
  • 安装testacular的另一个版本:npm安装-g testacular@0.5.6

#2


1  

I was not able to make it work with require, but I do have a partial solution.

我无法使它与require一起工作,但是我有一个部分的解决方案。

grunt.js:

grunt.js:

/*global module:false*/
module.exports = function(grunt) {"use strict";

  // Project configuration.
  grunt.initConfig({
    pkg : '<json:package.json>',
    meta : {
      banner : '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd") %>\n' + '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' + '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' + ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
    },
    lint : {
      files : ['grunt.js', 'src/*.js', 'src/public/js/**/*.js', 'src/specs/**/*.js']
    },
    watch : {
      files : '<config:lint.files>',
      tasks : 'default'
    },
    exec : {
      ensure_generated_directory : {
        command : 'mkdir -p generated/js/'
      }
    },
    clean : {
      all : ['generated']
    },
    jshint : {
      files : '<config:lint.files>',
      options : {
        curly : true,
        eqeqeq : true,
        forin : true,
        immed : true,
        latedef : true,
        newcap : true,
        noarg : true,
        sub : true,
        undef : true,
        unused : true,
        strict : true,
        boss : true,
        eqnull : true,
        es5 : true,
        browser : true,
        jquery : true,
        devel : true
      },
      globals : {
        //jasmine
        describe : false,
        it : false,
        expect : false,
        //commonjs
        require : false,
        exports : true,
        //angular
        angular : false
      }
    },
    'closure-compiler' : {
      frontend : {
        closurePath : 'closure-compiler',
        js : ['src/*.js', 'src/public/js/**/*.js'],
        jsOutputFile : 'generated/js/complete-app.js',
        options : {
          externs : 'externs.js',
          compilation_level : 'SIMPLE_OPTIMIZATIONS',
          language_in : 'ECMASCRIPT5_STRICT',
          logging_level : 'ALL',
          debug : null,
          warning_level : 'verbose',
          summary_detail_level : 3,
          formatting : ['PRETTY_PRINT', 'PRINT_INPUT_DELIMITER'],
          common_js_entry_module : 'src/public/js/app.js',
          process_common_js_modules : null,
          process_jquery_primitives : null,
          common_js_module_path_prefix : 'src'
        }
      }
    },
    testacularServer : {
      integration : {
        options : {
          keepalive : true
        },
        configFile : 'testacular.conf.js',
        autoWatch : false,
        singleRun : true
      }
    }
  }); 

  // Default task.
  grunt.registerTask('default', 'lint exec:ensure_generated_directory closure-compiler testacularServer:integration');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-closure-compiler');
  grunt.loadNpmTasks('grunt-exec');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-testacular');
};

I can run grunt watch and I get a similar result. grunt lints, then compiles, then runs testacular. This isn't as fast as I was hoping. testacular starts and stops the server each time.

我可以运行咕噜手表,我得到了类似的结果。咕噜lints,然后编译,然后运行testacular。这没有我希望的那么快。testacular每次启动和停止服务器。