Tag: 摩卡

快速路线永远不会执行与Supertest运行摩卡testing

这是一个运行应用程序,并直接用邮递员打电话的路线: router.get("/profile", function(req, res) { var schema = schemas.filter(function(e) { return e.route === "profile"; }).pop(); if (schema !== undefined) { var schemaJson = require(schema.schemaFile); return res.status(200).send(schemaJson); } }); 这是一个testing: var schemas = require("../../conf/schemas"), api_key = require("../../conf/api-keys").pop().key, app = require("../../app"), request = require("supertest"); describe("CDM API Get Schema Operations", function() { it("Returns 404 for schema that do […]

“完成()多次呼叫”摩根与摩卡

请参阅此处的debugging器输出 var expect = require('chai').expect var User = require('../../app/models/index.js').User describe('Model User', function(){ it('add a user', function(done){ var user = new User({ uname: 'cx', password: 'cx' }) user.save() //Model#save() return Promise .then(function(user,effectNum){ done() //success state }) .catch(function(error){ done(error) //fail state }) }) }) 使用Chai,Mocha,Moongoose我在学习moongodb时testing我的代码。 似乎是正确的但是我得到一个错误: 0 passing (2m) 2 failing 1) Model User add a user: […]

如何让node.js和Mocha在浏览器中运行(testing)?

在Win10.64中,我正在命令行运行testing,结果如下: >mocha test Array #indexOf() √ should return -1 when the value is not present 1 passing (16ms) 但是在Chrome中,控制台错误是: 未捕获的ReferenceError:require没有被定义(匿名函数)@test.lead-helper.js:1 test.lead-helper.js: var assert = require("assert"); 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)); }); }); }); 和HTMLtesting运行者: <html> <head> <meta […]

在windows上运行摩卡以获得jsxtesting

我正在尝试通过一个关于react / redux的教程,而且在运行摩卡unit testing时遇到了一个错误。 当我运行'npm运行testing'我得到以下错误(我怀疑必须与path或.jsx扩展名)。 17 error Windows_NT 6.3.9600 18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\xenoputtss\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "test" 19 error node v5.1.0 20 error npm v3.4.1 21 error code ELIFECYCLE 22 error voting-client@1.0.0 test: `mocha –compilers js:babel-core/register –require ./test/test_helper.js 'test/**/*.@(js|jsx)'` 22 error Exit status 255 23 error Failed at the voting-client@1.0.0 test script 'mocha –compilers js:babel-core/register […]

Babel + Mocha堆栈跟踪报告错误的行号

当使用Babel 6和Mocha时,堆栈跟踪报告错误的行号。 我很确定这是因为transpiling增加了额外的代码。 这是Babel 6与Babel 5.x的新行为。 有没有人有一个解决scheme,如何解决这个问题时使用摩卡进行unit testing? 这是我的.babelrcconfiguration: { "ignore": [ "node_modules", "bower_components" ], "presets": [ "es2015", "react" ], "plugins": [ "transform-react-constant-elements", "syntax-async-functions", "transform-regenerator" ] } 注意:不pipe我是否需要('babel-polyfill')在我的应用程序的入口点,这是发生。 示例堆栈跟踪如下所示: TypeError: Cannot read property 'should' of undefined at _callee2$ (test/unit/index.test.js:217:34) at step (test/unit/index.test.js:27:284)

如何在事件处理程序中进行asynchronous调用?

我试图写一个自定义的摩卡记者,与一个HTTP API的第三方集成。 我接受了自定义记者( https://github.com/mochajs/mocha/wiki/Third-party-reporters )的基本示例,并试图在启动事件处理程序中添加对API的调用。 然而,我的HTTP请求callback到第三方似乎从未触发。 示例代码如下所示。 我已经testing了请求代码在其他上下文中按预期工作,它似乎是在摩卡记者的事件处理程序内,没有任何反应。 注意 – 请原谅当前的黑客等待callback的结果,我只是想确保我没有退出之前,callback有机会开火,我会收拾起来,当我得到它的工作! var request = require('request'); module.exports = function (runner) { var passes = 0; var failures = 0; runner.on('start', function() { var callbackFired = false; request('http://www.testapi.com', function (error, response, body) { callbackFired = true; }); while(!callbackFired){ console.log('waiting…'); } }); runner.on('pass', function(test){ passes++; console.log('pass: %s', test.fullTitle()); […]

OSX中的解构工作在Linux上失败

我正在使用一些在我创build的摩卡testing中使用的一些ES6function,其中包括一些解构语句。 这是一个例子: const {logger, saveProxy, resolved, msg} = helpers; 我大部分时间都是从OSX上运行这些testing脚本,它的function就像一个魅力一样。 但是因为nodejs服务利用了一些Unix的二进制文件,所以我也需要在Linux中进行testing。 没问题我用Vagrant VM切换到Linux,并指向完全相同的testing脚本,并得到以下错误堆栈: const {logger, saveProxy, resolved, msg} = helpers; ^ SyntaxError: Unexpected token { at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions..js (module.js:405:10) 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 /usr/lib/node_modules/mocha/lib/mocha.js:216:27 at Array.forEach (native) at Mocha.loadFiles (/usr/lib/node_modules/mocha/lib/mocha.js:213:14) at […]

GitLab-CI和node.js – 如何启动本地服务器然后运行testing?

我已经设置了GitLab-CI,并正在编写我的.gitlab-ci.yml来运行我的testing。 我的应用程序是用node.js编写的,文件如下所示: before_script: – npm install – node server.js stages: – test job_name: stage: test script: – npm run test 我在启动服务器然后运行testing时遇到了麻烦,因为node server.js创build了一个永远不会存在的前台进程,除非您手动执行。 有没有办法启动服务器,然后继续,然后停止一旦testing完成? 或者我真的做错了,我的服务器应该开始在testing本身? 我所读的一切都只是说:“在另一个terminal上运行起始节点,然后在你的本地服务器上运行你的testing”,但是这在自动化的CI系统中显然没有意义。

执行多个Mochaunit testing,取决于其他unit testing结果的结果

我正在为我正在编写的NodeJS应用程序编写一些unit testing,而且我有一个关于某些unit testing逻辑的问题。 比方说,应用程序为用户创build一个“组”,然后创build一些单独的用户,然后将用户添加到该组中,这些都是按照方式(MongoDB)的所有asynchronousfunction, 我想要有一个testing用例来testing这个组是否成功创build,然后是一个用于testing用户创build的testing用例,然后是用户 – >帐户关联。 显然,如果组失败或者用户失败,那么执行用户 – >组关联unit testing就没有意义了。 我应该在unit testing中编写一些逻辑来检查是否有错误(在expect(err).to.equal(null) ),如果它为null,那么执行另一个testing用例? 这是对我来说唯一的逻辑,但我没有看到在任何其他包中的任何unit testing的任何types的逻辑(我看了一些unit testing,看看是否可以发现类似的东西) 另外,如果我有多个与新创build的用户交互的testing用例,那么每个testing用例是否应该尝试创build一个用户,然后继续进行独特的testing? 或者我应该尝试构build它,以便与刚刚创build的用户进行交互(如果成功的话) 例如: describe('User', function () { describe( '.createUser', function() { it( 'Create user Foobar', function( done ) { // … }) } ) describe( '.updateContacts', function() { it( 'Update contacts for user Foobar', function( done ) { […]

node.js如何获得更好的使用摩卡asynchronoustesting的错误消息

我的node.js mocha套件中的典型testing如下所示: it("; client should do something", function(done) { var doneFn = function(args) { // run a bunch of asserts on args client.events.removeListener(client.events.someEvent, userMuteFn); done(); } client.events.on(someEvent, doneFn); client.triggerEvent(); }); 这里的问题是,如果client.triggerEvent()没有做正确的事情,或者如果服务器中断,永远不会调用someEvent ,那么done()将永远不会被调用。 这为以前没有使用过testing套件的人留下了一个模棱两可的错误,如: Error: timeout of 10500ms exceeded. Ensure the done() callback is being called in this test. 我的问题是,有没有办法重写这些testing,无论是与摩卡或除了另一个库,这使得asynchronous工作更容易遵循。 我希望能够输出如下内容: the callback doneFn() was never […]