2014-09-29 41 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); 
    }); 
    }); 

})();