Git Product home page Git Product logo

Comments (4)

camlegleiter avatar camlegleiter commented on August 20, 2024 1

I was actually going to suggest doing something just like that: stub in your own stream implementation (Readable, PassThrough, etc.) as the returned value from unzipper and emit events to trigger different function calls. The only downside is because you're keeping your functions private it requires more setup and stubbing to test each private method instead of exposing them for testing by exporting the function separately.

from node-unzipper.

camlegleiter avatar camlegleiter commented on August 20, 2024

Seems like that's a bit of a hassle. Would it be easier to pull out each of your event handlers and test them more individually? Testing code like that feels like its closer to an "integration" test since you're verifying different units of work will work together, which would make more sense to actually use unzipper as part of the test.

from node-unzipper.

VictorCazanave avatar VictorCazanave commented on August 20, 2024

I'm not an expert in testing, so I may have mixed unit and integration tests.
My event handlers are private methods and I didn't plan to test it (I know there is a debate about it).
Actually, the only thing I would like to test is the callback (with or without error):

if(isValid) {
    readableStream.pipe(unzipper.Parse())
        .on('entry', (entry) => {
            // Filter and write files like in the doc
            ...
        })
        .on('finish', () => {
            callback();
        });
} else {
    callback(new Error());
}

If stubbing unzipper is too complicated, I'll try to use it with a simple .zip file for my tests.

from node-unzipper.

VictorCazanave avatar VictorCazanave commented on August 20, 2024

Oops! My bad... Actually my first idea works well but I called the event emit at the wrong place 🤦‍♂️
Here is my solution:

describe('...', () => {
   let readableStream = null;
   let Parse = null;

   beforeEach(() => {
      readableStream = new stream.Readable({ read: () => null });
      Parse = sinon.stub(unzipper, 'Parse');
      Parse.returns(readableStream);
   });
   
   afterEach(() => {
      Parse.restore();
   });

   it('...', done => {
      myFunctionToTest(..., err => {
         expect(err).to.be.undefined;
         done();
      });
      readableStream.emit('finish');
   });
});

Sorry and thanks for you help @camlegleiter

from node-unzipper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.