問題描述
我有一個使用 Karma+Jasmine 和 JSHint 的 Grunt 設(shè)置.每當(dāng)我在我的規(guī)范文件上運行 JSHint 時,我都會收到一系列未定義"錯誤,其中大部分是 Jasmine 的內(nèi)置函數(shù).例如:
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:
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.
(我的規(guī)范要測試的 JS 文件中的變量和函數(shù)也有一些未定義的錯誤,但我不確定這是為什么,這可能是一個單獨的問題.)
(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.)
我的 Karma 配置文件中有 frameworks: [ "jasmine" ]
,我沒有為 JSHint 設(shè)置任何全局變量,也沒有 .jshintrc
文件,因為我在 Grunt 中配置它.我曾嘗試將 Jasmine 的函數(shù)作為 JSHint 全局變量添加到我的 Gruntfile 中,但是將它們設(shè)置為 true
或 false
并沒有什么不同——錯誤仍然存??在JSHint 跑了.
My Karma config file has frameworks: [ "jasmine" ]
in it, I don't have any globals set for JSHint, and I don't have a .jshintrc
file 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 true
or false
didn't make a difference—the errors still persisted when JSHint ran.
我錯過了什么?我似乎無法讓 JSHint 在我的規(guī)范文件中跳過對 Jasmine 函數(shù)的定義檢查.
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.
推薦答案
MINOR CORRECTION - .jshintrc 文件中的 predef 周圍應(yīng)該有".
MINOR CORRECTION - there should be "" around predef in the .jshintrc file.
通過將其添加到我的 Gruntfile.coffee
中的 jshint
選項來修復(fù):
Fixed by adding this to the jshint
options in my Gruntfile.coffee
:
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",
]
這篇關(guān)于JSHint 認為 Jasmine 函數(shù)未定義的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!