Meteor-files: Unit testing meteor-files

Created on 30 Nov 2020  Â·  13Comments  Â·  Source: veliovgroup/Meteor-Files

I want to implement basic unit tests for inserting different types of files, fetching and removing. Do you know if there is an example of this anywhere?

question

All 13 comments

@shariq1989 Do you wish to test client to server upload? Or other library features?

Client to server upload would be ideal but then also just testing CRUD server operations, seeing how it reacts to different files, etc. We are using Meteor and Mocha but are having trouble getting tests written out. Not sure how to mock a file for upload, etc.

@shariq1989 this is tricky part; and the only reason this package not covered with tests — this kind of unit testing is time consuming to develop. As of right now I see only two options — oldschool Selenium __or__ modern puppeteer from Chrome team, as you got to replicate user's actions, like clicking on form, selecting file, waiting for upload to go through.

The rest of Server features can get tested with Mocha/Chai or even with Meteor's tinytest.

I'd start with one-by one features you're using, passing arguments matching your usage and validating its output.
If this is commercial project, I can join your team for this specific task, let me know if you're interested.

@dr-dimitru thank you for your prompt responses! We'll try to see how far we get with testing the server functions. This is for a school project but thanks for the interest.

@dr-dimitru, is there a simple way to add an image that is stored in the repo (say in assets dir) to the db using the server? Then we'll be able to at least test the server funcs against the pushed image. We've tried Images.load, Images.insert, etc without luck. I'm even fine with just grabbing an image from a public URL and storing it. I just need the server to insert to the test db.

@shariq1989

We've tried Images.load, Images.insert, etc without luck

Did you get some sort of error? Exception? What exactly doesn't work?

@shariq1989 You could use Meteor's builtin WebApp to crawl some random image from the web, say from unsplash.com or something and then insert it to the filesCollection before starting the tests.

@shariq1989

We've tried Images.load, Images.insert, etc without luck

Did you get some sort of error? Exception? What exactly doesn't work?

When I do something like this:


    it("delete image", async function () {
        // Upload sample files on server's startup:
        Receipts.load('https://upload.wikimedia.org/wikipedia/commons/4/4e/Pleiades_large.jpg', {
            fileName: 'gif img'
        }, function (writeError, fileRef) {
            if (writeError) {
                throw writeError;
            } else {
                console.log(fileRef._id);
            }
        });
    });

So I do see that a new file was created based on the contents of fileRef (there is a generated _id) but after that I am struggling to check the contents of the db. Ideally i'd like to assert against counts in the database but any other way works.

I tried the following but I got an empty array. I tried to use findOne with the specific _id and got the same. This made me question whether the insert was actually taking place.


let res = Receipts.find({}).fetch();
console.log(res);

I'm probably missing something small but crucial. Just can't put my finger on it.

@shariq1989 Check it inside 10 seconds setTimeout

That was it. Thanks @dr-dimitru

@shariq1989 perhaps fine tuning is required for your mongo setup.
But, I won't care much on early stages.
I'm glad we were able to solve it quickly

Was this page helpful?
0 / 5 - 0 ratings