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

Factory.create returns undefined inconsistently #34

Closed
yanickrochon opened this Issue Dec 1, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@yanickrochon
Copy link

yanickrochon commented Dec 1, 2017

This is related to issue #32.

I have tests wrapped like this one :

describe('Testing DB', () => {
  it('should create model', dbTest(() => {
    const user = Factory.create('user');
    expect( user ).to.not.be.undefined;
  }));
});

Where the dbTest function is defined as such :

export function dbTest(handler) {
  if (Meteor.isServer) {
    return function run() {
      return new Promise((resolve, reject) => {
        const runner = Meteor.bindEnvironment(() => {
          Fiber(function () {
            try {
              Promise.resolve(handler()).then(resolve, reject);
            } catch (error) {
              reject(error);
            }
          }).run();
        });
        runner();
      });
    }
  } else {
    return handler;
  }
};

(BTW: I have tried swapping the Fiber and Meteor.bindEnvironment, but nothing is different, so...)

I have tried several implementations, and this is the one that gives the least amount of failure. But some tests still fail because the created model is undefined. The errors are inconsistent, so I cannot pinpoint exactly where or what may be causing this. But it is always associated with models created from Factory.

Some of these tests directly fetch models from the collections, and I have yet to see any failure there; but all the random failures are from Factory.create.

@dburles

This comment has been minimized.

Copy link
Member

dburles commented Dec 1, 2017

Hey why the need for the dbTest function? The actual test will work perfectly without it

@yanickrochon

This comment has been minimized.

Copy link

yanickrochon commented Dec 1, 2017

Oh... well... when I started writing this app (Meteor 1.4), I had issues writing tests where it would fail all tests, saying "Error Meteor code must always run within a Fiber". I had written a wrapper, but it was breaking since 1.6. Did anything change for all test to work inside a fiber?

@dburles

This comment has been minimized.

Copy link
Member

dburles commented Dec 1, 2017

You shouldn’t have to worry about it. Check the testing section of the meteor guide for a good setup

@yanickrochon

This comment has been minimized.

Copy link

yanickrochon commented Dec 1, 2017

I have, but I usually code something and don't worry about it afterward. There was an issue, I found an answer, did code a solution, it was working again. 10 months later, BAM! this.

So I ditched the dbTest function, however the problem still persists. It seems that Collection.insert and immediate Collection.findOne does not work. I have tried to replicate this directly with a dummy collection, and found out that, about 1/20000 insertions followed with an immediate fetch, a value of undefined was returned for the newly inserted _id.

So, I can only assume that this is a Mongo / Meteor problem, and not this package. Thank you for the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment