javascript 业力中的茉莉花测试:未捕获的 ReferenceError:需要未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19117092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:26:06  来源:igfitidea点击:

jasmine tests in karma: Uncaught ReferenceError: require is not defined

javascriptkarma-runnerjasmine-node

提问by Igor Seliverstov

Karma can not recognize 'require' statement in JSFileSpec.js file. Running karma.conf.js:

Karma 无法识别 JSFileSpec.js 文件中的“require”语句。运行 karma.conf.js:

(function() {
    describe("DummyEmitter creation", function(){
        return it("creation", function(){
            var DummyEmitter = require('Util.DummyEmitter');
            var dummy = new DummyEmitter('someName');
            return expect(dummy).toBeDefined();
        });
    });
})();

ReferenceError: require is not defined

参考错误:需要未定义

回答by Sandeep Kumar

I was facing same issue, when trying to use require('module_name')(CommonJS style modules) inside a test case and running it using Karma.

当我尝试require('module_name')在测试用例中使用(CommonJS 样式模块)并使用 Karma 运行它时,我遇到了同样的问题。

The reason was requirefunction is not available to browser (it is undefined). To provide it to browser we can browserify the test js files before Karma runs test case in browser using karma-browserify.

原因是require功能对浏览器不可用(它是undefined)。为了将它提供给浏览器,我们可以在 Karma 使用karma-browserify在浏览器中运行测试用例之前浏览测试 js 文件。

Install karma-browserify using npm install karma-browserify --save-dev

安装 karma-browserify 使用 npm install karma-browserify --save-dev

Update karma.conf.js

更新karma.conf.js

 frameworks: ['jasmine', 'browserify'],
 preprocessors: {
    'app/tests/*.js': [ 'browserify' ]
 },
 plugins: [..., 'karma-browserify'],

After these changes browserified file is run in browser by Karma, in which requireis defined, and test case runs successfully

在这些更改之后,浏览器化文件由 Karma 在浏览器中运行,在其中require定义,并且测试用例运行成功

回答by Gamebreaker

You might be using a glob pattern that is picking up stuff inside karma's bin directory. Try to execute your tests by using absolute paths to see if that fixes it.

您可能正在使用 glob 模式,该模式正在 karma 的 bin 目录中获取内容。尝试通过使用绝对路径来执行您的测试,看看是否可以修复它。

If so then you know your glob pattern is grabbing stuff you did not want to.

如果是这样,那么您知道您的 glob 模式正在抓取您不想要的东西。

For example change

例如改变

{pattern: '**/**/*_test.js'},

to

{pattern: 'stuff/dashboard/home-page_test.js'},

see if that fixes your problem.

看看是否能解决您的问题。

回答by Dave

Karma is a test runner that runs your tests in a browser. Whatever browser you setup doesn't know what the require function is.

Karma 是一个测试运行,可以在浏览器中运行您的测试。您设置的任何浏览器都不知道 require 功能是什么。

To use jasmine with node try jasmine-node. https://github.com/mhevery/jasmine-node

要将 jasmine 与 node 一起使用,请尝试 jasmine-node。https://github.com/mheevery/jasmine-node

To have karma run jasmine node tests, try (wait for it....) jasmine-node-karma. https://npmjs.org/package/jasmine-node-karma

要让 karma 运行 jasmine 节点测试,请尝试(等待它....)jasmine-node-karma。https://npmjs.org/package/jasmine-node-karma

Here's the jasmine wiki pages where I found the above info. https://github.com/pivotal/jasmine/wiki

这是我找到上述信息的 jasmine wiki 页面。 https://github.com/pivotal/jasmine/wiki

Hope this helps.

希望这可以帮助。

回答by Carlo Rizzante

I've encountered today a similar issue. In my case the solution was quite simple. I am using Babel via Webpack to process .jsx files. Files with the .jsx extension did test successfully, while simple .js files throwed a reference error.

我今天遇到了类似的问题。就我而言,解决方案非常简单。我通过 Webpack 使用 Babel 来处理 .jsx 文件。带有 .jsx 扩展名的文件确实测试成功,而简单的 .js 文件引发了引用错误。

If anyone has a similar or equivalent setup they might be able to share the same solution.

如果有人有类似或等效的设置,他们可能能够共享相同的解决方案。

In karma.config.js I had to specify preprocessors for .js files as I did for the .jsx ones. Here's an example:

在 karma.config.js 中,我必须为 .js 文件指定预处理器,就像我为 .jsx 文件所做的那样。下面是一个例子:

preprocessors: {
  "app/tests/**/*.test.jsx": ["webpack", "sourcemap"],
  "app/tests/**/*.test.js": ["webpack", "sourcemap"]
},

I better add that in my case Webpack passes the code to Babel to compile so that it can run in the browser. I can copy and paste the entire webpack.config.js and karma.config.js if anyone needs them.

我最好补充一点,在我的情况下,Webpack 将代码传递给 Babel 进行编译,以便它可以在浏览器中运行。如果有人需要,我可以复制并粘贴整个 webpack.config.js 和 karma.config.js。

回答by Pawe?

I use webpack for that purpose. I published my configuration on npm to save my time for the future projects. Just run npm install webpack-karma-jasmine, and create config files for webpack and karma as described in docs: https://www.npmjs.com/package/webpack-karma-jasmine

我为此目的使用 webpack。我在 npm 上发布了我的配置,为以后的项目节省了时间。只需运行npm install webpack-karma-jasmine,并按照文档中的描述为 webpack 和 karma 创建配置文件:https://www.npmjs.com/package/webpack-karma-jasmine

回答by freedom

  module.exports = function(config) {
    config.set({

      basePath: '',


      frameworks: ['mocha', 'chai'],

      files: [
        'test/*.test.js'
      ],

      exclude: [
      ],

      preprocessors: {
        'test/*.test.js': ['webpack']
      },

      webpack: {
        mode: "none",
        module: {
          rules: [
            { test: /\.js?$/, loader: "babel-loader",  options: { presets: ["env"] }, }
          ]
        }
      },

      reporters: ['progress'],

      port: 9876,

      colors: true,

      logLevel: config.LOG_INFO,

      autoWatch: true,

      browsers: ['Chrome'],

      singleRun: false,

      concurrency: Infinity,

      browserNoActivityTimeout: 100000
    })
  }

use webpack

使用 webpack

回答by klaks

If someone still couldnt resolve with the above solutions, this is what helped me. yarn add browserify and watchify

如果有人仍然无法解决上述解决方案,这就是帮助我的原因。纱线添加浏览器和监视