javascript JSHint 认为 Jasmine 函数是未定义的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26091744/
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-28 05:29:18  来源:igfitidea点击:

JSHint thinks Jasmine functions are undefined

javascriptgruntjsjasminekarma-runnerjshint

提问by Ian128K

I've got a Grunt setup which uses Karma+Jasmine and JSHint. Whenever I run JSHint on my spec file, I get a series of "undefined" errors, most of which are for Jasmine's built-in functions. For example:

我有一个使用 Karma+Jasmine 和 JSHint 的 Grunt 设置。每当我在我的规范文件上运行 JSHint 时,我都会收到一系列“未定义”错误,其中大部分是针对 Jasmine 的内置函数。例如:

Running "jshint:test" (jshint) task

   js/main.spec.js
      3 |describe("loadMatrix()", function() {
         ^ 'describe' is not defined.
      4 |    it("should not assign a value if no arg is passed.", function() {
             ^ 'it' is not defined.

(I also get some undefined errors for the variables and functions from the JS file that my spec is meant to test against, but I'm not sure why that is and it may be a separate issue.)

(对于我的规范要测试的 JS 文件中的变量和函数,我也得到了一些未定义的错误,但我不确定为什么会这样,这可能是一个单独的问题。)

My Karma config file has frameworks: [ "jasmine" ]in it, I don't have any globals set for JSHint, and I don't have a .jshintrcfile since I'm configuring it in Grunt. I did try adding Jasmine's functions as JSHint globals in my Gruntfile at one point, but setting them as either trueor falsedidn't make a difference—the errors still persisted when JSHint ran.

我的 Karma 配置文件frameworks: [ "jasmine" ]在里面,我没有为 JSHint 设置任何全局变量,而且我没有.jshintrc文件,因为我是在 Grunt 中配置它的。我曾尝试将 Jasmine 的函数作为 JSHint 全局变量添加到我的 Gruntfile 中,但将它们设置为truefalse没有任何区别 - 当 JSHint 运行时错误仍然存​​在。

What am I missing? I can't seem to do anything to get JSHint to skip definition checking for Jasmine's functions in my spec file.

我错过了什么?我似乎无法让 JSHint 在我的规范文件中跳过对 Jasmine 函数的定义检查。

采纳答案by Ian128K

MINOR CORRECTION - there should be "" around predef in the .jshintrc file.

小更正 - .jshintrc 文件中的 predef 周围应该有“”。

Fixed by adding this to the jshintoptions in my Gruntfile.coffee:

通过将其添加到jshintmy 中的选项来修复Gruntfile.coffee

predef: [
    "jasmine"
    "describe"
    "xdescribe"
    "before"
    "beforeEach"
    "after"
    "afterEach"
    "it"
    "xit"
    "it"
    "inject"
    "expect"
    "spyOn"
]

.jshintrc:

.jshintrc

"predef": [
    "jasmine",
    "describe",
    "xdescribe",
    "before",
    "beforeEach",
    "after",
    "afterEach",
    "it",
    "xit",
    "it",
    "inject",
    "expect",
    "spyOn",
]

回答by Leon Fedotov

You can just add "jasmine": trueto your .jshintrcfile.

您可以添加"jasmine": true到您的.jshintrc文件中。

回答by GOTO 0

I fixed this in Gruntfile.js adding jasmine: trueto the options of the jshint task:

我在 Gruntfile.js 中修复了这个问题,添加jasmine: true到 jshint 任务的选项中:

jshint:
{
    options:
    {
        ...
        node: true,
        jasmine: true,
        ...
    },
    ...
},

Like the OP, I'm not using a .jshintrc file either.

像 OP 一样,我也没有使用 .jshintrc 文件。

回答by Petur Subev

I believe the other answers are correct, but I have never seen such exception before, however I see it now. Then I noticed that my tests are not in IIFE. So I moved them in IIFE like this and I no longer get such JSHINT warnings.

我相信其他答案是正确的,但我以前从未见过这样的例外,但我现在看到了。然后我注意到我的测试不在 IIFE 中。所以我像这样在 IIFE 中移动它们,我不再收到这样的 JSHINT 警告。

(function () {

  describe('foo', () => {
     it('bar', () => {
        expect(1+1).toEqual(2);
     });
  });

})();