Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    hannes
    @escapedcat
    anyone here could point me to an acrticle/post/info how to use tape with sinons fakeserver?
    Jeremy Smith
    @jeremy-smith-maco
    Hey. I am trying to stub an ES6 constructor. I've read a lot of stuff about people not understanding constructors and what not but I need someway for the constructor of a class to not run.\
    I am testing the constructor of one class which I want to run but I want to stub another Class's constructor that is called by the testing constructor.
    I don't want it to run because it fails when I run the whole test suite.
    Adrien Horgnies
    @AdrienHorgnies
    Anyone knows how to mock/stub functions from the same 'class' as the function under test ?
    AFunction = () => { ; }
    FunctionUnderTest = () => { AFunction(); }
    Nishant Jain
    @nishant-jain-94
    Hi all, How to spy on functions available on global or window object.?
    Adrien Horgnies
    @AdrienHorgnies
    @nishant-jain-94 Did you try something like sinon.spy(Window, 'myFunction') ?
    Nishant Jain
    @nishant-jain-94
    @AdrienHorgnies No I was using it in NodeJS
    Allen Houng
    @ayhoung
    hi all
    i was wondering how to get this inside of a fakeCall?
    const stub = sinon.stub(Person.prototype, ’sayHello').callsFake(() => {
        console.log(this); //returns empty object
        return Promise.resolve({});
    });
    this just returns an empty object
    Adrien Horgnies
    @AdrienHorgnies
    @ayhoung You can assert the context this with sinon method spy.calledOn(obj);. This compares the parameter obj you provide to the test with the actual this used to call the method.
    I suppose you can replace the anonymous function with a spy as the argument of the callsFake method.
    Allen Houng
    @ayhoung
    @AdrienHorgnies thanks for the feedback
    however, wouldn’t supplying the spy as an argument require a change in the core code?
    Adrien Horgnies
    @AdrienHorgnies

    @ayhoung No, use a stub to create whatever behavior you need, knowing that a stub has spies full functionalities.

    They [stubs] support the full test spy API in addition to methods which can be used to alter the stub’s behavior.

    Luis Vegerano
    @lvegerano

    Hi all, I have an object

    const contextAPI = {
      auth: {
        lock_api_call() { return true; },
      },
      sql: 'SELECT * FROM foo;',
    };

    that I would like to mock const context = Sinon.mock(contextAPI);
    but I’m getting an error Attempted to wrap undefined property lock_api_call as function and Im not sure what am I doing wrong. Any pointer would be appreciated

    Adrien Horgnies
    @AdrienHorgnies
    @lvegerano I don't quite understand the usefulness of sinon.mock but I can say it works better when you mock things such as const lib = require('libYouWantToMock');
    If it's an object you provide as a parameter to the unit you're testing, just build whatever you want to replace the actual parameter (and possibly put a spy/stub whatever function there is in your function).
    const lockStub = sinon.stub().returns(true);
    const contextAPIDummy = {
        auth: {
            lock_api_call : lockStub
        },
        sql: 'SELECT * FROM foo;'
    };
    objectkit
    @objectkit

    Hi there - glad to find this room! Just a quick one to start - I’m installing sinon latest via npm like so:

    npm i -D sinon

    when I do

    npm list sinon

    sinon is reported as version 2.4.1, yet the latest release is3.2.1 apparently…

    Any idea why there is a version difference?

    Thanks!

    Hekar Khani
    @hekar
    Hello, could a patch version (3.3.1) be published to npm with #1563?
    jxLoaderdoesn't have a license (among other issues. ie. not sure we want such questionable dependencies executing during our builds.)
    James Gardner
    @james-gardner
    Hi, I'm struggling with writing a test at the moment. I'm basically trying to test a sequence that runs error first callbacks. What I'd like to be able to do is check that an objects been modified within each call:
       const next = (ctx, next) => next();
    
      const done = sinon.spy();
    
      const context = {};
    
      sequence(context)
            .phase(next)
            .phase(next)
            .phase(next)
            .boot(done);
    
        t.truthy(done.calledOnce);
    so for each phase context has a property bolted on to it
    I'd love to be able to use something like:
        const phase = sinon.stub().callArg(1);
            phase.onCall(0) ....
    But how can I modify context from the stub ?
    James Gardner
    @james-gardner
    The answer is not to use spies or mocks.
    Stefan Schneider
    @hinogi
    good evening
    Brandon Istenes
    @brandones

    Hey folks, I'm mocking out a whole object (against the advice of the docs) like

    myStub = sinon.stub(myObj)

    However, that doesn't seem to define a restore method on myStub. How do I restore the original object?

    Brandon Istenes
    @brandones
    Oh wow, I just found out that where the documentation says, e.g. spy.args, it actually means call.args, where call is the sort of object returned by spy.getCall(0).
    Even if for some strange reason args is in fact defined on spies themselves (which it seems to be), the documentation seems misleading with respect to actual usage.
    Adrien Horgnies
    @AdrienHorgnies

    @brandones I advice you to always use the sandbox

    const sandbox = sinon.sandbox.create();
    // do things
    sandbox.verifyAndRestore();

    You can check some tests here

    Adrien Horgnies
    @AdrienHorgnies
    @brandones Regarding args, I didn't find the documentation confusing : Array of arguments received, spy.args[0] is an array of arguments received in the first call..
    But as you do seem confused, maybe some code can help you :
    > spy.thirdCall.args === spy.args[2]
    true
    > spy.getCall(20).args[3] === spy.args[20][3]
    true
    Brandon Istenes
    @brandones
    Yeah, it turned out that the function I was expecting to be called was being called in a promise, and I wasn't waiting for the promise before running my assertions.
    Sandbox seems like just the thing. Perhaps it deserves a more prominent note in the documentation (e.g. in connection with stubbing out whole objects). Thank you for your help!
    Adrien Horgnies
    @AdrienHorgnies
    @brandones I have tons of tests with promises if you want, you can check some if you want.
    Adrien Horgnies
    @AdrienHorgnies
    And yes, I agree with you, sandbox should get more light. I had read the whole documentation plus a tutorial or two about sinon when sinon.sandbox didn't exist. When I discovered it existed by mistake, I thought that it would save me time, make code more readable and more importantly, that would save collaborators from forgeting to verify or restore spies & co.
    Brandon Istenes
    @brandones
    @AdrienHorgnies Nice, thanks!
    Brandon Istenes
    @brandones

    Okay, new problem.

    foo.js

    const doFoo = () => { console.log('bad stuff!') }
    const doBar = () => { doFoo() }
    module.exports = { doFoo, doBar }

    foo.test.js

    const sandbox = require('sinon').sandbox.create()
    const foo = require('./foo')
    
    describe('doBar', () => {
      before(() => {
        sandbox.stub(foo, 'doFoo')
      })
      it('should call doFoo', () => {
        foo.doBar()  // prints "doFoo" to console
      })
    })

    What gives? Why isn't doFoo mocked when doBar calls it?

    Adrien Horgnies
    @AdrienHorgnies
    @brandones If I remember well, you can only wrap functions external to the code you test.
    In your test suit, you're calling a function and mocking another from the same module. That won't work; my guess is that it is because both functions are defined at the same time, giving no opportunity to sinon to wrap doFoo before doBar is defined.
    Anyway, if you must mock functions from the unit you're testing, it smells bad. Either you should move doFoo to another module or you should target more precisely the dangerous section : sandbox.stub(console, 'log');.
    Dimitris Karagiannis
    @mkarajohn
    Hello! I'd like to ask, is it possible to execute a callback once a spy has been called ?
    for example
    const aSpy = sinon.spy(object, 'method');
    aSpy.wasCalled(function() {
       //....do some stuff after the spied method was called
    })
    I have thought of using a stub, saving the original method that is going to get stubbed and then calling the original method along with other code inside a .callsFake()
    Dimitris Karagiannis
    @mkarajohn
    but then I don't know how to get the proper this inside the callsFake in order to apply it to the original method
    Dimitris Karagiannis
    @mkarajohn
    ...ok, it seems like the correct this is passed to the callsFake context. neat
    Adrien Horgnies
    @AdrienHorgnies
    @mkarajohn Look for the onCall method.
    Gintautas Miselis
    @Naktibalda

    I have a problem with undefined methods on mock
    My test code looks like this:

    const mock = sinon.mock(new Xxx(configuration));
    mock.expects('methodA').once().withArgs(sessionId, '', '', '', null).resolves(null);
    new ClassUnderTest(mock, 100);

    ClassUnderTest calls arg1.methodA(); and fails with TypeError: arg1.methodA is not a function

    in debugger I see that arg1 has methodA in expectations and proxies and I see the original object in object property
    Gintautas Miselis
    @Naktibalda
    thanks, I figured it out :) I had to pass the original instance of the class, not the mock
    jeffhall-wk
    @jeffhall-wk
    Hello all! I installed Sinon globally and my node script can't find it. I just receive a 'module not found' error. I see that it is advertised that Sinon doesn't pollute the global namespace, and I'm kind of a newb here. So, is my global installation not being found expected?
    jeffhall-wk
    @jeffhall-wk
    Nevermind! It looks like I shouldn't install it globally. I was trying to mirror another system I found, but they home-roll some other configurations. So I'm all good now :D. Thanks, all!