Problem with unit test on Meteor server side TypeScript


#1

Hi,

I have a problem with my unit tests on server side.

My test is the next :

import { Meteor } from 'meteor/meteor';
import { Article } from '../../../imports/models/article';
import { Articles } from '../../../imports/collections/articles';
import './articles';
import { Random } from 'meteor/random';
import {Rate} from "../../../imports/models/rate.model";
import { expect, assert } from 'chai';
import {Observable} from "rxjs/Observable";

if (Meteor.isServer) {
    describe('Articles', () => {

        const userId = Random.id();

        beforeEach(() => {
            StubCollections.add([Articles]);
            StubCollections.stub();
            Articles.remove({});
        });

        it('can delete owned article', async (done) => {

            const articleId = await Articles.insert({
                title: "string",
                content: "string",
                owner: userId,
                picture_url: "string",
                source: "string",
                createdAt: new Date()
            }).toPromise();


            const deleteArticle = Meteor.server.method_handlers["removeArticle"];
            // // Run the method with `this` set to the fake invocation
            //
            const invocation = {userId};

            deleteArticle.apply(invocation, [articleId]);

            console.log(articleId);

            const count = await Articles.find({}).count().toPromise();

            // Verify that the method does what we expected
            expect(count).equal(0);
            StubCollections.restore();
            done()
        });
    });
}

And I can’t import stub-collection because typescript not found it.

I have try to had meteor server package on tsconfig.json but I did not suceed.

And when I delete StubCollection I have a Timeout of 2 seconde when “Articles.find({})”

Have you a idea for resolve it ?


#2

My problems is with Article.find({}) result of type Observable with Meteor.observable.

My new test is the next

        it('can delete owned article', async done => {

            let fixFindToPromise: number  = 0;

            const articleId = await Articles.insert({
                title: "string",
                content: "string",
                owner: userId,
                picture_url: "string",
                source: "string",
                createdAt: new Date()
            }).toPromise();
            // console.log('A2', articleId2);

            const deleteArticle = Meteor.server.method_handlers["removeArticle"];
            // Run the method with `this` set to the fake invocation
            deleteArticle.apply({userId}, [articleId]);


            // Find the internal implementation of the task method so we can
            console.log("ArticleId:", articleId);

            Articles.find().subscribe((countLog) => {
                fixFindToPromise++;

                if (fixFindToPromise == 1 ) {
                    if ( countLog.length == 0 ) {
                        done();
                    } else {
                        done("Count not correct");
                    }
                }
            });

It’s possible use find method with promise compatibilty result for better syntax and use assert and expect ?
And exists a project or solution for import package on typescript unit test server side ?

Thank for your response