parse-server-test-runner icon indicating copy to clipboard operation
parse-server-test-runner copied to clipboard

Example for testing rejected Cloud triggers

Open danepowell opened this issue 7 years ago • 0 comments

I have a beforeSave trigger that validates objects and throws errors if they fail validation. I want to test that this validation is working as expected.

I can't figure out how to catch the exception / error and prevent Jasmine from failing. Even if I try a structure like this, Jasmine fails early, the catch() block is never even called. I suspect because it's catching the console error thrown by the server backend first. I would have expected the spyOn function to catch that.

// ...
beforeEach((done) => {
    spyOn(console, 'error');
    dropDB()
      .then(done).catch(done.fail);
  });
// ...
it('should reject invalid items', async (done) => {
  // Set up an `item` for saving.
  try {
    await item.save(null, {sessionToken: user.getSessionToken()})
  }
  catch (error) {
    // check error, etc. This is never even called, even though beforeSave is definitely throwing an exception.
    done();
  }
}

danepowell avatar Sep 10 '18 14:09 danepowell