标签: mocha.js

Mocha API测试:获取'TypeError:app.address不是函数'

我的问题

我编写了一个非常简单的CRUD API,我最近开始编写一些使用的测试chai,chai-http但是在运行我的测试时遇到了问题$ mocha.

当我运行测试时,我在shell上收到以下错误:

TypeError: app.address is not a function

我的守则

以下是我的一个测试(/tests/server-test.js)的示例:

var chai = require('chai');
var mongoose = require('mongoose');
var chaiHttp = require('chai-http');
var server = require('../server/app'); // my express app
var should = chai.should();
var testUtils = require('./test-utils');

chai.use(chaiHttp);

describe('API Tests', function() {
  before(function() {
    mongoose.createConnection('mongodb://localhost/bot-test', myOptionsObj);
  });

  beforeEach(function(done) {
    // I do stuff like populating db
  });

  afterEach(function(done) {
    // I do stuff like deleting populated db
  }); …
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js node.js express chai

88
推荐指数
6
解决办法
5万
查看次数

运行mocha测试时导入Babel意外的令牌

其他相关问题中提供的解决方案,例如在.babelrc中包含正确的预设(es2015),已在我的项目中实现.

我有两个项目(让我们称之为A和B),它们都使用ES6模块语法.在项目A中,我正在导入通过npm安装的项目B,它位于node_modules文件夹中.当我为项目A运行我的测试套件时,我收到错误:

SyntaxError:意外的令牌导入

之前是项目B的这个错误的代码行:

(函数(exports,require,module,__ filename,__ dirname){从'history/lib/createBrowserHistory'导入createBrowserHistory;

因为我的源文件只包含"来自'history/lib/createBrowserHistory'的import createBrowserHistory',所以iife似乎是npm或者babel相关的东西; Project B的测试套件中的单元测试运行正常,如果我将Project B作为依赖项从项目A,我的测试套件(仍然使用es6导入内部项目模块)工作正常.

完整堆栈跟踪:

 SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Module._extensions..js (module.js:405:10)
    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (actionCreators.js:4:17)
    at Module._compile (module.js:398:26)
    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/ProjectA/src/components/core/wrapper/wrapper.js:28:23)
    at Module._compile (module.js:398:26)
    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5) …
Run Code Online (Sandbox Code Playgroud)

syntax-error mocha.js node.js npm babeljs

87
推荐指数
8
解决办法
7万
查看次数

如何使用Karma(testacular)测试nodejs后端代码

如何设置Karma来运行我的后端单元测试(用Mocha编写)?如果我将后端测试脚本添加到files = [],则说明require未定义.

mocha.js node.js karma-runner

86
推荐指数
2
解决办法
4万
查看次数

mocha before()中的异步函数总是在它()规范之前完成?

我有一个回调函数,before()用于清理数据库.一切都before()保证在it()开始之前完成吗?

before(function(){
   db.collection('user').remove({}, function(res){}); // is it guaranteed to finish before it()? 
});

it('test spec', function(done){
  // do the test
});

after(function(){
});
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous mocha.js

85
推荐指数
2
解决办法
5万
查看次数

使用Mocha进行Javascript测试时assert.equal和assert.deepEqual之间的区别?

我正在使用Mocha来测试Express.js应用程序中的一个小模块.在这个模块中,我的一个函数返回一个数组.我想测试数组对于给定输入是否正确.我是这样做的:

suite('getWords', function(){
    test("getWords should return list of numbers", function() {
        var result = ['555', '867', '5309'];
        assert.equal(result, getWords('555-867-5309'));
    });
});
Run Code Online (Sandbox Code Playgroud)

当它运行时,我得到以下断言错误:

AssertionError: ["555","867","5309"] == ["555","867","5309"]
Run Code Online (Sandbox Code Playgroud)

但是,当我将测试更改为a时assert.deepEqual,测试通过正常.我想知道这是否是==vs 的情况===,但如果我进入

[1,2,3] === [1,2,3]
Run Code Online (Sandbox Code Playgroud)

进入node.js命令行,我仍然得到假.

为什么数组不像其他值那样比较(例如1 == 1)?和assert.equal和assert.deepEqual有什么区别?

javascript arrays testing mocha.js

82
推荐指数
1
解决办法
5万
查看次数

你如何安装和运行Node.js测试模块Mocha?安装后获取"mocha:command not found"

我很难让Mocha像预期的那样工作,我很乐意按照文档记录说,但是(似乎)没有太多关于实际运行的文档.

我已经使用它npm(全局和本地)安装它,每次运行时我得到:

$ mocha
mocha: command not found
Run Code Online (Sandbox Code Playgroud)

好的,所以我认为它不在我的身上PATH,所以我试着直接运行它,

$ ./node_modules/mocha/bin/mocha 
execvp(): No such file or directory
Run Code Online (Sandbox Code Playgroud)

最后,我试着点击另一个bin文件,得到了,

$ ./node_modules/mocha/bin/_mocha 
path.existsSync is deprecated. It is now called `fs.existsSync`.

  .

  ? 1 tests complete (1ms)
Run Code Online (Sandbox Code Playgroud)

如何使用单个命令执行测试?誓言似乎让你,但我听说摩卡是更好的选择,我似乎无法让它正常工作.

关于我在第三次尝试中遇到的错误的任何想法?

编辑:

我在跑,

  • Ubuntu 11.10 64位
  • Node.js 0.7.5
  • npm 1.1.8
  • 摩卡0.14.1
  • 应该是0.6.0

mocha.js node.js

81
推荐指数
3
解决办法
9万
查看次数

有没有办法让Chai使用异步Mocha测试?

我正在使用浏览器运行器在Mocha中运行一些异步测试,我正在尝试使用Chai的期望样式断言:

window.expect = chai.expect;
describe('my test', function() {
  it('should do something', function (done) {
    setTimeout(function () {
      expect(true).to.equal(false);
    }, 100);
  }
}
Run Code Online (Sandbox Code Playgroud)

这不会给我正常的失败断言消息,而是我得到:

Error: the string "Uncaught AssertionError: expected true to equal false" was thrown, throw an Error :)
    at Runner.fail (http://localhost:8000/tests/integration/mocha/vendor/mocha.js:3475:11)
    at Runner.uncaught (http://localhost:8000/tests/integration/mocha/vendor/mocha.js:3748:8)
    at uncaught (http://localhost:8000/tests/integration/mocha/vendor/mocha.js:3778:10)
Run Code Online (Sandbox Code Playgroud)

所以它显然正在捕捉错误,它只是没有正确显示它.任何想法如何做到这一点?我想我可以用一个错误对象调用"完成",但后来我失去了像柴这样的东西的所有优雅,它变得非常笨重......

javascript unit-testing mocha.js

79
推荐指数
3
解决办法
4万
查看次数

`before()`和`beforeEach()`有什么区别?

什么具体的区别是摩卡before()beforeEach()?(同样的问题after()afterEach().)

我假设before()每个describe()beforeEach()运行一次,每次测试运行一次(it()块).真的吗?

什么时候我会选择使用另一个?

javascript unit-testing mocha.js

78
推荐指数
1
解决办法
4万
查看次数

使用mocha.js连接多个文件的测试

我正在尝试将多个文件中的所有测试加入到一个文件中,如下所示:

  describe('Controllers', function() {
    describe('messages.js', function() {
      require('./controllertests/messages').test(options);
    })
    describe('users.js', function() {
      require('./controllertests/users').test(options);
    })
  })
Run Code Online (Sandbox Code Playgroud)

我很确定这不是加入测试的最佳方式,我有一些难以找到如何做到这一点的例子:s

unit-testing mocha.js node.js

78
推荐指数
4
解决办法
4万
查看次数

Mocha使用额外的选项或参数进行测试

我正在使用Mocha为我的Node.js应用程序编写测试用例.测试用例需要API密钥作为额外的输入选项或参数.API密钥是私有的,所以我不想将它直接包含在测试文件中,因为每个人都可以在GitHub上看到它.我知道Mocha有一些选择:

http://mochajs.org/#usage

但是是否可以包含一些参数让测试人员在命令行中为测试指定自己的API密钥?如:

./node_modules/mocha/bin/mocha test/*.js --key YOUR_KEY
Run Code Online (Sandbox Code Playgroud)

mocha.js node.js

77
推荐指数
7
解决办法
7万
查看次数