mocha

Testing for an Asynchronous throw in Mocha

扶醉桌前 提交于 2020-01-04 03:01:27
问题 I have a block of code that tries to reconnect to Redis if a connection breaks. If it can not re-establish connection, it throws an error. I am trying to test the block of code that throws the error, but I am unable to write a successful test using mocha and chai. My test looks like this: it('throws an error when a connection can\'t be established', function (done) { var c = redisClient.newClient(); c.end(); sinon.stub(redisClient, 'newClient', function () { return { connected: false }; });

async/await clarity, with sleep example

╄→尐↘猪︶ㄣ 提交于 2020-01-04 01:44:16
问题 I am trying to get hang of async/await with below implementation but it is not working as expected public static async sleep(ms: number): Promise<void> { await Utilities._sleep(ms); } private static _sleep(ms: number): Promise<{}> { return new Promise((resolve: Function) => setTimeout(resolve, ms)); } _sleep will resolve promise after n milliseconds, and await should sleep till that time.. but below test of mine is failing it("should sleep for 500 ms", ()=> { const date1 = (new Date())

Node.js - why do I get leaks when testing with mocha and zombie?

£可爱£侵袭症+ 提交于 2020-01-03 15:31:10
问题 I've tried to make zombie work with mocha, but unless I use the mocha --ignore-leaks command options, my test always fails with the error: Error: global leaks detected: k, i, name, chars, char My test looks exactly like the one explained in this thread: Mocha and ZombieJS I wish I could have posted my question there, but as a newbie, I cannot comment on the thread, only ask a new question. Do you have any idea why I get these leaks? I'm using mocha 1.0.3 and zombie 1.0.0. 回答1: The leaks can

How to make assertions inside a promise when any errors thrown don't bubble up?

三世轮回 提交于 2020-01-03 08:42:40
问题 Running this with mocha results in timing out, rather than letting mocha catch the error so it could fail immediately.. var when = require('when'); var should = require('should'); describe('', function() { it('', function(done) { var d = when.defer(); d.resolve(); d.promise.then(function() { true.should.be.false; false.should.be.true; throw new Error('Promise'); done(); }); }); }); http://runnable.com/me/U7VmuQurokZCvomD Is there another way to make assertions inside the promise, such that

Mocking file input in React TestUtils

蹲街弑〆低调 提交于 2020-01-03 07:06:05
问题 I have a component with a following render f-tion: render: function() { <input type="file" name: this.props.name, className={this.props.className} onChange={this.props.handleChange} accept={this.props.accept}/> } State is managed by a container which uploads the file server-side using jquery AJAX call: getInitialState: function() { return { uploaded: false }; } handleChange: function(event) { event.preventDefault(); var file = event.target.files[0]; if (!file) { return; } var reader = new

Mocha async test handle errors

断了今生、忘了曾经 提交于 2020-01-02 19:34:22
问题 I'm trying to create a test case with Mocha but my code is asynchronous. That's fine, I can add a "done" callback function to "it" and that will work perfectly fine for positive cases. But when trying to test negative cases, it will just make the test fail. I would like to make something like this but asynchronous: someObject.someMethod(null).should.equal(false) Instead I can only test for a callback to return, instead of testing what really happend (null is not valid): it('this should return

Mocha async test handle errors

≡放荡痞女 提交于 2020-01-02 19:34:08
问题 I'm trying to create a test case with Mocha but my code is asynchronous. That's fine, I can add a "done" callback function to "it" and that will work perfectly fine for positive cases. But when trying to test negative cases, it will just make the test fail. I would like to make something like this but asynchronous: someObject.someMethod(null).should.equal(false) Instead I can only test for a callback to return, instead of testing what really happend (null is not valid): it('this should return

Using HTML reporting with Mocha test framework

痴心易碎 提交于 2020-01-02 01:01:30
问题 I've been generating some tests using NodeJS and Mocha, and I'd like to find a way to place the results into a browser. I know that Mocha has support for this using 'html' reporter and mocha init <dir> however neither seem to be working for me (the reporter actually throws errors without even running a test). Could someone give me a good example of running a test via Mocha and generating a HTML report?An example I want to mimic is on the visionmedia site. Also, for examples sake we'll say I'm

How to force 1:n association with Sequelizejs

女生的网名这么多〃 提交于 2020-01-01 17:37:21
问题 I'm implementing some tests to make sure my sequelize objects are saved correctly. I have a very simple schema: Article <--> Users An Article is published by ONE User A User can publish MANY Articles Here is my Article model definition: module.exports = function(sequelize){ "use strict"; var Sequelize = require('sequelize'); ... var Article = sequelize.define("Article", { slug: { type: Sequelize.STRING, unique: true, comment: "Unique URL slug to access the article" }, title: { type: Sequelize

How to test methods and callback using Mocha, Chai, & Enzyme in React-Redux

不羁岁月 提交于 2020-01-01 12:27:40
问题 I have to write unit test cases for a PlayerList container and Player component. Writing test cases for branches and props is OK, but how do I test the component's methods and the logic inside them. My code coverage is incomplete because the methods are not tested. Scenario: Parent component passes a reference to its method onSelect as a callback to child component. The method is defined in PlayerList component, but Player is generating the onClick event that calls it. Parent Component