Unit test of Meteor method


#1

Hi all.
In server folder I have file with description some Meteor methods. Smth like this

Meteor.methods({
    sumCoords(lat:number, lng:number)
    {...;
return lat+lng
}

I created test-file in client folder test.spec.ts:

describe('Test coordinates method', () => {

    beforeEach(() => {
    });

    test('You should have equal values', () => {
const a = Meteor.call('sumCoords',34,45);
        expect(a).toBe(79);
    });

Then I’m starting this test and get error


    ReferenceError: Meteor is not defined

      22 |        
      23 |        
    > 24 | const a = Meteor.call('sumCoords',34,45);
      25 |         expect(a).toBe(79);
      26 |     });
      27 | 
      
      at client/imports/app/shared/location.spec.ts:24:11
      at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:388:26)
      at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:79:39)
      at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:387:32)
      at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:138:43)
      at Object.testBody.length (node_modules/jest-zone-patch/index.js:50:27)

Another tests are working and showing “PASS” in terminal.
I use Meteor method in another files and it works correctly.
I found some answer in Google and tried to add import { Meteor } from 'meteor/meteor'; but get the same test-fail with error Cannot find module 'meteor/meteor' from 'location.spec.ts'

Can you help, please.


#2

Meteor.call from the client does not return a result like that. It has an asynchronous callback.

On the client, if you do not pass a callback and you are not inside a stub, call will return undefined , and you will have no way to get the return value of the method. That is because the client doesn’t have fibers, so there is not actually any way it can block on the remote execution of a method.


#3

How I can call this method here?


#4

Something like

Meteor.call('sumCoords',34,45, (err, a) => {
  expect(a).toBe(79);
})

However, you’ll need to ensure this is an asynchronous test. You may also have syntax like expect(a).to.eventually.equal(79) which your test framework may support.


#5

But error is in Meteor.
No matter how I call methods, program can not understand Meteor.

ReferenceError: Meteor is not defined

#6

Check out how I do this. I’ve had no issue testing methods this way.


#7

As I understood, my problem is not in tests files, I have trouble with import { Meteor } from 'meteor/meteor'; and error while running tests

Test suite failed to run
    Cannot find module 'meteor/meteor' from 'location.spec.ts'

#8

So you’re saying in other testfiles meteor/meteor imports ok it’s just in this particular file it’s not found?


#9

IDE understands this import, but tests are falling.


#10

How are you running your tests?
You need to run them with meteor test to have the meteor environment