Skip to content
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

Cannot chain existing mongoose query object #67

Open
newmanw opened this issue Oct 2, 2020 · 1 comment
Open

Cannot chain existing mongoose query object #67

newmanw opened this issue Oct 2, 2020 · 1 comment

Comments

@newmanw
Copy link

newmanw commented Oct 2, 2020

const query = Model.find(conditions, fields, options);
if (someCondition) {
  query.populate('someField)
}
query.exec(callback);

Trying to test populate is called

sinon.mock(Model)
  .expects('find')
  .chain('populate).withArgs('someField')
  .chain('exec')
  .yields(null, {
    foo: 'bar'
  });

I get the following error:

Uncaught TypeError: Cannot read property 'emit' of undefined

If I remove the chain for 'populate' it works just fine.

Debugging further it looks like the yields is not returning the mock object causing a failure further in the test.

@newmanw
Copy link
Author

newmanw commented Oct 2, 2020

Looks like I can modify the code to reassign the query as a work around.

let query = Model.find(conditions, fields, options);
if (someCondition) {
  query = query.populate('someField)
}
query.exec(callback);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant