我编写了一个非常简单的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) 其他相关问题中提供的解决方案,例如在.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) 如何设置Karma来运行我的后端单元测试(用Mocha编写)?如果我将后端测试脚本添加到files = [],则说明require未定义.
我有一个回调函数,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) 我正在使用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有什么区别?
我很难让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)
如何使用单个命令执行测试?誓言似乎让你,但我听说摩卡是更好的选择,我似乎无法让它正常工作.
关于我在第三次尝试中遇到的错误的任何想法?
编辑:
我在跑,
我正在使用浏览器运行器在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)
所以它显然正在捕捉错误,它只是没有正确显示它.任何想法如何做到这一点?我想我可以用一个错误对象调用"完成",但后来我失去了像柴这样的东西的所有优雅,它变得非常笨重......
什么具体的区别是摩卡的before()和beforeEach()?(同样的问题after()和afterEach().)
我假设before()每个describe()块beforeEach()运行一次,每次测试运行一次(it()块).真的吗?
什么时候我会选择使用另一个?
我正在尝试将多个文件中的所有测试加入到一个文件中,如下所示:
  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
我正在使用Mocha为我的Node.js应用程序编写测试用例.测试用例需要API密钥作为额外的输入选项或参数.API密钥是私有的,所以我不想将它直接包含在测试文件中,因为每个人都可以在GitHub上看到它.我知道Mocha有一些选择:
但是是否可以包含一些参数让测试人员在命令行中为测试指定自己的API密钥?如:
./node_modules/mocha/bin/mocha test/*.js --key YOUR_KEY
Run Code Online (Sandbox Code Playgroud) mocha.js ×10
node.js ×6
javascript ×5
unit-testing ×3
arrays ×1
asynchronous ×1
babeljs ×1
chai ×1
express ×1
karma-runner ×1
npm ×1
syntax-error ×1
testing ×1