Tag: 摩卡

运行node.js mochatesting时意外的令牌ILLEGAL

我已经搜遍了,但还没有弄清楚我做错了什么。 我正在尝试设置mocha来testingnode.js javascript应用程序文件。 我已经安装了节点,并已成功运行基本的东西,以确认它正在工作。 我在我的项目文件中安装了摩卡,在我的项目文件中也有一个名为“test”的Makefile和一个文件。 这是当我运行命令“make test”时,我的terminal(osx 10)吐出的错误。 humbleMousesMBP:chapter02 humbleMouse$ make test /Users/humbleMouse/chapter02/test/exchange.test.js:22  ^ SyntaxError: Unexpected token ILLEGAL at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:364:17) at require (module.js:380:17) at /Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:313:27 at Array.forEach (native) at load (/Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:310:9) at Object.<anonymous> (/Users/humbleMouse/chapter02/node_modules/mocha /bin/_mocha:301:1) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) […]

对同一个幻象实例使用createPage()两次

我试图用PhantomJS设置摩卡testing,而且我遇到了一个问题,我无法使用相同的幻像实例来创build多个页面。 第一个testing用例运行得很好,但第二个testing用例运行良好。 我只想使用一个实例,因为它应该更快。 var assert = require('assert'); var phantom = require('phantom'); var path = require('path'); var ph; describe('document', function() { before(function(done) { // Create only one phantom instance for the whole suite this.timeout(10000); // Prevent test case from aborting while phantom loads phantom.create(function(p) { ph = p; done(); }, { dnodeOpts: {weak: false} }); }); […]

如何通过摩卡testing两个文件是否相同

我已经编写了一个node.js脚本,用于扫描非常大的遗留代码库并应用一组更改。 这些更改可能相当复杂,因为这会影响某些存储机制和相关的大小定义。 我已经写了testing,所有的计算和报告是正确的,使用摩卡,柴和sinon。 “ 现在我正在尝试编写一个testing,将更改应用到传统源的20个样本。 我可以使用正确的更改创build20个样本的版本,我只是希望我的testing将所应用的更改与预测的更改进行比较。 所以实质上,testing是文件A和文件B之间的差异,其结果应该是文件是相同的。 Google在数组,string和对象差异上显示了很多结果。 有没有人有解决scheme如何做到这一点的文件? 我应该注意到,我并不是试图重新发明轮子。 我已经用prettydiff了一些prettydiff ,其中包括: prettydiff({source: processedFile, diff: expectedFile, lang: 'text'}).should.equal("Number of differences: 0 differences from 0 lines of text."); 我也试过: expect(prettydiff({source: processedFile, diff: expectedFile, lang: 'text'})).to.equal("Number of differences: 0 differences from 0 lines of text."); 然而,我在这两种情况下得到一个错误,说明Uncaught TypeError: object is not a function 。 使用摩卡testing两个文件的差异的正确方法是什么?

柴期望抛出一个(错误)不通过testing(使用节点)

问题是: 我正在使用Chai来做testing,而且我似乎被卡住在testing一个预期的错误: 柴期待[function]抛出(错误) 当前代码: 这是testing的代码: describe('Do something', function () { it('should remove a record from the table', function (done) { storage.delete(ID, done); }); it('should throw an error when the lookup fails', function () { expect(storage.delete.bind(storage, ID)).to.throw('Record not found'); }); }); 这里是函数的代码: delete: function (id, callback) { // Generate a Visitor object visitor = new Visitor(id); […]

如何使用chai和mochatestingsubprocess?

我正在创build一个框架来在特定的时间执行进程(cron-like),并testing我正在使用chai-mocha-grunt。 解决scheme的体系结构基于此示例 。 基本上,我们有: 一个主进程,通过特定的次数调用subprocess(通过child_process.fork)。 一个subprocess,它使用setInverval()来执行某些事情; 调用Master.start()函数的过程。 有了这个架构,我该如何testing,以确保线程在正确的时间使用摩卡和柴(与“断言”库)执行? 换句话说,我该如何让线程“听”线程并检查它们是否在正确的时间执行?

在未被捕获的错误上,node.js mocha回退到debugging器

有没有一种方法在node.js(理想的摩卡也)模拟python的行为 python -m pdb setup.py test 如果程序抛出一个未捕获的exception,那么这将回退到debugging接口。 编辑 @robertklep提出了无证的breakOnException命令,看起来非常棒,但在实践中并没有那么多: test.js var hello = {}; function testFunc () { anotherFunct(); } function anotherFunct () { var b = hello.unfound; } testFunc(); 然后它冻结在: $ node debug test.js < Debugger listening on port 5858 connecting to port 5858… ok break in test.js:1 > 1 var hello = {}; […]

SailsJS Passport会话持久性问题:摩卡testing失败

我正在尝试使用Mocha,Chai和Superagent以SailsJS和Passport作为身份validation框架来编写一些基本的身份validationtesting。 以下是我的testing场景,由于某种原因,我无法保持一个持续的会话,它似乎是我的最后一个testing,看看用户是否有权访问/userplansetting/edit失败。 注意:我已经确认,当我通过networking界面login时,这是有效的。 更新:这是一个包含类似的代码和结果的回购https://github.com/robksawyer/sailsjs-starter-template request = require("superagent") crypto = require("crypto") async = require("async") chai = require("chai") expect = chai.expect should = chai.should() assert = chai.assert userStub = -> randString = crypto.randomBytes(20).toString("hex") username: randString.slice(0, 15) biography: randString + " is a auto generated user!" email: randString + "@gmail.com" password: "123123123123" displayName: "John Doe" language: "en-us" describe […]

摩卡asynchronoustesting处理错误

我试图用摩卡创build一个testing用例,但是我的代码是asynchronous的。 这很好,我可以添加一个“完成”callback函数“它”,这将正常情况下完美的罚款。 但是当试图testing负面情况时,只会使testing失败。 我想做这样的事情,但asynchronous: someObject.someMethod(null).should.equal(false) 相反,我只能testing一个callback返回,而不是testing真正发生了什么(null是无效的): it('this should return false or an error', function(done) { someObject.someMethod(null, '', done); }); 我想写这样的东西: it('this should return false or an error', function(done) { someObject.someMethod(null, '', done).should.throw(); }); 但是这会导致这个错误: "TypeError: Cannot read property 'should' of undefined" 我也尝试使用期望和断言,但同样的规则适用。 任何线索? 谢谢 编辑#1: 我没有成功尝试: 使用true / false return (true); // ok retur (false); […]

使用Mocha / Chai来testingNodeJS的匿名callback代码

我是unit testing的新手。 我正在使用Node.js,我正在使用asynchronous模块 。 这是我正在使用的代码: module.exports = { postYelp: function (cb, results) { if (results.findLocation) { results.client.post(['resources', 'Yelp'].join('/'), results.findLocation, function (err, data, ctx) { /* istanbul ignore next */ if (err) { cb(err); } else { console.log('data', data); console.log('ctx', ctx.statusCode); return cb(null, ctx.statusCode); } }); } /* istanbul ignore next */ else cb(null); }, } […]

从生成的subprocess运行脚本时,如何在控制台中保留颜色?

我有一个充满seleniumtesting的摩卡文件。 当我从正常的命令行运行摩卡时,由于颜色模块,我得到了这个不错的格式化和彩色输出。 这看起来不错,工作起来很奇妙,但手动运行只能在单个环境下运行testing。 为了在多个环境中并行运行testing,Sauce Labs(Selenium云托pipe)build议产生摩卡subprocess。 我在我们的项目中build立了一个Gulp任务。 gulp.task('test', function () { var targets = [ 'windows.chrome', 'windows.firefox', 'windows.ie', 'mac.chrome', 'mac.firefox', 'mac.safari', 'linux.chrome', 'linux.firefox' ]; function run_mocha(target, done) { var env = Object.assign({}, process.env); env.TARGET = target; var mocha = exec('mocha ./test/test-runner.js', {env: env}, done); ['stdout', 'stderr'].forEach((stream) => mocha[stream].on('data', (data) => process[stream].write(`${target}:${data}`)) ); } var jobs = […]