Search code examples
javascriptmeteorjestjsts-jest

How to use jest to test blocks in Meteor.methods


I am writing jest test to my meteor project. I am trying to test codes in the Meteor.methods, but I am not sure how to do it with Jest.

On the server side (which is also main.js), the code snippets look like something bellow,

Meteor.methods({
        'shops.get': () => { return ShopList.find({}).fetch();},
)};

in main.test.js, I wrote something like

describe('methods', () => {
    let shops=[];
    beforeEach(() => {
    Meteor.call('shops.get',(e,r)=>{
      if(!e) shops=r;
    });
 });
});

But an error occurs:

    ReferenceError: Meteor is not defined

I've read a an article mentioning faking meteor in a file.(https://blog.meteor.com/real-world-unit-tests-with-meteor-and-jest-3d557e84e84a), but I am not sure in this way how to resolve symbols like Meteor.call or Meteor.methods.

Any ideas are appreciated.


Solution

  • Have a look at https://guide.meteor.com/testing.html for information on testing.

    This is an integration test, as you are testing database functions, you should use Meteor to run the tests, as otherwise you have a lot of mocking to do, and then you don't prove much except your mocks work as expected.

    So use the meteor test command, with the appropriate driver, such as https://atmospherejs.com/meteortesting/mocha - it's not jest, but it will give you the result you want.

    If you need more information, I can provide you with some sample code if you like.