-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to stub unzipper? #58
Comments
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 |
I'm not an expert in testing, so I may have mixed unit and integration tests. 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. |
Oops! My bad... Actually my first idea works well but I called the event emit at the wrong place 🤦♂️ 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 |
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 |
Hello,
I use unzipper in a node.js module and everything works well:
Now I would like to unit test my module with Mocha/Sinon/Chai and avoid to really call unzipper using a stub (or something else).
I tried many things but I can't figure out how to handle events (
entry
andfinish
), so the callback() is never called and the test returns a timeout error.Does anyone have an example of unit test with a "fake" unzipper?
PS: I know it's not really an issue, so if you think it's not relevant, fell free to close it.
The text was updated successfully, but these errors were encountered: