Tag: 摩卡

从摩卡中的afterEach中检测testing失败

我想创build一个afterEach挂钩与逻辑,应该只会触发如果以前的testing失败。 例如: it("some_test1", function(){ // something that could fail }) it("some_test2", function(){ // something that could fail }) afterEach(function(){ if (some_test_failed) { // do something to respond to the failing test } else { // do nothing and continue to next test } }) 但是,我没有已知的方式来检测afterEach钩子内的testing是否失败。 是否有某种我可以附加到摩卡的事件监听器? 也许这样的事情: myTests.on("error", function(){ /* … */ })

Chai unittesting – expect(42).to.be.an('integer')

根据http://chaijs.com/api/bdd/#a ,可以使用a / an来检查variables的types。 。一种) @param{ String } type @param{ String } message _optional_ a和an断言是可以用作语言链或声明一个值types的别名。 但是,我无法检查variablesbeeing整数。 给定的例子,例如expect('1337').to.be.a('string'); 为我工作,但以下不: expect(42).to.be.an('integer'); expect(42).to.be.an('Integer'); expect(42).to.be.an('int'); expect(42).to.be.an('Int'); 运行摩卡时,他们都给我以下错误: Uncaught AssertionError: expected 42 to be an integer 我如何用chai来testing一个整数的variables?

以编程方式运行Mocha并将结果传递给variables或函数

我使用ZombieJS和Chai在摩卡上设置了一套testing。 testing加载一个网站,并检查各种服务是否正确预订,并显示给网站的访问者。 我的目标是testing将每天运行,然后将结果通过电子邮件发送给我的团队。 testing全部按预期运行,但是我遇到的阻塞如下。 如何将JSON记者结果传递给另一个node.js脚本,我可以通过电子邮件发送结果。 build立电子邮件并发送它将会直接使用nodemailer和下划线模板。 我目前的想法是有两种方法。 使用shell脚本运行mochatesting,并将JSON输出传递给节点脚本,并从命令行参数处理JSON。 就像是… mocha test/services/homepage.js > node email.js 另一种方法是从节点脚本中运行testing,并将返回的结果放入一个variables中。 我一直在使用这里的信息来运行节点内的testing。 https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically 这运行正确,但我迷失了如何从下面的代码中获取JSON记者结果变成一个variables。 var Mocha = require('mocha'), Suite = Mocha.Suite, Runner = Mocha.Runner, Test = Mocha.Test; // First, you need to instantiate a Mocha instance var mocha = new Mocha({ reporter: 'json' }); var suite = new Suite('JSON suite', 'root'); […]

使用Node.js更好的方式来require.extensions

我正在testing一堆React JSX组件。 他们都需要用React或者Babel或者其他方法进行编译,但是我们有特殊的需求,所以我试图用一个与Mocha一起运行的特殊编译器来覆盖需求。 下面的解决scheme运行良好,但是您会注意到我们正在使用require.extensions []来捕获所有的.jsx文件。 我所关心的是require.extensions被locking并被弃用。 有没有更好的方法来做到这一点? // Install the compiler. require.extensions['.jsx'] = function(module, filename) { return module._compile(transform(filename), filename); }; 下面是整个转发器供参考: // Based on https://github.com/Khan/react-components/blob/master/test/compiler.js var fs = require('fs'), ReactTools = require('react-tools'); // A module that exports a single, stubbed-out React Component. var reactStub = 'module.exports = require("react").createClass({render:function(){return null;}});'; // Should this file be stubbed […]

摩卡:以编程方式创build并完成testing

我想要代理testing结果在其他环境上实时运行。 下面是一些我想实现的伪代码: var test = proxy.getCurrentTest(); // => {slow: 200, timeout: 2000, duration: 235, result: 'error'}; var tmpIt = it('test1', function(){ this.slow(test.slow); this.timeout(test.timeout); }); tmpIt.close({ duration: test.duration, result: test.result }); // this should make this test red in the output, // because the `result` is not 'success' 无论如何设置testing的结果和持续时间没有“真正”运行吗? 并获得所有的视觉摩卡输出到terminal? 编辑:这个问题不是关于如何将testing结果forms的subprocess传递给主进程。 它已经在为我工作。

咕嘟,摩卡,手表不重新加载我的源文件

我正试图让gulp工作来帮助自动化一些unit testing。 我有以下gulp文件。 var gulp = require('gulp'), mocha = require('gulp-mocha'); gulp.task('unit', function() { return gulp.src('test/unit/**/*.js') .pipe(mocha({ reporter: 'spec' })) .on('error', handleError); }); gulp.task('watch', function() { gulp.watch(['src/**/*.js', 'test/unit/**/*.js'], ['unit']); }); gulp.task('test', ['unit', 'watch']); 当我运行“吞吐单位”时,testing运行良好。 当我运行“吞噬testing”时,testing运行,看起来“看”正在工作。 如果我对其中一个testing文件进行了更改,则考虑到我在testing文件中所做的更改,testing会正确地重新运行。 如果我对源文件进行了更改,那么testing也会重新运行,但不会针对源文件的更新版本运行。 我的想法是,不知何故,源文件被caching,但我找不到任何似乎有这个问题或find解决scheme的其他人。 感谢帮助这个Gulp / Node / Mocha的新手!

在NodeJs中使用摩卡嘲笑

我如何使用NodeJ来嘲笑Mocha中的客户端和服务器。 具体来说,我有以下代码: app.post ('path name', function (req, res) { // Some Action res.send(response); }); 我想模拟req , res参数和testingres (状态,标题,消息)。

确保快速应用程序在每个摩卡testing之前运行

我正在使用ExpressJS,NodeJS,Mongoose和Mocha开发REST API。 问题是我有一个app.coffee文件,负责设置ExpressJS并连接到Mongoose。 我设置的方式是,首先连接Mongoose,如果通过,则启动ExpressJS App。 问题是,在设置Mocha时,我需要确保app.coffee中存在的ExpressJS App在执行任何testing用例之前已经成功启动,包括所有asynchronous代码。 为此,我创build了一个test_helper.coffee,并在其中放置了下面的代码,但是,即使app.coffee中的代码还没有完全完成它的执行,testing用例也会开始执行,这实际上是有意义的: before (done) -> require(__dirname + '/../src/app') done() 简而言之,我想确保ExpressJS应用程序在执行任何testing用例之前已完全完成设置。 我怎么能做到这一点?

摩卡命令给ReferenceError:窗口没有定义

首先它不是重复的问题,因为我已经看过他们。 我正在使用下面的命令 mocha –compilers :./test/babel-setup.js –recursive –watch 给出下面的错误: ReferenceError: window is not defined 在babel-setup.js下面的代码在那里。 require("babel/register")({ compact: false }); 我使用节点0.12.7和“摩卡”:“^ 2.2.5”和“jsdom”:“^ 3.1.2”。 我错过了什么? 我已经安装了该项目的jsdom 。

在mocha中为ts-node覆盖`tsconfig.json`

有没有可能覆盖哪些tsconfig.json ts节点使用时从摩卡调用? 我的主要tsconfig.json包含"module": "es2015" ,但我想只使用"module": "commonjs" ts节点只。 我试过这个 mocha –compilers ts:ts-node/register,tsx:ts-node/register \ –compilerOptions '{"module":"commonjs"}' \ –require ts-node/register test/**/*.spec.ts* 但它没有工作: SyntaxError: Unexpected token import at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:387:25) at Module.m._compile (/usr/lib/node_modules/ts-node/src/index.ts:406:23) at Module._extensions..js (module.js:422:10) at Object.require.extensions.(anonymous function) [as .tsx] (/usr/lib/node_modules/ts-node/src/index.ts:409:12) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) at require (internal/module.js:16:19) at /usr/lib/node_modules/mocha/lib/mocha.js:222:27 […]