Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Babita-Sandooja
    @Babita-Sandooja
    I did it just to make it work.. but not sure how it's better than what I had already!
    Derek W. S. Gray
    @derekwsgray
    In that SO example, the function has already been assigned. It can't be replaced
    Babita-Sandooja
    @Babita-Sandooja
    ya.. same seemed to be the issue with me
    but we have sinon.replace :thought_balloon:
    even that can't override?
    Derek W. S. Gray
    @derekwsgray
    But you aren't replacing anything
    the original function has been assigned.
    you aren't replacing that assignment
    Babita-Sandooja
    @Babita-Sandooja
    hmm.. you mean I am doing it wrong or is it not possible?
    Derek W. S. Gray
    @derekwsgray
    You need the refactor
    Babita-Sandooja
    @Babita-Sandooja
    yeah
    that's the only way to unit test it?
    Derek W. S. Gray
    @derekwsgray
    To "unit test" a unit has to be isolated. Yours isn't.
    Babita-Sandooja
    @Babita-Sandooja
    yup
    Derek W. S. Gray
    @derekwsgray
    the refactor gives you better isolated and testable code. I'd be good to do regardless
    Babita-Sandooja
    @Babita-Sandooja
    yes yes
    Derek W. S. Gray
    @derekwsgray
    easier for future refactoring, dependency-injection etc.
    I use bottle.js for dependency injection.
    Babita-Sandooja
    @Babita-Sandooja
    okay
    I haven't used it ever.. just read about it
    Looks cool
    Thanks for sharing and all your help!
    Derek W. S. Gray
    @derekwsgray
    Looks like SO did the helping here ;)
    Slim Hmidi
    @slim-hmidi
    hello
    Aaron Gray
    @AaronNGray

    trying to test my minimal jQuery replacement library QueryJS. I am trying to test my once/one function, its fine when I use { once: true } but when I have to use removeEventHandler in the intermediate callback I am not able test it due to async problems.

            describe('one()', function () {
                var documentEventSpy;
    
                beforeEach(function(){
                    documentEventSpy = sinon.spy();
                });
    
                it('event should only fire once', function(){
                    var event = new Event('test');
    
                    $(document).one('test', documentEventSpy);
                    document.dispatchEvent(event);
    
                    this.timeout(1000);
    
                    document.dispatchEvent(event);
    
                    this.timeout(1000);
    
                    expect(documentEventSpy.calledOnce).to.be.true;
                });
            });

    Heres my one function

        one<K extends keyof DocumentEventMap>(event: K, listener: EventListener, options:any) {
            var _this = this;
            this.element.addEventListener(event, (evt: Event):void => {
                _this.element.removeEventListener(event, listener, options);
                listener(evt);
            }, options);
        }
    {once: true} works but not on IE apparently
        once<K extends keyof DocumentEventMap>(event: K, listener: EventListener, options:any) {
            options = options || {once: true};
            return this.element.addEventListener(event, listener, options);
        }
    Craig Freeman
    @Craig939393_twitter

    Hi

    I am trying to stub properties on a stub instance. is it possible please?

    the property is readonly
    Alexandrith C Sharron
    @asharron

    Hey, everyone! Quick question: Is it possible to mock a class before it's imported?

    I have a file that imports a class from an ES6 module using ES6 import / export syntax, but I am testing this file using Node (commonjs).

    During testing, if I import my file to be tested, it breaks because of the ES6 export in the class I am importing.

    Was wondering if it's possible to stub or mock an import so that when my file does an import on the ES6 class, it pulls my Sinon mock instead.

    Any pointers are greatly appreciated!

    Alexandrith C Sharron
    @asharron
    ^ For anyone else running into this issue, I was able to install the esm package npm install -D esm and then use it with my Mocha command mocha -r esm ...
    Lars Gyrup Brink Nielsen
    @LayZeeDK
    I think -r esm works with Node.js v10.x. For Node.js 12.x, I think we need --experimental-modules instead.
    Lukas Wilkeer
    @lukaswilkeer

    What to do when the beforeEach hook doens't get called?
    It's a async function with synchronous and asynchronous following the async/await spec.

    The code looks like.

    let user;
    let db;
    
    beforeEach(async () => {
      const password = md5('manity')
    
      user = {
        account_type: 0,
        email: '****@****.com',
        password: password,
        name: 'Name',
        last_name: 'Surname
        permission: 1
      }
    
      db = await model
    })

    I attempted to set console.log statements to ensure the beforeEach was called, doesn't work.
    The user and db variables doesn't get setted.

    After each works fine.

    What could cause this behaviour?

    • Sorry, a lost my previous question.
    Derek W. S. Gray
    @derekwsgray
    @lukaswilkeer Think you have the wrong forum - I don't see any sinon usage here
    Lukas Wilkeer
    @lukaswilkeer
    @derekwsgray thanks, a tiny mismatch.
    mustafaKamal-fe
    @mustafaKamal-fe
    hello all,
    Is there any body out there now
    mustafaKamal-fe
    @mustafaKamal-fe
    damin it.. i am going to mars
    Carl-Erik Kopseng
    @fatso83
    Just for reference, the maintainers are not actively using this. Your best bet of getting your questions answered is probably StackOverflow (tag with sinon).
    https://stackoverflow.com/questions/tagged/sinon