sinon

sinon stub not replacing function.

吃可爱长大的小学妹 提交于 2019-12-04 05:33:17
I tried a dummy module and to stub it, but does not work. the app.js function foo() { return run_func() } function run_func() { return '1' } exports._test = {foo: foo, run_func: run_func} the test.js app = require("./app.js")._test describe('test', function(){ it('test', function(){ var test_stub = sinon.stub(app, 'run_func').callsFake( function(){ return '0' }) test_stub.restore() var res = app.foo() assert.equal('0', res) }) }) I tried the advice from: sinon stub not replacing function But still the same. It does not replace the function. You have a couple of problems here. The first is that

Mocking http requests in node using mocha and sinon

橙三吉。 提交于 2019-12-04 05:31:33
I have written a NodeJS app using express that proxies some calls to external APIs. So I am trying to write a unit test using Mocha and Sinon. My goal is to test the app without any internet connectivity so I am trying to mock the https requests and return mock replies. I'm having a problem that I can't find any examples or tutorials that fit my case. My node app listens on port 8081 for http requests and then proxies them to another site. I want to test my app without it having to actually send the request to those external servers. I'm trying it below and I put the json replies I want to

Sinon - how to stub nested function?

一笑奈何 提交于 2019-12-03 16:00:21
Apologies if this is a simple question, I'm relatively new to Node and Sinon. I'm struggling trying to figure out how to assert that a nested asynchronous function was called in Nodejs. I'm using mocha, chai, sinon, and request ( https://github.com/request/request ) but think I'm missing something basic on the stubbing part. Example inside my_app.js - var request = require('request'); function MyModule() { }; MyModule.prototype.getTicker = function(callback) { request('http://example.com/api/ticker', function(error, response) { if (error) { callback(error); } else { callback(null, response); }

stubbing a function using jest

不羁的心 提交于 2019-12-03 14:56:56
问题 is there a way to stub a function using jest API? I'm used to working with sinon stub, where I can write unit-tests with stubs for any function call coming out of my tested unit- http://sinonjs.org/releases/v1.17.7/stubs/ for example- sinon.stub(jQuery, "ajax").yieldsTo("success", [1, 2, 3]); 回答1: With jest you should use jest.spyOn: jest .spyOn(jQuery, "ajax") .mockImplementation(({ success }) => success([ 1, 2, 3 ])); Full example: const spy = jest.fn(); const payload = [1, 2, 3]; jest

How to mock a React component lifecycle method with Jest and Enzyme?

和自甴很熟 提交于 2019-12-03 13:11:44
问题 The Enzyme docs for Full DOM Rendering here contains the following example of spying on a lifecycle method with Sinon: describe('<Foo />', () => { it('calls componentDidMount', () => { sinon.spy(Foo.prototype, 'componentDidMount'); const wrapper = mount(<Foo />); expect(Foo.prototype.componentDidMount.calledOnce).to.equal(true); }); }); What is the equivalent to this using mock functions from Jest? I'm using Create-React-App, and would rather not include Sinon if the same can be achieved with

Stub out module function

大城市里の小女人 提交于 2019-12-03 11:56:24
Edit: Being a little bit more precise. I want to test usecases for a Github API wrapper extension, that our team has created. For testing, we don't want to use API wrapper extension directly, so we want to stub out its functions. All calls to the API wrapper should be stubbed out for the tests, not just creating a clone stub. I have a module "github" in Node.js: module.exports = function(args, done) { ... } And I am requiring it like this: var github = require('../services/github'); Now, I would like to stub out github(...) using Sinon.js: var stub_github = sinon.stub(???, "github", function

Stubbing a React component method with Sinon

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 11:53:07
问题 I'm trying to stub a React component method for testing purpose: var Comp = React.createClass({ displayName: "Comp", plop: function() { console.log("plop"); }, render: function() { this.plop(); return React.DOM.div(null, "foo"); } }); var stub = sinon.stub(Comp.type.prototype, "plop"); React.addons.TestUtils.renderIntoDocument(Comp()); sinon.assert.called(stub); // throws This sadly keeps printing "plop" onto the console… and the assertion fails. Note: Directly stubbing the spec object method

How to unit test console output with mocha on nodejs?

[亡魂溺海] 提交于 2019-12-03 10:43:00
问题 Take into account the following example Javascript code below: function privateFunction (time) { if (time < 12) { console.log('Good morning'); } if (time >= 12 && time <19) { console.log('Good afternoon'); } else { console.log('Good night!'); } }; How should I unit test that on nodejs using mocha (and possibly sinonjs), noticing that this is a private function called inside a module? I need to pass in the argument and check if the function is logging the right thing to the console. Can I do

How to mock e.preventDefault in react component's child

社会主义新天地 提交于 2019-12-03 10:34:53
问题 I don't really know how to mock inline function in react component's child My stack: sinon , chai , enzyme ; Component usage: <ListItem onClick={() => someFn()} /> Component's render: render() { return ( <li> <a href="#" onClick={e => { e.preventDefault(); this.props.onClick(); }} > whatever </a> </li> ); } Here we have an onClick function that calls e.preventDefault() . How to tell to <a href> ( link ) not to call e.preventDefault() ? How can I mock an onClick ? Below is what I have tried in

Calling original function from Sinon.js Stub

半腔热情 提交于 2019-12-03 09:48:28
I'm trying to intercept a call with Sinon.js so I can do some logging and then execute the original call. I don't see a way to do this with sinon.spy(), but I think I can do it with sinon.stub(). I provided a custom function: sinon.stub(servicecore.ServiceWrapper.prototype, '_invoke', function(method, name, body, headers, callback) { console.log('---- ServiceWrapper._invoke called! ----'); // How do I call the original function? }); The problem I have is executing the original function, so my application behaves the same. Any idea? You could use a closure. For example: var obj = { foo: