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

Inconsistent errors server-side, factory not created every time #32

Closed
Floriferous opened this Issue Jul 20, 2017 · 9 comments

Comments

Projects
None yet
3 participants
@Floriferous
Copy link

Floriferous commented Jul 20, 2017

I'm having lots of trouble making my tests run consistently every time, with errors coming from this package. It is very well possible that it is my fault, but I don't understand why.

This is one of the errors I see on the first run of my tests (though not every time), and not on a subsequent rerun of my tests, only on the server, never on the client:

Error: Cannot read property '_id' of undefined
    at makeRelation (packages/dburles:factory/factory.js:45:14)
    at packages/dburles:factory/factory.js:71:20
    at Function._.each._.forEach (packages/underscore.js:147:22)
    at walk (packages/dburles:factory/factory.js:67:7)
    at Function.Factory._build (packages/dburles:factory/factory.js:97:3)
    at Function.Factory.create (packages/dburles:factory/factory.js:121:23)
    at Hook.beforeEach2 (imports/api/loanrequests/test/loanRequests.test.js:67:27)
    at run (packages/practicalmeteor:mocha-core/server.js:34:29)

On circle CI, this test keeps failing but with a timeout error:
timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

These are the 3 factories that are involved in this error:

// Top level
Factory.define('loanRequest', LoanRequests, {
  userId: Factory.get('user'),
  createdAt: () => new Date(),
  general: () => ({
    fortuneUsed: 250000,
    insuranceFortuneUsed: 0,
    partnersToAvoid: ['joe', 'john'],
  }),
  borrowers: [Factory.get('borrower')],
  property: () => ({ value: 1000000 }),
  files: () => ({}),
  logic: () => ({ auction: {}, lender: {}, verification: {}, step: 1 }),
  name: () => 'request name',
  emails: () => [],
});

// Linked
Factory.define('user', Meteor.users, {
  roles: () => 'user',
  emails: () => [{ address: chance.email(), verified: false }],
});

Factory.define('borrower', Borrowers, {
  userId: Factory.get('user'),
  createdAt: () => new Date(),
  expenses: () => [{ description: 'test', value: 1 }],
  files: () => ({}),
  logic: () => ({}),
});

And this is the structure of my test file:

describe('loanRequests', function () {
  beforeEach(function beforeEach1() {
    // xolvio:cleaner
    resetDatabase();
  });

  describe('modifiers', () => {
    let request;
    beforeEach(function beforeEach2() {
      request = Factory.create('loanRequest');
    });

    it('should work', () => {
      // This is where it fails
      const id = request._id;

      // ..test..
    })
  });
});
@dburles

This comment has been minimized.

Copy link
Member

dburles commented Jul 20, 2017

Hey @Floriferous you might need to either set a longer timeout (see: https://mochajs.org/#timeouts) or better stub your collections using https://atmospherejs.com/hwillson/stub-collections

@Floriferous

This comment has been minimized.

Copy link

Floriferous commented Jul 24, 2017

Seems like it might not be related to this package, but to meteor-mocha..

practicalmeteor/meteor-mocha#79

@dburles

This comment has been minimized.

Copy link
Member

dburles commented Jul 24, 2017

yeah definitely, I'd make use of stub-collections if possible, your tests will run a lot faster

@dburles

This comment has been minimized.

Copy link
Member

dburles commented Jul 24, 2017

and in that case you probably won't have to worry about your tests timing out

@dburles

This comment has been minimized.

@Floriferous

This comment has been minimized.

Copy link

Floriferous commented Aug 14, 2017

It was indeed an error with stub collections taking an order of magnitude longer to stub on the first call. So I fixed this by stubbing and restoring my collections once before all tests. Thanks for your help!

@yanickrochon

This comment has been minimized.

Copy link

yanickrochon commented Nov 27, 2017

I have this issue, and it's not related to Mocha. I have installed the stub-collections package, but it does not help. The problem happens since Meteor 1.6 as my tests never shown anything like this before (that I recall). I see random errors happening, where Factory.create returns undefined.

No idea why. All my tests run inside a Fiber (Meteor.bindEnvironment) and (Meteor.wrapAsync)

@yanickrochon

This comment has been minimized.

Copy link

yanickrochon commented Nov 30, 2017

Nivermind, Meteor's documentation is not clear about how Meteor.wrapAsync actually work; I wrap my tests in Meteor.startup functions and it seems to work, now.

@Floriferous

This comment has been minimized.

Copy link

Floriferous commented Jul 19, 2018

I'm still seeing this, I removed stub collections everywhere, as it just produces unreliable tests.

Now I keep seeing Factory.create just return undefined at random (seemingly).

Could it have something to do with allow/deny, that I set on every collection like this ? :

MyCollection.deny({
  insert: () => true,
  update: () => true,
  remove: () => true,
});
MyCollection.allow({
  insert: () => false,
  update: () => false,
  remove: () => false,
});

It really shouldn't since my tests are failing server-side, but I'm at loss for why this keeps coming back and I'm never feeling confident in my meteor testing setup.

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