8

This is my first foray into jest. I am attempting to run the "stock" test for my service, i.e., the one feathersjs sets up for you when it creates the service via the cli. The service uses an asychronous function with a callback, where I do some logging to the console as a final step. I am getting the error: "Cannot log after tests are done. Did you forget to wait for something async in your test?" I suspect that the test isn't waiting for the callback to complete before exiting, so when the callback executes tries to log after the tests finish.

Here's the stock test:

  it('registered the service', () => {
    const service = app.service('order');
    expect(service).toBeTruthy();
  });

I've tried some techniques mentioned in the documentation, like async/await:

  it('registered the service', async () => {
    const service = await app.service('order');
    expect(service).toBeTruthy();
  });

and I've tried using "done": I get the same error message each time.

Here's the part of the service code that's doing the logging:

  amqp.connect(amqp_url, (err0, conn) => {
    if(err0) {
      throw(err0);
    }

    conn.createChannel((err1, ch) => {
      if(err1) {
        throw(err1);
      }

      ch.assertQueue(orchToOrderQName);
      ch.consume(orchToOrderQName, function(msg) {
        ch.ack(msg);
        service.emit('create', msg);
      });
      orchToOrderChannel = ch;
    });

    conn.createChannel((err1, ch) => {
      if(err1) {
        throw(err1);
      }

      ch.assertQueue(orderToOrchQName);
      orderToOrchChannel = ch;
  
      console.log(" [*] Order is consuming messages from %s.", orchToOrderQName);
    });

  });

I think I need to find a way for the test to wait on the callback, but maybe the problem is elsewhere.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
boing
  • 499
  • 2
  • 6
  • 22
  • Do you know why the solution worked? The original code looks good to me. Since I met the same issue and can not figure out what's wrong. Which version of Jest were you using? thanks. – Lisa Apr 22 '20 at 02:03
  • Yeah same here. I used what was suggested in other posts like putting await in the function call in the tests but I still receive the same error. Adding `done()` does nothing either – Kim Merino Apr 26 '21 at 20:26

1 Answers1

0

I was able to get around this by changing the stock test:

service = null;
beforeAll(async () => {
    service = await app.service('order');
});

it('registered the service', () => {
    expect(service).toBeTruthy();
});

and I no longer get the error message about logging.


This answer was posted as an edit to the question Why am I getting "Cannot log after tests are done?" by the OP boing under CC BY-SA 4.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81