[SOLVED] How to mock collectionHooks while testing

Hello, I’m not sure I understand the problem, but I want to mock collection hooks in server-integration tests. My goal is to not let them run. It it possible?
I’ve tried:

  • `SpyOn(collection.after, “update”) in spec – no effect

  • z_collection-hooks-stub.js putted into server/integration folder – errors ReferenceError: Mongo is not defined

I dont find information that your custom blackbox script is throwing reference error very helpful.
If I want to monitor side-effects like collection hooks, I would probably just cycle collection.find test few times and timeout it. Or somehow use observeChanges on the side-effect collection with selector of expected document to update monitored test level variable.
Than you can wait some test framework time if the variable will be updated as expected.
Dont forget to kill the observeChanges at the end of test.

1 Like

Here in the readme it says:

When adding a hook, a handler object is returned with these methods:

remove(): will remove that particular hook;
replace(callback, options): will replace the hook callback and options.

So you can use the replace method of the handler to mock a collection hook. Don’t forget to reset the collection hook with another replace after the test :wink:

1 Like

We are found not very gentle solution, how to mock collection hook, which is work for as:

beforeAll(function () {
  if (Array.isArray(CollectionName._hookAspects.update.after) &&
    CollectionName._hookAspects.remove.after.length) {
    spyOn(CollectionName._hookAspects.update.after[0], "aspect");
  }
})