JSHint 认为 Jasmine 函数未定义

时间:2023-02-15
本文介绍了JSHint 认为 Jasmine 函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

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

(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 设置任何全局变量,也没有 .jshintrc 文件,因为我在 Grunt 中配置它.我曾尝试将 Jasmine 的函数作为 JSHint 全局变量添加到我的 Gruntfile 中,但是将它们设置为 truefalse 并没有什么不同——错误仍然存在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 在我的规范文件中跳过对 Jasmine 函数的定义检查.

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 周围应该有".

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

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

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",
]

这篇关于JSHint 认为 Jasmine 函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 $timeout 和 Jasmine 的模拟时钟的单元测试 Angular 服务 下一篇:茉莉花测试失败,未定义不是函数(评估 $browser.$$checkUrlChange())

相关文章

最新文章