2014-09-29 74 views
29

我有一个使用Karma + Jasmine和JSHint的Grunt设置。每当我在我的spec文件上运行JSHint时,我会遇到一系列“未定义”的错误,其中大部分是针对Jasmine的内置函数的。例如:JSHint认为茉莉花功能不明

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. 

(我也得到了来自我的规格是为了测试对JS文件的变量和函数的一些不确定的错误,但我不知道这是为什么,它可以是单独的问题。)

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

我错过了什么?我似乎无法做任何事情让JSHint跳过定义检查我的spec文件中Jasmine的函数。

+0

你使用什么版本的噶玛? – 2014-09-29 06:53:26

+0

业力0.12.23,业力茉莉花0.2.0。 – Ian128K 2014-09-30 02:13:13

回答

22

MINOR CORRECTION - 在.jshintrc文件中应该有“”前缀。

固定在我的Gruntfile.coffee添加此向jshint选项:

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

.jshintrc

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

[来自Leon的回答](http://stackoverflow.com/a/27136840/3116322)应该是被接受的答案,因为它只需定义''茉莉花'就够短了:true' – Ande 2016-03-11 08:26:12

65

您可以再补充"jasmine": true.jshintrc文件。

+0

https://github.com/ SBT/SBT-jshint /问题/ 13 – 2016-08-25 06:13:06

9

我Gruntfile.js添加jasmine: true到jshint任务的选项固定这样的:

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

像OP,我没有使用.jshintrc文件要么。

0

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

(function() { 

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

})();