阅读背景:

Cloud9中Mocha测试中的“未声明的变量”警告

来源:互联网 

I'm new at node.js and the framework Mocha for unit testing, but I've created a couple of tests in cloud9 IDE just to see how it works. The code looks like this:

我是node.js和用于单元测试的框架Mocha的新手,但我在cloud9 IDE中创建了几个测试,看看它是如何工作的。代码如下所示:

var assert = require("assert");
require("should");

describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    });
  });
});

describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return the index when the value is present', function(){
      assert.equal(1, [1,2,3].indexOf(2));
      assert.equal(0, [1,2,3].indexOf(1));
      assert.equal(2, [1,2,3].indexOf(3));
    });
  });
});

The tests work if I type mocha in the console, but the IDE shows warnings in the lines where "describe" and "it" are because it says that the variable has not been declared ("undeclared variable").

如果我在控制台中键入mocha,则测试会起作用,但IDE会在“describe”和“it”的行中显示警告,因为它表示该变量尚未声明(“未声明的变量”)。

I wonder what should I do these tests to avoid the warnings.

我想知道我应该怎么做这些测试以避免警告。

Thanks.

2 个解决方案

#1


2  

In cloud9 you can add a hint for globals as a comment at the top of the file and it will remove the warnings. e.g.

在cloud9中,您可以在文件顶部为全局变量添加提示作为注释,它将删除警告。例如

**/* global describe it before */**

var expect = require('chai').expect;


describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
        expect(true).to.equal(true);
    })
  })
})

#2


0  

That's because mocha "executable" wraps your test in requires needed to use mocha functions (describe, and it). Take a look at mocha and _mocha in your node_modules/mocha/bin directory.

这是因为mocha“executable”包含了你需要使用mocha函数(describe和it)所需的测试。在node_modules / mocha / bin目录中查看mocha和_mocha。

On the other hand cloud9 tries to resolve all symbols using a pure node executable, so you have to require everything by hand.

另一方面,cloud9尝试使用纯节点可执行文件解析所有符号,因此您必须手动需要所有符号。


分享到: