0

In my MochaJS Test, my Firebase data is not sending when I have Promise-terminating code in, but it will push when the code is not present (but the test won't terminate). How do I fix that?

Note: The output is the same, except it will hang if the Promise-resolving code is not present.

Output:

OK Attempting to inject new event with id eidMAEfy
Attempting to set /db/events/eidMAEfy to {
... JSON DATA ...
}
* HANGS HERE IF RESOLVE NOT PRESENT*

Test.js:

describe('*NEW* Create New Event Test Suite', async function() {                                                                                                                                      

  this.timeout(0);
  let new_event_req_body = unformalized_new_test_event();
  let is_preexisting = false;

      it('creates a new event', async function() {
        return new Promise(async function (resolve, reject) {

            try {

               let res = await utils.create_new_event(new_event_req_body, is_preexisting); // ISSUE. Remove line & it posts the data (but never terminates test)
               assert.isOk('Ok','Ok');
               resolve('ok');

            } catch (e) {
               assert.fail(e);
               reject('noo');
            }
        })
      });
});

Utils.js

var create_new_event = async (req_body, is_preexisting) => {
    return new Promise( async function(resolve, reject) {
        // make json
        let new_event_json = new_event_result(req_body);
        if (!new_event_json) reject("Invalid Input");
        let event_id = new_event_json.id;
        ok_log("Attempting to inject new event with id " + event_id);


        let dbstring = '/db/events/'+event_id;
        log('Attempting to set ' + dbstring + ' to ' + prettify(new_event_json));
        // post data
        let re = root.ref(dbstring);
        re.set(new_event_json);

        resolve(event_id);           // Always resolves
    });
}
Ryan Cocuzzo
  • 3,109
  • 7
  • 35
  • 64
  • [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! You should drop the whole `return new Promise` wrapper thing, and just `return` or `throw` from the outer `async function`. – Bergi Jan 11 '20 at 22:41
  • @Bergi Appreciate the response, but I dropped the Promise wrapping and it did not fix the issue (Code above). The issue is that the tests are resolving/terminating before the data is fully sent. – Ryan Cocuzzo Jan 11 '20 at 22:57

0 Answers0